diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index a656b6d07e6916..24cebe7a1b6471 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -165,10 +165,21 @@ export async function workerFileToUrl( export function webWorkerPostPlugin(): Plugin { return { name: 'vite:worker-post', - resolveImportMeta(property, { chunkId, format }) { + resolveImportMeta(property, { format }) { // document is undefined in the worker, so we need to avoid it in iife - if (property === 'url' && format === 'iife') { - return 'self.location.href' + if (format === 'iife') { + // compiling import.meta + if (!property) { + // rollup only supports `url` property. we only support `url` property as well. + // https://github.com/rollup/rollup/blob/62b648e1cc6a1f00260bb85aa2050097bb4afd2b/src/ast/nodes/MetaProperty.ts#L164-L173 + return `{ + url: self.location.href + }` + } + // compiling import.meta.url + if (property === 'url') { + return 'self.location.href' + } } return null diff --git a/playground/worker/__tests__/iife/iife-worker.spec.ts b/playground/worker/__tests__/iife/iife-worker.spec.ts index 7fe242a7f33a70..01c9eb25edc798 100644 --- a/playground/worker/__tests__/iife/iife-worker.spec.ts +++ b/playground/worker/__tests__/iife/iife-worker.spec.ts @@ -114,17 +114,17 @@ describe.runIf(isBuild)('build', () => { test('module worker', async () => { await untilUpdated( async () => page.textContent('.worker-import-meta-url'), - /A\sstring.*\/iife\/.+url-worker\.js/, + /A\sstring.*\/iife\/.+url-worker\.js.+url-worker\.js/, true, ) await untilUpdated( () => page.textContent('.worker-import-meta-url-resolve'), - /A\sstring.*\/iife\/.+url-worker\.js/, + /A\sstring.*\/iife\/.+url-worker\.js.+url-worker\.js/, true, ) await untilUpdated( () => page.textContent('.worker-import-meta-url-without-extension'), - 'A string', + /A\sstring.*\/iife\/.+url-worker\.js.+url-worker\.js/, true, ) await untilUpdated( diff --git a/playground/worker/url-worker.js b/playground/worker/url-worker.js index bede21cc42cc49..a329ffdbf840e4 100644 --- a/playground/worker/url-worker.js +++ b/playground/worker/url-worker.js @@ -3,6 +3,7 @@ self.postMessage( 'A string', import.meta.env.BASE_URL, self.location.url, + import.meta && import.meta.url, import.meta.url, ].join(' '), )