@squoosh-kit/vite-plugin 0.0.45 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,31 @@
1
+ export default function squooshVitePlugin(): {
2
+ name: string;
3
+ buildStart(this: import("rollup").PluginContext): void;
4
+ config: () => {
5
+ server: {
6
+ fs: {
7
+ allow: string[];
8
+ };
9
+ headers: {
10
+ 'Cross-Origin-Embedder-Policy': string;
11
+ 'Cross-Origin-Opener-Policy': string;
12
+ 'Access-Control-Allow-Origin': string;
13
+ };
14
+ };
15
+ optimizeDeps: {
16
+ exclude: string[];
17
+ };
18
+ assetsInclude: string[];
19
+ build: {
20
+ rollupOptions: {
21
+ output: {
22
+ assetFileNames: (assetInfo: {
23
+ name?: string;
24
+ }) => "assets/[name].[ext]" | "assets/[name]-[hash].[ext]";
25
+ };
26
+ };
27
+ };
28
+ };
29
+ configureServer(this: import("vite").MinimalPluginContextWithoutEnvironment, server: import("vite").ViteDevServer): void;
30
+ };
31
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA0CA,MAAM,CAAC,OAAO,UAAU,iBAAiB;;;;;;;;;;;;;;;;;;;;;gDAuCD;wBAAE,IAAI,CAAC,EAAE,MAAM,CAAA;qBAAE;;;;;;EA2DxD"}
package/dist/index.js ADDED
@@ -0,0 +1,113 @@
1
+ // @bun
2
+ // src/index.ts
3
+ import {
4
+ existsSync,
5
+ mkdirSync,
6
+ cpSync,
7
+ readdirSync,
8
+ rmSync,
9
+ readFileSync
10
+ } from "fs";
11
+ import { join } from "path";
12
+ import { searchForWorkspaceRoot } from "vite";
13
+ function copyBrowserFiles(srcDir, destDir) {
14
+ if (!existsSync(srcDir)) {
15
+ console.warn(`Source directory does not exist: ${srcDir}`);
16
+ return;
17
+ }
18
+ mkdirSync(destDir, { recursive: true });
19
+ const files = readdirSync(srcDir);
20
+ const browserFiles = files.filter((file) => file.endsWith(".browser.mjs") || file.endsWith(".browser.mjs.map") || file.endsWith(".d.ts") || file.endsWith(".d.ts.map"));
21
+ for (const file of browserFiles) {
22
+ const srcPath = join(srcDir, file);
23
+ const destPath = join(destDir, file);
24
+ cpSync(srcPath, destPath);
25
+ }
26
+ const wasmDir = join(srcDir, "wasm");
27
+ if (existsSync(wasmDir)) {
28
+ const destWasmDir = join(destDir, "wasm");
29
+ cpSync(wasmDir, destWasmDir, { recursive: true });
30
+ }
31
+ }
32
+ function squooshVitePlugin() {
33
+ return {
34
+ name: "squoosh-vite-plugin",
35
+ buildStart() {
36
+ console.log("Copying Squoosh browser assets...");
37
+ const viteRoot = process.cwd();
38
+ const projectRoot = searchForWorkspaceRoot(viteRoot);
39
+ const publicDir = join(viteRoot, "public", "squoosh-kit");
40
+ const webpDist = join(projectRoot, "packages", "webp", "dist");
41
+ const resizeDist = join(projectRoot, "packages", "resize", "dist");
42
+ if (existsSync(publicDir)) {
43
+ rmSync(publicDir, { recursive: true, force: true });
44
+ }
45
+ copyBrowserFiles(webpDist, join(publicDir, "webp"));
46
+ copyBrowserFiles(resizeDist, join(publicDir, "resize"));
47
+ console.log("Squoosh assets copied successfully!");
48
+ },
49
+ config: () => ({
50
+ server: {
51
+ fs: {
52
+ allow: [searchForWorkspaceRoot(process.cwd())]
53
+ },
54
+ headers: {
55
+ "Cross-Origin-Embedder-Policy": "require-corp",
56
+ "Cross-Origin-Opener-Policy": "same-origin",
57
+ "Access-Control-Allow-Origin": "*"
58
+ }
59
+ },
60
+ optimizeDeps: {
61
+ exclude: ["@squoosh-kit/webp", "@squoosh-kit/resize"]
62
+ },
63
+ assetsInclude: ["**/*.wasm"],
64
+ build: {
65
+ rollupOptions: {
66
+ output: {
67
+ assetFileNames: (assetInfo) => {
68
+ if (assetInfo.name?.endsWith(".wasm")) {
69
+ return "assets/[name].[ext]";
70
+ }
71
+ return "assets/[name]-[hash].[ext]";
72
+ }
73
+ }
74
+ }
75
+ }
76
+ }),
77
+ configureServer(server) {
78
+ server.middlewares.use((req, res, next) => {
79
+ const url = req.url;
80
+ if (!url?.includes(".wasm")) {
81
+ next();
82
+ return;
83
+ }
84
+ try {
85
+ const workspaceRoot = searchForWorkspaceRoot(process.cwd());
86
+ const cleanUrl = url.split("?")[0];
87
+ const urlPath = cleanUrl?.startsWith("/") ? cleanUrl.slice(1) : cleanUrl;
88
+ const pathsToTry = [
89
+ join(workspaceRoot, urlPath ?? ""),
90
+ urlPath?.includes("@squoosh-kit/wasm/") ? join(workspaceRoot, urlPath.replace(/node_modules\/@squoosh-kit\/wasm\//, "node_modules/@squoosh-kit/webp/dist/wasm/")) : null
91
+ ].filter((p) => p !== null);
92
+ for (const filePath of pathsToTry) {
93
+ try {
94
+ const wasmData = readFileSync(filePath);
95
+ res.setHeader("Content-Type", "application/wasm");
96
+ res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
97
+ res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
98
+ res.setHeader("Content-Length", wasmData.length.toString());
99
+ res.end(wasmData);
100
+ return;
101
+ } catch {}
102
+ }
103
+ next();
104
+ } catch {
105
+ next();
106
+ }
107
+ });
108
+ }
109
+ };
110
+ }
111
+ export {
112
+ squooshVitePlugin as default
113
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squoosh-kit/vite-plugin",
3
- "version": "0.0.45",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "description": "Vite plugin for Squoosh Kit - handles asset copying and WASM configuration",
6
6
  "author": "Bartosz Nowak <bnowak008@gmail.com>",
@@ -15,17 +15,17 @@
15
15
  "access": "public",
16
16
  "registry": "https://registry.npmjs.org/"
17
17
  },
18
- "main": "./src/index.ts",
19
- "types": "./src/index.ts",
18
+ "main": "./dist/index.js",
19
+ "types": "./dist/index.d.ts",
20
20
  "exports": {
21
21
  ".": {
22
- "types": "./src/index.ts",
23
- "import": "./src/index.ts",
24
- "require": "./src/index.ts"
22
+ "types": "./dist/index.d.ts",
23
+ "import": "./dist/index.js",
24
+ "require": "./dist/index.cjs"
25
25
  }
26
26
  },
27
27
  "files": [
28
- "src/**",
28
+ "dist/**",
29
29
  "README.md"
30
30
  ],
31
31
  "sideEffects": false,
package/src/index.ts DELETED
@@ -1,141 +0,0 @@
1
- import {
2
- existsSync,
3
- mkdirSync,
4
- cpSync,
5
- readdirSync,
6
- rmSync,
7
- readFileSync,
8
- } from 'fs';
9
- import { join } from 'path';
10
- import type { PluginOption } from 'vite';
11
- import { searchForWorkspaceRoot } from 'vite';
12
-
13
- function copyBrowserFiles(srcDir: string, destDir: string) {
14
- if (!existsSync(srcDir)) {
15
- console.warn(`Source directory does not exist: ${srcDir}`);
16
- return;
17
- }
18
-
19
- mkdirSync(destDir, { recursive: true });
20
-
21
- const files = readdirSync(srcDir);
22
- const browserFiles = files.filter(
23
- (file) =>
24
- file.endsWith('.browser.mjs') ||
25
- file.endsWith('.browser.mjs.map') ||
26
- file.endsWith('.d.ts') ||
27
- file.endsWith('.d.ts.map')
28
- );
29
-
30
- for (const file of browserFiles) {
31
- const srcPath = join(srcDir, file);
32
- const destPath = join(destDir, file);
33
- cpSync(srcPath, destPath);
34
- }
35
-
36
- const wasmDir = join(srcDir, 'wasm');
37
- if (existsSync(wasmDir)) {
38
- const destWasmDir = join(destDir, 'wasm');
39
- cpSync(wasmDir, destWasmDir, { recursive: true });
40
- }
41
- }
42
-
43
- export default function squooshVitePlugin() {
44
- return {
45
- name: 'squoosh-vite-plugin',
46
- buildStart() {
47
- console.log('Copying Squoosh browser assets...');
48
-
49
- const viteRoot = process.cwd();
50
- const projectRoot = searchForWorkspaceRoot(viteRoot);
51
- const publicDir = join(viteRoot, 'public', 'squoosh-kit');
52
- const webpDist = join(projectRoot, 'packages', 'webp', 'dist');
53
- const resizeDist = join(projectRoot, 'packages', 'resize', 'dist');
54
-
55
- if (existsSync(publicDir)) {
56
- rmSync(publicDir, { recursive: true, force: true });
57
- }
58
-
59
- copyBrowserFiles(webpDist, join(publicDir, 'webp'));
60
- copyBrowserFiles(resizeDist, join(publicDir, 'resize'));
61
-
62
- console.log('Squoosh assets copied successfully!');
63
- },
64
- config: () => ({
65
- server: {
66
- fs: {
67
- allow: [searchForWorkspaceRoot(process.cwd())],
68
- },
69
- headers: {
70
- 'Cross-Origin-Embedder-Policy': 'require-corp',
71
- 'Cross-Origin-Opener-Policy': 'same-origin',
72
- 'Access-Control-Allow-Origin': '*',
73
- },
74
- },
75
- optimizeDeps: {
76
- exclude: ['@squoosh-kit/webp', '@squoosh-kit/resize'],
77
- },
78
- assetsInclude: ['**/*.wasm'],
79
- build: {
80
- rollupOptions: {
81
- output: {
82
- assetFileNames: (assetInfo: { name?: string }) => {
83
- if (assetInfo.name?.endsWith('.wasm')) {
84
- return 'assets/[name].[ext]';
85
- }
86
- return 'assets/[name]-[hash].[ext]';
87
- },
88
- },
89
- },
90
- },
91
- }),
92
- configureServer(server) {
93
- server.middlewares.use((req, res, next) => {
94
- const url = req.url;
95
- if (!url?.includes('.wasm')) {
96
- next();
97
- return;
98
- }
99
-
100
- try {
101
- const workspaceRoot = searchForWorkspaceRoot(process.cwd());
102
- const cleanUrl = url.split('?')[0];
103
- const urlPath = cleanUrl?.startsWith('/')
104
- ? cleanUrl.slice(1)
105
- : cleanUrl;
106
-
107
- const pathsToTry = [
108
- join(workspaceRoot, urlPath ?? ''),
109
- urlPath?.includes('@squoosh-kit/wasm/')
110
- ? join(
111
- workspaceRoot,
112
- urlPath.replace(
113
- /node_modules\/@squoosh-kit\/wasm\//,
114
- 'node_modules/@squoosh-kit/webp/dist/wasm/'
115
- )
116
- )
117
- : null,
118
- ].filter((p) => p !== null);
119
-
120
- for (const filePath of pathsToTry) {
121
- try {
122
- const wasmData = readFileSync(filePath);
123
- res.setHeader('Content-Type', 'application/wasm');
124
- res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp');
125
- res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
126
- res.setHeader('Content-Length', wasmData.length.toString());
127
- res.end(wasmData);
128
- return;
129
- } catch {
130
- // Try next path
131
- }
132
- }
133
-
134
- next();
135
- } catch {
136
- next();
137
- }
138
- });
139
- },
140
- } satisfies PluginOption;
141
- }