vite-userscript-plugin 2.4.0 โ†’ 2.5.0

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.
Files changed (3) hide show
  1. package/README.md +10 -0
  2. package/dist/index.js +29 -0
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -15,6 +15,7 @@
15
15
  - ๐Ÿ“ Only used `@grant`s in the production build
16
16
  - ๐Ÿ“ฆ Built-in types for Tampermonkey, Greasemonkey and Violentmonkey
17
17
  - ๐Ÿ“„ Virtual module with script metadata
18
+ - ๐Ÿงต Support Web Workers
18
19
 
19
20
  ## Getting started
20
21
 
@@ -107,6 +108,14 @@ import './style.css'
107
108
  > [!NOTE]
108
109
  > Do not put userscript assets in `public/` โ€” those URLs hit the host site and 404. Import the file so Vite inlines it.
109
110
 
111
+ ## Web Workers
112
+
113
+ HMR serve rewrites `import Worker from './w?worker'` (and `?worker&inline`) to a data-URI module worker that imports Vite's `?worker_file`. The host page cannot load `/src/w.ts?worker_file` from localhost.
114
+
115
+ `vite build` and `server.file` keep Vite's worker emit. A plain `?worker` is a separate file the match site cannot serve โ€” use `?worker&inline`. `?worker&url` and `?sharedworker` stay as Vite emits them.
116
+
117
+ See [examples/web-worker](./examples/web-worker).
118
+
110
119
  ## HTML pages
111
120
 
112
121
  `index.html` is a normal Vite app next to the userscript. `vite` serves it at `/`. `vite build` writes it to `dist/` beside `{fileName}.user.js`.
@@ -184,6 +193,7 @@ In serve mode the header lists every grant. In production the plugin scans the b
184
193
  | [sourcemap](./examples/sourcemap) | Inline map, HTML page, virtual module. |
185
194
  | [serve-file](./examples/serve-file) | `server.file`, install `.user.js` or the proxy from the printed URLs. |
186
195
  | [external-cdn](./examples/external-cdn) | `@types/jquery` only; runtime `$` from a CDN `@require`. |
196
+ | [web-worker](./examples/web-worker) | `?worker&inline`, data-URI bridge in HMR. |
187
197
 
188
198
  ## FAQ
189
199
 
package/dist/index.js CHANGED
@@ -881,6 +881,7 @@ function shouldShimModule(id) {
881
881
  if (/\.(?:css|scss|sass|less|styl|stylus|pcss)(?:$|\?)/i.test(cleanId)) return false;
882
882
  if (/[?&](?:vue|svelte)&type=style/.test(cleanId)) return false;
883
883
  if (/[?&](?:raw|url)(?:&|$)/.test(cleanId)) return false;
884
+ if (/[?&](?:worker_file|sharedworker|worker)(?:&|$)/.test(cleanId)) return false;
884
885
  if (/\.(?:m|c)?[jt]sx?(?:$|\?)/.test(cleanId)) return true;
885
886
  if (/[?&]vue&type=script/.test(cleanId) || cleanId.endsWith(".vue")) return true;
886
887
  if (/[?&]svelte&type=script/.test(cleanId) || cleanId.endsWith(".svelte")) return true;
@@ -1202,6 +1203,26 @@ function createDebouncedSingleFlight(task, delay, onError) {
1202
1203
  };
1203
1204
  }
1204
1205
  //#endregion
1206
+ //#region src/serve/web-worker.ts
1207
+ function isWebWorkerRequest(id) {
1208
+ const queryIndex = id.indexOf("?");
1209
+ if (queryIndex === -1) return false;
1210
+ const query = new URLSearchParams(id.slice(queryIndex + 1));
1211
+ return query.has("worker") && !query.has("url");
1212
+ }
1213
+ const webWorkerWrapper = `const dataUri = \`data:text/javascript;charset=utf-8,\${encodeURIComponent(
1214
+ \`import \${JSON.stringify(
1215
+ new URL('?worker_file&type=module', import.meta['url']).href,
1216
+ )};\`,
1217
+ )}\`;
1218
+
1219
+ export default function WorkerWrapper(options) {
1220
+ return new Worker(dataUri, {
1221
+ type: 'module',
1222
+ name: options?.name,
1223
+ });
1224
+ }`;
1225
+ //#endregion
1205
1226
  //#region src/plugin.ts
1206
1227
  function absolutizeEntries(config, root) {
1207
1228
  return { scripts: config.scripts.map((script) => ({
@@ -1345,6 +1366,14 @@ function UserscriptPlugin(config) {
1345
1366
  return renderExternalModule(external.global);
1346
1367
  }
1347
1368
  },
1369
+ {
1370
+ name: `${PLUGIN_NAME}:web-worker`,
1371
+ enforce: "pre",
1372
+ apply: "serve",
1373
+ load(id) {
1374
+ if (isWebWorkerRequest(id)) return webWorkerWrapper;
1375
+ }
1376
+ },
1348
1377
  {
1349
1378
  name: `${PLUGIN_NAME}:serve`,
1350
1379
  apply: "serve",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-userscript-plugin",
3
3
  "type": "module",
4
- "version": "2.4.0",
4
+ "version": "2.5.0",
5
5
  "author": {
6
6
  "name": "Vitalij Ryndin",
7
7
  "url": "https://github.com/crashmax-dev"