diff --git a/packages/playground/worker/__tests__/worker.spec.ts b/packages/playground/worker/__tests__/worker.spec.ts index 46b136edd66d2a..f10e6208fb53c3 100644 --- a/packages/playground/worker/__tests__/worker.spec.ts +++ b/packages/playground/worker/__tests__/worker.spec.ts @@ -73,3 +73,11 @@ if (isBuild) { expect(content).toMatch(`window.Blob`) }) } + +test('?worker&url import', async () => { + if (isBuild) { + expect(await page.textContent('.worker-url')).toMatch('/assets/my-worker.') + } else { + expect(await page.textContent('.worker-url')).toMatch('/my-worker.ts?worker_file') + } +}) \ No newline at end of file diff --git a/packages/playground/worker/index.html b/packages/playground/worker/index.html index 1ce429eece42cc..1a6b16a0fcccbf 100644 --- a/packages/playground/worker/index.html +++ b/packages/playground/worker/index.html @@ -19,12 +19,18 @@ 0 +

+ ?worker&url import + +

+ + \ No newline at end of file diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 799233088d6b13..1dc7f6392cfbad 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -103,6 +103,10 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { url = injectQuery(url, WorkerFileId) } + if (query.url != null) { + return `export default ${JSON.stringify(url)}` + } + const workerConstructor = query.sharedworker != null ? 'SharedWorker' : 'Worker' const workerOptions = { type: 'module' }