@squoosh-kit/vite-plugin 0.1.6 → 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,6 +17,7 @@ export default function squooshVitePlugin(squooshKitRoot: string): {
16
17
  exclude: string[];
17
18
  };
18
19
  assetsInclude: string[];
20
+ configureServer(server: ViteDevServer): void;
19
21
  };
20
22
  };
21
23
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAmCA,MAAM,CAAC,OAAO,UAAU,iBAAiB,CAAC,cAAc,EAAE,MAAM;;;;;;;;;;;;;;;;;;;EA6C/D"}
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
@@ -1,8 +1,14 @@
1
1
  // @bun
2
2
  // src/index.ts
3
- import { existsSync, mkdirSync, cpSync, readdirSync, rmSync } from "fs";
3
+ import {
4
+ existsSync,
5
+ mkdirSync,
6
+ cpSync,
7
+ readdirSync,
8
+ rmSync,
9
+ readFileSync
10
+ } from "fs";
4
11
  import { join } from "path";
5
- import { searchForWorkspaceRoot } from "vite";
6
12
  function copyBrowserFiles(srcDir, destDir) {
7
13
  if (!existsSync(srcDir)) {
8
14
  console.warn(`Source directory does not exist: ${srcDir}`);
@@ -30,12 +36,6 @@ function squooshVitePlugin(squooshKitRoot) {
30
36
  return {
31
37
  name: "squoosh-vite-plugin",
32
38
  buildStart() {
33
- console.log("Copying Squoosh browser assets...");
34
- console.log("squooshKitRoot", squooshKitRoot);
35
- console.log("viteRoot", viteRoot);
36
- console.log("publicDir", publicDir);
37
- console.log("webpDist", webpDist);
38
- console.log("resizeDist", resizeDist);
39
39
  if (existsSync(publicDir)) {
40
40
  rmSync(publicDir, { recursive: true, force: true });
41
41
  }
@@ -50,7 +50,7 @@ function squooshVitePlugin(squooshKitRoot) {
50
50
  config: () => ({
51
51
  server: {
52
52
  fs: {
53
- allow: [searchForWorkspaceRoot(process.cwd())]
53
+ allow: [viteRoot, squooshKitRoot]
54
54
  },
55
55
  headers: {
56
56
  "Cross-Origin-Embedder-Policy": "require-corp",
@@ -61,7 +61,38 @@ function squooshVitePlugin(squooshKitRoot) {
61
61
  optimizeDeps: {
62
62
  exclude: ["@squoosh-kit/webp", "@squoosh-kit/resize"]
63
63
  },
64
- assetsInclude: ["**/*.wasm"]
64
+ assetsInclude: ["**/*.wasm"],
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 {}
89
+ }
90
+ next();
91
+ } catch {
92
+ next();
93
+ }
94
+ });
95
+ }
65
96
  })
66
97
  };
67
98
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squoosh-kit/vite-plugin",
3
- "version": "0.1.6",
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>",