diff --git a/packages/vite/src/node/plugins/assetImportMetaUrl.ts b/packages/vite/src/node/plugins/assetImportMetaUrl.ts index 1ff1ee631f3254..d7f8125c6c6622 100644 --- a/packages/vite/src/node/plugins/assetImportMetaUrl.ts +++ b/packages/vite/src/node/plugins/assetImportMetaUrl.ts @@ -46,13 +46,15 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin { if (templateLiteral.expressions.length) { const pattern = buildGlobPattern(templateLiteral) // Note: native import.meta.url is not supported in the baseline - // target so we use window.location here - + // target so we use the global location here. It can be + // window.location or self.location in case it is used in a Web Worker. + // @see https://developer.mozilla.org/en-US/docs/Web/API/Window/self s.overwrite( index, index + exp.length, `new URL(import.meta.globEagerDefault(${JSON.stringify( pattern - )})[${rawUrl}], window.location)` + )})[${rawUrl}], self.location)` ) continue } @@ -64,7 +66,7 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin { s.overwrite( index, index + exp.length, - `new URL(${JSON.stringify(builtUrl)}, window.location)` + `new URL(${JSON.stringify(builtUrl)}, self.location)` ) } if (s) {