MCPcopy
hub / github.com/vitejs/vite / handler

Function handler

packages/vite/src/node/plugins/worker.ts:345–387  ·  view source on GitHub ↗
(code, id)

Source from the content-addressed store, hash-verified

343 },
344 order: 'post',
345 async handler(code, id) {
346 // import.meta is unavailable in the IIFE worker, so we need to replace it
347 if (this.environment.config.worker.format === 'iife') {
348 await init
349
350 let imports: readonly ImportSpecifier[]
351 try {
352 imports = parse(code)[0]
353 } catch {
354 // ignore if parse fails
355 return
356 }
357
358 let injectedImportMeta = false
359 let s: MagicString | undefined
360 for (const { s: start, e: end, d: dynamicIndex } of imports) {
361 // is import.meta
362 if (dynamicIndex === -2) {
363 const prop = code.slice(end, end + 4)
364 if (prop === '.url') {
365 s ||= new MagicString(code)
366 s.overwrite(start, end + 4, 'self.location.href')
367 } else {
368 s ||= new MagicString(code)
369 if (!injectedImportMeta) {
370 s.prepend(
371 'const _vite_importMeta = { url: self.location.href };\n',
372 )
373 injectedImportMeta = true
374 }
375 s.overwrite(start, end, '_vite_importMeta')
376 }
377 }
378 }
379
380 if (!s) return
381
382 return {
383 code: s.toString(),
384 map: s.generateMap({ hires: 'boundary', source: id }),
385 }
386 }
387 },
388 },
389 }
390}

Callers

nothing calls this directly

Calls 8

cleanUrlFunction · 0.90
fileToUrlFunction · 0.90
injectQueryFunction · 0.90
bundleWorkerEntryFunction · 0.85
workerFileToUrlFunction · 0.85
addWatchFileMethod · 0.45
getModuleByIdMethod · 0.45

Tested by

no test coverage detected