@@ -245,10 +245,11 @@ export function webWorkerPostPlugin(): Plugin {
245
245
name : 'vite:worker-post' ,
246
246
transform : {
247
247
filter : {
248
- code : 'import.meta.url ' ,
248
+ code : 'import.meta' ,
249
249
} ,
250
+ order : 'post' ,
250
251
async handler ( code , id ) {
251
- // document is undefined in the worker, so we need to avoid import.meta.url in iife
252
+ // import.meta is unavailable in the IIFE worker, so we need to replace it
252
253
if ( this . environment . config . worker . format === 'iife' ) {
253
254
await init
254
255
@@ -260,6 +261,7 @@ export function webWorkerPostPlugin(): Plugin {
260
261
return
261
262
}
262
263
264
+ let injectedImportMeta = false
263
265
let s : MagicString | undefined
264
266
for ( const { s : start , e : end , d : dynamicIndex } of imports ) {
265
267
// is import.meta
@@ -268,6 +270,15 @@ export function webWorkerPostPlugin(): Plugin {
268
270
if ( prop === '.url' ) {
269
271
s ||= new MagicString ( code )
270
272
s . overwrite ( start , end + 4 , 'self.location.href' )
273
+ } else {
274
+ s ||= new MagicString ( code )
275
+ if ( ! injectedImportMeta ) {
276
+ s . prepend (
277
+ 'const _vite_importMeta = { url: self.location.href };\n' ,
278
+ )
279
+ injectedImportMeta = true
280
+ }
281
+ s . overwrite ( start , end , '_vite_importMeta' )
271
282
}
272
283
}
273
284
}
0 commit comments