@squoosh-kit/vite-plugin 0.1.5 → 0.1.7

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.
package/dist/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
+ import type { ViteDevServer } from 'vite';
1
2
  export default function squooshVitePlugin(squooshKitRoot: string): {
2
3
  name: string;
3
4
  buildStart(this: import("rollup").PluginContext): void;
4
- config: () => {
5
+ config: (this: import("vite").ConfigPluginContext) => {
5
6
  server: {
6
7
  fs: {
7
8
  allow: string[];
@@ -16,16 +17,7 @@ export default function squooshVitePlugin(squooshKitRoot: string): {
16
17
  exclude: string[];
17
18
  };
18
19
  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
- };
20
+ configureServer(server: ViteDevServer): void;
28
21
  };
29
- configureServer(this: import("vite").MinimalPluginContextWithoutEnvironment, server: import("vite").ViteDevServer): void;
30
22
  };
31
23
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA0CA,MAAM,CAAC,OAAO,UAAU,iBAAiB,CAAC,cAAc,EAAE,MAAM;;;;;;;;;;;;;;;;;;;;;gDA8CxB;wBAAE,IAAI,CAAC,EAAE,MAAM,CAAA;qBAAE;;;;;;EA2DxD"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAgB,aAAa,EAAE,MAAM,MAAM,CAAC;AA+BxD,MAAM,CAAC,OAAO,UAAU,iBAAiB,CAAC,cAAc,EAAE,MAAM;;;;;;;;;;;;;;;;;;gCAoClC,aAAa;;EAmD1C"}
package/dist/index.js CHANGED
@@ -9,7 +9,6 @@ import {
9
9
  readFileSync
10
10
  } from "fs";
11
11
  import { join } from "path";
12
- import { searchForWorkspaceRoot } from "vite";
13
12
  function copyBrowserFiles(srcDir, destDir) {
14
13
  if (!existsSync(srcDir)) {
15
14
  console.warn(`Source directory does not exist: ${srcDir}`);
@@ -37,12 +36,6 @@ function squooshVitePlugin(squooshKitRoot) {
37
36
  return {
38
37
  name: "squoosh-vite-plugin",
39
38
  buildStart() {
40
- console.log("Copying Squoosh browser assets...");
41
- console.log("squooshKitRoot", squooshKitRoot);
42
- console.log("viteRoot", viteRoot);
43
- console.log("publicDir", publicDir);
44
- console.log("webpDist", webpDist);
45
- console.log("resizeDist", resizeDist);
46
39
  if (existsSync(publicDir)) {
47
40
  rmSync(publicDir, { recursive: true, force: true });
48
41
  }
@@ -57,7 +50,7 @@ function squooshVitePlugin(squooshKitRoot) {
57
50
  config: () => ({
58
51
  server: {
59
52
  fs: {
60
- allow: [searchForWorkspaceRoot(process.cwd())]
53
+ allow: [viteRoot, squooshKitRoot]
61
54
  },
62
55
  headers: {
63
56
  "Cross-Origin-Embedder-Policy": "require-corp",
@@ -69,51 +62,38 @@ function squooshVitePlugin(squooshKitRoot) {
69
62
  exclude: ["@squoosh-kit/webp", "@squoosh-kit/resize"]
70
63
  },
71
64
  assetsInclude: ["**/*.wasm"],
72
- build: {
73
- rollupOptions: {
74
- output: {
75
- assetFileNames: (assetInfo) => {
76
- if (assetInfo.name?.endsWith(".wasm")) {
77
- return "assets/[name].[ext]";
78
- }
79
- return "assets/[name]-[hash].[ext]";
65
+ configureServer(server) {
66
+ server.middlewares.use((req, res, next) => {
67
+ const url = req.url;
68
+ if (!url?.includes(".wasm")) {
69
+ next();
70
+ return;
71
+ }
72
+ try {
73
+ const cleanUrl = url.split("?")[0];
74
+ const urlPath = cleanUrl?.startsWith("/") ? cleanUrl.slice(1) : cleanUrl;
75
+ const pathsToTry = [
76
+ join(squooshKitRoot, urlPath ?? ""),
77
+ urlPath?.includes("@squoosh-kit/wasm/") ? join(squooshKitRoot, urlPath.replace(/node_modules\/@squoosh-kit\/wasm\//, "webp/dist/wasm/")) : null
78
+ ].filter((p) => p !== null);
79
+ for (const filePath of pathsToTry) {
80
+ try {
81
+ const wasmData = readFileSync(filePath);
82
+ res.setHeader("Content-Type", "application/wasm");
83
+ res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
84
+ res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
85
+ res.setHeader("Content-Length", wasmData.length.toString());
86
+ res.end(wasmData);
87
+ return;
88
+ } catch {}
80
89
  }
90
+ next();
91
+ } catch {
92
+ next();
81
93
  }
82
- }
94
+ });
83
95
  }
84
- }),
85
- configureServer(server) {
86
- server.middlewares.use((req, res, next) => {
87
- const url = req.url;
88
- if (!url?.includes(".wasm")) {
89
- next();
90
- return;
91
- }
92
- try {
93
- const workspaceRoot = searchForWorkspaceRoot(process.cwd());
94
- const cleanUrl = url.split("?")[0];
95
- const urlPath = cleanUrl?.startsWith("/") ? cleanUrl.slice(1) : cleanUrl;
96
- const pathsToTry = [
97
- join(workspaceRoot, urlPath ?? ""),
98
- urlPath?.includes("@squoosh-kit/wasm/") ? join(workspaceRoot, urlPath.replace(/node_modules\/@squoosh-kit\/wasm\//, "node_modules/@squoosh-kit/webp/dist/wasm/")) : null
99
- ].filter((p) => p !== null);
100
- for (const filePath of pathsToTry) {
101
- try {
102
- const wasmData = readFileSync(filePath);
103
- res.setHeader("Content-Type", "application/wasm");
104
- res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
105
- res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
106
- res.setHeader("Content-Length", wasmData.length.toString());
107
- res.end(wasmData);
108
- return;
109
- } catch {}
110
- }
111
- next();
112
- } catch {
113
- next();
114
- }
115
- });
116
- }
96
+ })
117
97
  };
118
98
  }
119
99
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squoosh-kit/vite-plugin",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
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>",