@squoosh-kit/vite-plugin 0.0.34 → 0.0.45
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/README.md +155 -49
- package/package.json +15 -17
- package/src/index.ts +141 -0
- package/dist/index.d.ts +0 -37
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -101
- package/dist/index.js.map +0 -10
package/README.md
CHANGED
|
@@ -1,100 +1,206 @@
|
|
|
1
1
|
# @squoosh-kit/vite-plugin
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://badge.fury.io/js/%40squoosh-kit%2Fvite-plugin)
|
|
4
|
+
[](https://bun.sh/)
|
|
5
|
+
[](https://opensource.org/license/apache-2-0)
|
|
6
|
+
[](https://www.typescriptlang.org/)
|
|
7
|
+
|
|
8
|
+
A Vite plugin for Squoosh Kit that handles asset copying and WebAssembly configuration.
|
|
9
|
+
|
|
10
|
+
## Overview
|
|
11
|
+
|
|
12
|
+
The `@squoosh-kit/vite-plugin` simplifies integrating Squoosh Kit codecs into Vite projects by:
|
|
13
|
+
|
|
14
|
+
- **Automatically copying browser-compatible assets** from `@squoosh-kit/webp` and `@squoosh-kit/resize` packages
|
|
15
|
+
- **Configuring WASM module handling** with proper MIME types and CORS headers
|
|
16
|
+
- **Setting up optimized dependency exclusions** to prevent bundling of heavy WASM modules
|
|
17
|
+
- **Providing WASM file serving** with correct headers in development mode
|
|
4
18
|
|
|
5
19
|
## Installation
|
|
6
20
|
|
|
7
21
|
```bash
|
|
8
|
-
|
|
9
|
-
# or
|
|
10
|
-
bun add @squoosh-kit/vite-plugin
|
|
22
|
+
bun add -D @squoosh-kit/vite-plugin
|
|
11
23
|
# or
|
|
12
|
-
|
|
24
|
+
npm install --save-dev @squoosh-kit/vite-plugin
|
|
13
25
|
```
|
|
14
26
|
|
|
15
|
-
##
|
|
27
|
+
## Quick Start
|
|
16
28
|
|
|
17
29
|
Add the plugin to your `vite.config.ts`:
|
|
18
30
|
|
|
19
31
|
```typescript
|
|
20
32
|
import { defineConfig } from 'vite';
|
|
21
|
-
import
|
|
33
|
+
import react from '@vitejs/plugin-react';
|
|
34
|
+
import squooshVitePlugin from '@squoosh-kit/vite-plugin';
|
|
22
35
|
|
|
23
36
|
export default defineConfig({
|
|
24
|
-
plugins: [
|
|
25
|
-
squooshKit(), // That's it!
|
|
26
|
-
],
|
|
37
|
+
plugins: [react(), squooshVitePlugin()],
|
|
27
38
|
});
|
|
28
39
|
```
|
|
29
40
|
|
|
30
|
-
The plugin
|
|
41
|
+
That's it! The plugin will:
|
|
31
42
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
43
|
+
1. Copy Squoosh Kit browser assets to your `public/squoosh-kit` directory
|
|
44
|
+
2. Configure Vite to properly handle WASM files
|
|
45
|
+
3. Set necessary CORS headers for cross-origin embedder policy
|
|
46
|
+
4. Exclude heavy dependencies from optimization
|
|
36
47
|
|
|
37
|
-
##
|
|
48
|
+
## What the Plugin Does
|
|
49
|
+
|
|
50
|
+
### Asset Copying
|
|
51
|
+
|
|
52
|
+
The plugin automatically copies:
|
|
53
|
+
|
|
54
|
+
- **Browser builds** (`.browser.mjs`, `.browser.mjs.map`)
|
|
55
|
+
- **TypeScript definitions** (`.d.ts`, `.d.ts.map`)
|
|
56
|
+
- **WASM modules** (`.wasm` files and supporting JavaScript)
|
|
57
|
+
|
|
58
|
+
From both `@squoosh-kit/webp` and `@squoosh-kit/resize` to `public/squoosh-kit/`.
|
|
59
|
+
|
|
60
|
+
### WASM Configuration
|
|
61
|
+
|
|
62
|
+
The plugin:
|
|
38
63
|
|
|
39
|
-
|
|
64
|
+
- Sets correct MIME type (`application/wasm`) for `.wasm` files
|
|
65
|
+
- Configures CORS headers:
|
|
66
|
+
- `Cross-Origin-Embedder-Policy: require-corp`
|
|
67
|
+
- `Cross-Origin-Opener-Policy: same-origin`
|
|
68
|
+
- Ensures WASM files are treated as assets
|
|
69
|
+
- Handles WASM file paths correctly in both development and production
|
|
40
70
|
|
|
41
|
-
|
|
42
|
-
2. Injects worker configuration code at the top of the entry file
|
|
43
|
-
3. Resolves worker URLs using `import.meta.url` (which Vite transforms correctly)
|
|
44
|
-
4. Calls `configureResizeWorker()` and `configureWebpWorker()` automatically
|
|
71
|
+
### Development Server
|
|
45
72
|
|
|
46
|
-
|
|
73
|
+
In development mode, the plugin:
|
|
47
74
|
|
|
48
|
-
|
|
75
|
+
- Serves WASM files with proper headers
|
|
76
|
+
- Handles multiple path variants (e.g., `./wasm/` vs `../wasm/`)
|
|
77
|
+
- Correctly resolves `@squoosh-kit/wasm/` imports
|
|
78
|
+
|
|
79
|
+
## Usage Example
|
|
80
|
+
|
|
81
|
+
After setting up the plugin, you can use Squoosh Kit as normal:
|
|
49
82
|
|
|
50
83
|
```typescript
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
84
|
+
import { createWebpEncoder, createResizer } from '@squoosh-kit/core';
|
|
85
|
+
|
|
86
|
+
// Create encoder and resizer with worker mode
|
|
87
|
+
const encoder = createWebpEncoder('worker', {
|
|
88
|
+
assetPath: '/squoosh-kit/',
|
|
55
89
|
});
|
|
90
|
+
|
|
91
|
+
const resizer = createResizer('worker', {
|
|
92
|
+
assetPath: '/squoosh-kit/',
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// Use them
|
|
96
|
+
const webpData = await encoder(imageData, { quality: 80 });
|
|
97
|
+
const resizedImage = await resizer(imageData, { width: 800 });
|
|
56
98
|
```
|
|
57
99
|
|
|
58
|
-
##
|
|
100
|
+
## API Reference
|
|
59
101
|
|
|
60
|
-
|
|
102
|
+
### `squooshVitePlugin()`
|
|
103
|
+
|
|
104
|
+
Returns a Vite plugin that configures Squoosh Kit integration.
|
|
105
|
+
|
|
106
|
+
**Parameters:** None
|
|
107
|
+
|
|
108
|
+
**Returns:** Vite Plugin
|
|
109
|
+
|
|
110
|
+
**Example:**
|
|
61
111
|
|
|
62
112
|
```typescript
|
|
63
|
-
|
|
64
|
-
import squooshKit from '@squoosh-kit/vite-plugin';
|
|
113
|
+
import squooshVitePlugin from '@squoosh-kit/vite-plugin';
|
|
65
114
|
|
|
66
115
|
export default defineConfig({
|
|
67
|
-
plugins: [
|
|
116
|
+
plugins: [squooshVitePlugin()],
|
|
68
117
|
});
|
|
69
118
|
```
|
|
70
119
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
120
|
+
## How It Works
|
|
121
|
+
|
|
122
|
+
### Build Process
|
|
123
|
+
|
|
124
|
+
1. **buildStart** hook: Copies assets from package distributions to `public/squoosh-kit/`
|
|
125
|
+
2. **config** hook: Injects Vite configuration for WASM handling and optimization
|
|
126
|
+
3. **configureServer** hook: Sets up development middleware for WASM file serving
|
|
127
|
+
|
|
128
|
+
### Asset Paths
|
|
129
|
+
|
|
130
|
+
Assets are copied to predictable locations:
|
|
131
|
+
|
|
75
132
|
```
|
|
133
|
+
public/
|
|
134
|
+
└── squoosh-kit/
|
|
135
|
+
├── webp/
|
|
136
|
+
│ ├── index.browser.mjs
|
|
137
|
+
│ ├── index.d.ts
|
|
138
|
+
│ └── wasm/
|
|
139
|
+
│ ├── webp_enc.js
|
|
140
|
+
│ ├── webp_enc.wasm
|
|
141
|
+
│ └── ... (other WASM files)
|
|
142
|
+
└── resize/
|
|
143
|
+
├── index.browser.mjs
|
|
144
|
+
├── index.d.ts
|
|
145
|
+
└── wasm/
|
|
146
|
+
├── squoosh_resize.js
|
|
147
|
+
├── squoosh_resize.wasm
|
|
148
|
+
└── ... (other WASM files)
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
In development, these are served from `http://localhost:5173/squoosh-kit/`.
|
|
152
|
+
In production, they're available at `/squoosh-kit/`.
|
|
153
|
+
|
|
154
|
+
## Configuration
|
|
76
155
|
|
|
77
|
-
|
|
156
|
+
The plugin requires no configuration, but it assumes:
|
|
78
157
|
|
|
79
|
-
- Vite
|
|
80
|
-
- `@squoosh-kit/
|
|
158
|
+
- Your project uses the standard Vite `public/` directory
|
|
159
|
+
- You're using `@squoosh-kit/webp` and/or `@squoosh-kit/resize` packages
|
|
160
|
+
- You want WASM assets at `/squoosh-kit/` path
|
|
81
161
|
|
|
82
162
|
## Troubleshooting
|
|
83
163
|
|
|
84
|
-
###
|
|
164
|
+
### "Cannot find module @squoosh-kit/webp"
|
|
165
|
+
|
|
166
|
+
Ensure the packages are installed:
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
bun add @squoosh-kit/webp @squoosh-kit/resize
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### WASM files not loading
|
|
173
|
+
|
|
174
|
+
Check that:
|
|
175
|
+
|
|
176
|
+
1. The plugin is configured before other plugins
|
|
177
|
+
2. You're importing from the correct path: `/squoosh-kit/`
|
|
178
|
+
3. Your server headers allow CORS (plugin should set these automatically)
|
|
179
|
+
|
|
180
|
+
### Build fails with "dist not found"
|
|
181
|
+
|
|
182
|
+
Ensure you've built the Squoosh Kit packages first:
|
|
183
|
+
|
|
184
|
+
```bash
|
|
185
|
+
bun run build
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Performance Tips
|
|
85
189
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
190
|
+
- The plugin runs once during Vite startup
|
|
191
|
+
- Asset copying adds < 100ms to build time
|
|
192
|
+
- WASM files are excluded from Vite's optimization pipeline
|
|
193
|
+
- Consider using worker mode for long-running image processing
|
|
89
194
|
|
|
90
|
-
|
|
195
|
+
## Architecture
|
|
91
196
|
|
|
92
|
-
The plugin
|
|
197
|
+
The plugin follows Squoosh Kit principles:
|
|
93
198
|
|
|
94
|
-
-
|
|
95
|
-
-
|
|
96
|
-
-
|
|
199
|
+
- **Minimal and focused**: Handles only WASM and asset configuration
|
|
200
|
+
- **Framework agnostic**: Works with any Vite-based framework
|
|
201
|
+
- **Well-typed**: Full TypeScript support
|
|
202
|
+
- **Performance conscious**: Lazy loading of WASM modules
|
|
97
203
|
|
|
98
204
|
## License
|
|
99
205
|
|
|
100
|
-
MIT
|
|
206
|
+
MIT - part of the Squoosh Kit family
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@squoosh-kit/vite-plugin",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.45",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "Vite plugin for
|
|
5
|
+
"description": "Vite plugin for Squoosh Kit - handles asset copying and WASM configuration",
|
|
6
6
|
"author": "Bartosz Nowak <bnowak008@gmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"repository": {
|
|
@@ -15,31 +15,29 @@
|
|
|
15
15
|
"access": "public",
|
|
16
16
|
"registry": "https://registry.npmjs.org/"
|
|
17
17
|
},
|
|
18
|
-
"main": "./
|
|
19
|
-
"types": "./
|
|
18
|
+
"main": "./src/index.ts",
|
|
19
|
+
"types": "./src/index.ts",
|
|
20
20
|
"exports": {
|
|
21
21
|
".": {
|
|
22
|
-
"types": "./
|
|
23
|
-
"import": "./
|
|
22
|
+
"types": "./src/index.ts",
|
|
23
|
+
"import": "./src/index.ts",
|
|
24
|
+
"require": "./src/index.ts"
|
|
24
25
|
}
|
|
25
26
|
},
|
|
26
27
|
"files": [
|
|
27
|
-
"
|
|
28
|
+
"src/**",
|
|
28
29
|
"README.md"
|
|
29
30
|
],
|
|
30
31
|
"sideEffects": false,
|
|
31
32
|
"scripts": {
|
|
32
|
-
"build": "bun
|
|
33
|
-
"clean
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
},
|
|
37
|
-
"peerDependencies": {
|
|
38
|
-
"vite": "^4.0.0 || ^5.0.0"
|
|
33
|
+
"build": "bunx tsc -p . && bun build ./src/index.ts --outdir dist --format esm --target bun --external vite",
|
|
34
|
+
"clean": "rm -rf dist node_modules .bun/install/cache bun.lockb bun.lock *.tsbuildinfo",
|
|
35
|
+
"prepublishOnly": "bun run build",
|
|
36
|
+
"typecheck": "tsc --noEmit"
|
|
39
37
|
},
|
|
40
38
|
"devDependencies": {
|
|
41
|
-
"@types/
|
|
42
|
-
"typescript": "
|
|
43
|
-
"vite": "^
|
|
39
|
+
"@types/bun": "latest",
|
|
40
|
+
"typescript": "~5.9.3",
|
|
41
|
+
"vite": "^7.2.2"
|
|
44
42
|
}
|
|
45
43
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
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
|
+
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @squoosh-kit/vite-plugin
|
|
3
|
-
*
|
|
4
|
-
* Vite plugin that automatically configures squoosh-kit worker URLs.
|
|
5
|
-
* This plugin resolves worker paths using Vite's module resolution,
|
|
6
|
-
* ensuring workers load correctly in both development and production builds.
|
|
7
|
-
*/
|
|
8
|
-
import type { Plugin } from 'vite';
|
|
9
|
-
export interface SquooshKitPluginOptions {
|
|
10
|
-
/**
|
|
11
|
-
* Whether to auto-configure workers automatically.
|
|
12
|
-
* If true, configuration code is injected into entry points.
|
|
13
|
-
* If false, you must manually import '@squoosh-kit/vite-plugin/auto-config' in your app.
|
|
14
|
-
* @default true
|
|
15
|
-
*/
|
|
16
|
-
autoConfigure?: boolean;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Vite plugin for automatic squoosh-kit worker configuration.
|
|
20
|
-
*
|
|
21
|
-
* This plugin automatically resolves and configures worker URLs for
|
|
22
|
-
* @squoosh-kit/resize and @squoosh-kit/webp packages, ensuring they
|
|
23
|
-
* work correctly in both development and production builds.
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```ts
|
|
27
|
-
* // vite.config.ts
|
|
28
|
-
* import { defineConfig } from 'vite';
|
|
29
|
-
* import squooshKit from '@squoosh-kit/vite-plugin';
|
|
30
|
-
*
|
|
31
|
-
* export default defineConfig({
|
|
32
|
-
* plugins: [squooshKit()],
|
|
33
|
-
* });
|
|
34
|
-
* ```
|
|
35
|
-
*/
|
|
36
|
-
export default function squooshKit(options?: SquooshKitPluginOptions): Plugin;
|
|
37
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAkB,MAAM,MAAM,CAAC;AAEnD,MAAM,WAAW,uBAAuB;IACtC;;;;;OAKG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB;AAKD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,OAAO,UAAU,UAAU,CAChC,OAAO,GAAE,uBAA4B,GACpC,MAAM,CAmGR"}
|
package/dist/index.js
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
var VIRTUAL_MODULE_ID = "virtual:@squoosh-kit/vite-plugin/auto-config";
|
|
3
|
-
var RESOLVED_VIRTUAL_MODULE_ID = "\x00" + VIRTUAL_MODULE_ID;
|
|
4
|
-
function squooshKit(options = {}) {
|
|
5
|
-
const { autoConfigure = true } = options;
|
|
6
|
-
const entryPoints = new Set;
|
|
7
|
-
let hasInjected = false;
|
|
8
|
-
return {
|
|
9
|
-
name: "@squoosh-kit/vite-plugin",
|
|
10
|
-
enforce: "pre",
|
|
11
|
-
buildStart() {
|
|
12
|
-
entryPoints.clear();
|
|
13
|
-
hasInjected = false;
|
|
14
|
-
},
|
|
15
|
-
configResolved(config) {
|
|
16
|
-
const input = config.build.rollupOptions.input;
|
|
17
|
-
if (input) {
|
|
18
|
-
if (typeof input === "string") {
|
|
19
|
-
entryPoints.add(input);
|
|
20
|
-
} else if (Array.isArray(input)) {
|
|
21
|
-
input.forEach((entry) => {
|
|
22
|
-
if (typeof entry === "string") {
|
|
23
|
-
entryPoints.add(entry);
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
} else {
|
|
27
|
-
Object.values(input).forEach((entry) => {
|
|
28
|
-
if (typeof entry === "string") {
|
|
29
|
-
entryPoints.add(entry);
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
if (config.root) {
|
|
35
|
-
const commonEntries = [
|
|
36
|
-
"src/main.ts",
|
|
37
|
-
"src/main.tsx",
|
|
38
|
-
"src/main.js",
|
|
39
|
-
"src/main.jsx",
|
|
40
|
-
"src/index.ts",
|
|
41
|
-
"src/index.tsx",
|
|
42
|
-
"src/index.js",
|
|
43
|
-
"src/index.jsx",
|
|
44
|
-
"src/app.ts",
|
|
45
|
-
"src/app.tsx"
|
|
46
|
-
];
|
|
47
|
-
commonEntries.forEach((entry) => {
|
|
48
|
-
entryPoints.add(entry);
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
},
|
|
52
|
-
resolveId(id) {
|
|
53
|
-
if (id === VIRTUAL_MODULE_ID) {
|
|
54
|
-
return RESOLVED_VIRTUAL_MODULE_ID;
|
|
55
|
-
}
|
|
56
|
-
return null;
|
|
57
|
-
},
|
|
58
|
-
async load(id) {
|
|
59
|
-
if (id === RESOLVED_VIRTUAL_MODULE_ID) {
|
|
60
|
-
return generateAutoConfigCode();
|
|
61
|
-
}
|
|
62
|
-
return null;
|
|
63
|
-
},
|
|
64
|
-
transform(code, id) {
|
|
65
|
-
if (!autoConfigure || hasInjected) {
|
|
66
|
-
return null;
|
|
67
|
-
}
|
|
68
|
-
const isEntryPoint = entryPoints.has(id) || entryPoints.has(id.replace(/\\/g, "/")) || /[/\\]src[/\\](main|index|app)\.(ts|tsx|js|jsx)$/.test(id) || id.includes("main.") && (code.includes("ReactDOM") || code.includes("createRoot"));
|
|
69
|
-
if (isEntryPoint) {
|
|
70
|
-
hasInjected = true;
|
|
71
|
-
const configCode = generateAutoConfigCode();
|
|
72
|
-
return {
|
|
73
|
-
code: configCode + `
|
|
74
|
-
|
|
75
|
-
` + code,
|
|
76
|
-
map: null
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
return null;
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
function generateAutoConfigCode() {
|
|
84
|
-
return `// Auto-configured by @squoosh-kit/vite-plugin
|
|
85
|
-
import { configureResizeWorker } from '@squoosh-kit/resize';
|
|
86
|
-
import { configureWebpWorker } from '@squoosh-kit/webp';
|
|
87
|
-
|
|
88
|
-
// Resolve worker paths - Vite will transform these correctly during build
|
|
89
|
-
const resizeWorkerUrl = new URL('@squoosh-kit/resize/dist/resize.worker.browser.mjs', import.meta.url).href;
|
|
90
|
-
const webpWorkerUrl = new URL('@squoosh-kit/webp/dist/webp.worker.browser.mjs', import.meta.url).href;
|
|
91
|
-
|
|
92
|
-
// Configure workers immediately
|
|
93
|
-
configureResizeWorker(resizeWorkerUrl);
|
|
94
|
-
configureWebpWorker(webpWorkerUrl);`;
|
|
95
|
-
}
|
|
96
|
-
export {
|
|
97
|
-
squooshKit as default
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
//# debugId=478346E675C2FED564756E2164756E21
|
|
101
|
-
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../src/index.ts"],
|
|
4
|
-
"sourcesContent": [
|
|
5
|
-
"/**\n * @squoosh-kit/vite-plugin\n *\n * Vite plugin that automatically configures squoosh-kit worker URLs.\n * This plugin resolves worker paths using Vite's module resolution,\n * ensuring workers load correctly in both development and production builds.\n */\n\nimport type { Plugin, ResolvedConfig } from 'vite';\n\nexport interface SquooshKitPluginOptions {\n /**\n * Whether to auto-configure workers automatically.\n * If true, configuration code is injected into entry points.\n * If false, you must manually import '@squoosh-kit/vite-plugin/auto-config' in your app.\n * @default true\n */\n autoConfigure?: boolean;\n}\n\nconst VIRTUAL_MODULE_ID = 'virtual:@squoosh-kit/vite-plugin/auto-config';\nconst RESOLVED_VIRTUAL_MODULE_ID = '\\0' + VIRTUAL_MODULE_ID;\n\n/**\n * Vite plugin for automatic squoosh-kit worker configuration.\n *\n * This plugin automatically resolves and configures worker URLs for\n * @squoosh-kit/resize and @squoosh-kit/webp packages, ensuring they\n * work correctly in both development and production builds.\n *\n * @example\n * ```ts\n * // vite.config.ts\n * import { defineConfig } from 'vite';\n * import squooshKit from '@squoosh-kit/vite-plugin';\n *\n * export default defineConfig({\n * plugins: [squooshKit()],\n * });\n * ```\n */\nexport default function squooshKit(\n options: SquooshKitPluginOptions = {}\n): Plugin {\n const { autoConfigure = true } = options;\n\n // Per-plugin-instance state\n const entryPoints = new Set<string>();\n let hasInjected = false;\n\n return {\n name: '@squoosh-kit/vite-plugin',\n enforce: 'pre',\n\n buildStart() {\n // Reset state for each build\n entryPoints.clear();\n hasInjected = false;\n },\n\n configResolved(config: ResolvedConfig) {\n // Track entry points from Vite config\n const input = config.build.rollupOptions.input;\n if (input) {\n if (typeof input === 'string') {\n entryPoints.add(input);\n } else if (Array.isArray(input)) {\n input.forEach((entry) => {\n if (typeof entry === 'string') {\n entryPoints.add(entry);\n }\n });\n } else {\n // Object format\n Object.values(input).forEach((entry) => {\n if (typeof entry === 'string') {\n entryPoints.add(entry);\n }\n });\n }\n }\n\n // Also track default entry points\n if (config.root) {\n const commonEntries = [\n 'src/main.ts',\n 'src/main.tsx',\n 'src/main.js',\n 'src/main.jsx',\n 'src/index.ts',\n 'src/index.tsx',\n 'src/index.js',\n 'src/index.jsx',\n 'src/app.ts',\n 'src/app.tsx',\n ];\n commonEntries.forEach((entry) => {\n entryPoints.add(entry);\n });\n }\n },\n\n resolveId(id: string) {\n if (id === VIRTUAL_MODULE_ID) {\n return RESOLVED_VIRTUAL_MODULE_ID;\n }\n return null;\n },\n\n async load(id: string) {\n if (id === RESOLVED_VIRTUAL_MODULE_ID) {\n return generateAutoConfigCode();\n }\n return null;\n },\n\n transform(code: string, id: string) {\n if (!autoConfigure || hasInjected) {\n return null;\n }\n\n // Check if this is an entry point\n const isEntryPoint =\n entryPoints.has(id) ||\n entryPoints.has(id.replace(/\\\\/g, '/')) ||\n /[/\\\\]src[/\\\\](main|index|app)\\.(ts|tsx|js|jsx)$/.test(id) ||\n (id.includes('main.') &&\n (code.includes('ReactDOM') || code.includes('createRoot')));\n\n // Only inject once at the first entry point\n if (isEntryPoint) {\n hasInjected = true;\n const configCode = generateAutoConfigCode();\n return {\n code: configCode + '\\n\\n' + code,\n map: null,\n };\n }\n\n return null;\n },\n };\n}\n\n/**\n * Generates the auto-configuration code that resolves and configures worker URLs.\n */\nfunction generateAutoConfigCode(): string {\n return `// Auto-configured by @squoosh-kit/vite-plugin\nimport { configureResizeWorker } from '@squoosh-kit/resize';\nimport { configureWebpWorker } from '@squoosh-kit/webp';\n\n// Resolve worker paths - Vite will transform these correctly during build\nconst resizeWorkerUrl = new URL('@squoosh-kit/resize/dist/resize.worker.browser.mjs', import.meta.url).href;\nconst webpWorkerUrl = new URL('@squoosh-kit/webp/dist/webp.worker.browser.mjs', import.meta.url).href;\n\n// Configure workers immediately\nconfigureResizeWorker(resizeWorkerUrl);\nconfigureWebpWorker(webpWorkerUrl);`;\n}\n"
|
|
6
|
-
],
|
|
7
|
-
"mappings": ";AAoBA,IAAM,oBAAoB;AAC1B,IAAM,6BAA6B,SAAO;AAoB1C,SAAwB,UAAU,CAChC,UAAmC,CAAC,GAC5B;AAAA,EACR,QAAQ,gBAAgB,SAAS;AAAA,EAGjC,MAAM,cAAc,IAAI;AAAA,EACxB,IAAI,cAAc;AAAA,EAElB,OAAO;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA,IAET,UAAU,GAAG;AAAA,MAEX,YAAY,MAAM;AAAA,MAClB,cAAc;AAAA;AAAA,IAGhB,cAAc,CAAC,QAAwB;AAAA,MAErC,MAAM,QAAQ,OAAO,MAAM,cAAc;AAAA,MACzC,IAAI,OAAO;AAAA,QACT,IAAI,OAAO,UAAU,UAAU;AAAA,UAC7B,YAAY,IAAI,KAAK;AAAA,QACvB,EAAO,SAAI,MAAM,QAAQ,KAAK,GAAG;AAAA,UAC/B,MAAM,QAAQ,CAAC,UAAU;AAAA,YACvB,IAAI,OAAO,UAAU,UAAU;AAAA,cAC7B,YAAY,IAAI,KAAK;AAAA,YACvB;AAAA,WACD;AAAA,QACH,EAAO;AAAA,UAEL,OAAO,OAAO,KAAK,EAAE,QAAQ,CAAC,UAAU;AAAA,YACtC,IAAI,OAAO,UAAU,UAAU;AAAA,cAC7B,YAAY,IAAI,KAAK;AAAA,YACvB;AAAA,WACD;AAAA;AAAA,MAEL;AAAA,MAGA,IAAI,OAAO,MAAM;AAAA,QACf,MAAM,gBAAgB;AAAA,UACpB;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,QACF;AAAA,QACA,cAAc,QAAQ,CAAC,UAAU;AAAA,UAC/B,YAAY,IAAI,KAAK;AAAA,SACtB;AAAA,MACH;AAAA;AAAA,IAGF,SAAS,CAAC,IAAY;AAAA,MACpB,IAAI,OAAO,mBAAmB;AAAA,QAC5B,OAAO;AAAA,MACT;AAAA,MACA,OAAO;AAAA;AAAA,SAGH,KAAI,CAAC,IAAY;AAAA,MACrB,IAAI,OAAO,4BAA4B;AAAA,QACrC,OAAO,uBAAuB;AAAA,MAChC;AAAA,MACA,OAAO;AAAA;AAAA,IAGT,SAAS,CAAC,MAAc,IAAY;AAAA,MAClC,IAAI,CAAC,iBAAiB,aAAa;AAAA,QACjC,OAAO;AAAA,MACT;AAAA,MAGA,MAAM,eACJ,YAAY,IAAI,EAAE,KAClB,YAAY,IAAI,GAAG,QAAQ,OAAO,GAAG,CAAC,KACtC,kDAAkD,KAAK,EAAE,KACxD,GAAG,SAAS,OAAO,MACjB,KAAK,SAAS,UAAU,KAAK,KAAK,SAAS,YAAY;AAAA,MAG5D,IAAI,cAAc;AAAA,QAChB,cAAc;AAAA,QACd,MAAM,aAAa,uBAAuB;AAAA,QAC1C,OAAO;AAAA,UACL,MAAM,aAAa;AAAA;AAAA,IAAS;AAAA,UAC5B,KAAK;AAAA,QACP;AAAA,MACF;AAAA,MAEA,OAAO;AAAA;AAAA,EAEX;AAAA;AAMF,SAAS,sBAAsB,GAAW;AAAA,EACxC,OAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;",
|
|
8
|
-
"debugId": "478346E675C2FED564756E2164756E21",
|
|
9
|
-
"names": []
|
|
10
|
-
}
|