@squoosh-kit/vite-plugin 0.1.0 → 0.1.3

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":"AAyDA,MAAM,CAAC,OAAO,UAAU,iBAAiB;;;;;;;;;;;;;;;;;;;;;gDA0CD;wBAAE,IAAI,CAAC,EAAE,MAAM,CAAA;qBAAE;;;;;;EA2DxD"}
package/dist/index.js ADDED
@@ -0,0 +1,130 @@
1
+ // @bun
2
+ var __require = import.meta.require;
3
+
4
+ // src/index.ts
5
+ import {
6
+ existsSync,
7
+ mkdirSync,
8
+ cpSync,
9
+ readdirSync,
10
+ rmSync,
11
+ readFileSync
12
+ } from "fs";
13
+ import { join, dirname } from "path";
14
+ import { searchForWorkspaceRoot } from "vite";
15
+ function copyBrowserFiles(srcDir, destDir) {
16
+ if (!existsSync(srcDir)) {
17
+ console.warn(`Source directory does not exist: ${srcDir}`);
18
+ return;
19
+ }
20
+ mkdirSync(destDir, { recursive: true });
21
+ const files = readdirSync(srcDir);
22
+ const browserFiles = files.filter((file) => file.endsWith(".browser.mjs") || file.endsWith(".browser.mjs.map") || file.endsWith(".d.ts") || file.endsWith(".d.ts.map"));
23
+ for (const file of browserFiles) {
24
+ const srcPath = join(srcDir, file);
25
+ const destPath = join(destDir, file);
26
+ cpSync(srcPath, destPath);
27
+ }
28
+ const wasmDir = join(srcDir, "wasm");
29
+ if (existsSync(wasmDir)) {
30
+ const destWasmDir = join(destDir, "wasm");
31
+ cpSync(wasmDir, destWasmDir, { recursive: true });
32
+ }
33
+ }
34
+ function resolvePackagePath(packageName, from) {
35
+ try {
36
+ const packageJsonPath = __require.resolve(`${packageName}/package.json`, {
37
+ paths: [from]
38
+ });
39
+ const packageDir = dirname(packageJsonPath);
40
+ return join(packageDir, "dist");
41
+ } catch (err) {
42
+ console.warn(`[squoosh-vite-plugin] Could not find package "${packageName}". It might not be installed. Skipping asset copy for this package.: ${err}`);
43
+ return null;
44
+ }
45
+ }
46
+ function squooshVitePlugin() {
47
+ return {
48
+ name: "squoosh-vite-plugin",
49
+ buildStart() {
50
+ console.log("Copying Squoosh browser assets...");
51
+ const viteRoot = process.cwd();
52
+ const publicDir = join(viteRoot, "public", "squoosh-kit");
53
+ const webpDist = resolvePackagePath("@squoosh-kit/webp", viteRoot);
54
+ const resizeDist = resolvePackagePath("@squoosh-kit/resize", viteRoot);
55
+ if (existsSync(publicDir)) {
56
+ rmSync(publicDir, { recursive: true, force: true });
57
+ }
58
+ if (webpDist) {
59
+ copyBrowserFiles(webpDist, join(publicDir, "webp"));
60
+ }
61
+ if (resizeDist) {
62
+ copyBrowserFiles(resizeDist, join(publicDir, "resize"));
63
+ }
64
+ console.log("Squoosh assets copied successfully!");
65
+ },
66
+ config: () => ({
67
+ server: {
68
+ fs: {
69
+ allow: [searchForWorkspaceRoot(process.cwd())]
70
+ },
71
+ headers: {
72
+ "Cross-Origin-Embedder-Policy": "require-corp",
73
+ "Cross-Origin-Opener-Policy": "same-origin",
74
+ "Access-Control-Allow-Origin": "*"
75
+ }
76
+ },
77
+ optimizeDeps: {
78
+ exclude: ["@squoosh-kit/webp", "@squoosh-kit/resize"]
79
+ },
80
+ assetsInclude: ["**/*.wasm"],
81
+ build: {
82
+ rollupOptions: {
83
+ output: {
84
+ assetFileNames: (assetInfo) => {
85
+ if (assetInfo.name?.endsWith(".wasm")) {
86
+ return "assets/[name].[ext]";
87
+ }
88
+ return "assets/[name]-[hash].[ext]";
89
+ }
90
+ }
91
+ }
92
+ }
93
+ }),
94
+ configureServer(server) {
95
+ server.middlewares.use((req, res, next) => {
96
+ const url = req.url;
97
+ if (!url?.includes(".wasm")) {
98
+ next();
99
+ return;
100
+ }
101
+ try {
102
+ const workspaceRoot = searchForWorkspaceRoot(process.cwd());
103
+ const cleanUrl = url.split("?")[0];
104
+ const urlPath = cleanUrl?.startsWith("/") ? cleanUrl.slice(1) : cleanUrl;
105
+ const pathsToTry = [
106
+ join(workspaceRoot, urlPath ?? ""),
107
+ urlPath?.includes("@squoosh-kit/wasm/") ? join(workspaceRoot, urlPath.replace(/node_modules\/@squoosh-kit\/wasm\//, "node_modules/@squoosh-kit/webp/dist/wasm/")) : null
108
+ ].filter((p) => p !== null);
109
+ for (const filePath of pathsToTry) {
110
+ try {
111
+ const wasmData = readFileSync(filePath);
112
+ res.setHeader("Content-Type", "application/wasm");
113
+ res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
114
+ res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
115
+ res.setHeader("Content-Length", wasmData.length.toString());
116
+ res.end(wasmData);
117
+ return;
118
+ } catch {}
119
+ }
120
+ next();
121
+ } catch {
122
+ next();
123
+ }
124
+ });
125
+ }
126
+ };
127
+ }
128
+ export {
129
+ squooshVitePlugin as default
130
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squoosh-kit/vite-plugin",
3
- "version": "0.1.0",
3
+ "version": "0.1.3",
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
- }