(_config: ResolvedConfig)
| 326 | } |
| 327 | |
| 328 | export function webWorkerPostPlugin(_config: ResolvedConfig): Plugin { |
| 329 | return { |
| 330 | name: 'vite:worker-post', |
| 331 | applyToEnvironment(environment) { |
| 332 | if (environment.config.isBundled) { |
| 333 | if (environment.config.worker.format === 'iife') { |
| 334 | return nativeWebWorkerPostPlugin() |
| 335 | } |
| 336 | return false |
| 337 | } |
| 338 | return true |
| 339 | }, |
| 340 | transform: { |
| 341 | filter: { |
| 342 | code: 'import.meta', |
| 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 | } |
no outgoing calls
no test coverage detected