@@ -262,60 +262,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
262262 } ,
263263
264264 load : {
265- handler ( id ) {
266- if ( isBuild && workerOrSharedWorkerRE . test ( id ) ) {
267- return ''
268- }
269- } ,
270- } ,
271-
272- shouldTransformCachedModule ( { id } ) {
273- if ( isBuild && config . build . watch && workerOrSharedWorkerRE . test ( id ) ) {
274- return true
275- }
276- } ,
277-
278- transform : {
279- async handler ( raw , id ) {
280- const workerFileMatch = workerFileRE . exec ( id )
281- if ( workerFileMatch ) {
282- // if import worker by worker constructor will have query.type
283- // other type will be import worker by esm
284- const workerType = workerFileMatch [ 1 ] as WorkerType
285- let injectEnv = ''
286-
287- const scriptPath = JSON . stringify (
288- path . posix . join ( config . base , ENV_PUBLIC_PATH ) ,
289- )
290-
291- if ( workerType === 'classic' ) {
292- injectEnv = `importScripts(${ scriptPath } )\n`
293- } else if ( workerType === 'module' ) {
294- injectEnv = `import ${ scriptPath } \n`
295- } else if ( workerType === 'ignore' ) {
296- if ( isBuild ) {
297- injectEnv = ''
298- } else {
299- // dynamic worker type we can't know how import the env
300- // so we copy /@vite /env code of server transform result into file header
301- const environment = this . environment
302- const moduleGraph =
303- environment . mode === 'dev' ? environment . moduleGraph : undefined
304- const module = moduleGraph ?. getModuleById ( ENV_ENTRY )
305- injectEnv = module ?. transformResult ?. code || ''
306- }
307- }
308- if ( injectEnv ) {
309- const s = new MagicString ( raw )
310- s . prepend ( injectEnv + ';\n' )
311- return {
312- code : s . toString ( ) ,
313- map : s . generateMap ( { hires : 'boundary' } ) ,
314- }
315- }
316- return
317- }
318-
265+ async handler ( id ) {
319266 const workerMatch = workerOrSharedWorkerRE . exec ( id )
320267 if ( ! workerMatch ) return
321268
@@ -417,6 +364,54 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
417364 } ,
418365 } ,
419366
367+ shouldTransformCachedModule ( { id } ) {
368+ if ( isBuild && config . build . watch && workerOrSharedWorkerRE . test ( id ) ) {
369+ return true
370+ }
371+ } ,
372+
373+ transform : {
374+ async handler ( raw , id ) {
375+ const workerFileMatch = workerFileRE . exec ( id )
376+ if ( workerFileMatch ) {
377+ // if import worker by worker constructor will have query.type
378+ // other type will be import worker by esm
379+ const workerType = workerFileMatch [ 1 ] as WorkerType
380+ let injectEnv = ''
381+
382+ const scriptPath = JSON . stringify (
383+ path . posix . join ( config . base , ENV_PUBLIC_PATH ) ,
384+ )
385+
386+ if ( workerType === 'classic' ) {
387+ injectEnv = `importScripts(${ scriptPath } )\n`
388+ } else if ( workerType === 'module' ) {
389+ injectEnv = `import ${ scriptPath } \n`
390+ } else if ( workerType === 'ignore' ) {
391+ if ( isBuild ) {
392+ injectEnv = ''
393+ } else {
394+ // dynamic worker type we can't know how import the env
395+ // so we copy /@vite /env code of server transform result into file header
396+ const environment = this . environment
397+ const moduleGraph =
398+ environment . mode === 'dev' ? environment . moduleGraph : undefined
399+ const module = moduleGraph ?. getModuleById ( ENV_ENTRY )
400+ injectEnv = module ?. transformResult ?. code || ''
401+ }
402+ }
403+ if ( injectEnv ) {
404+ const s = new MagicString ( raw )
405+ s . prepend ( injectEnv + ';\n' )
406+ return {
407+ code : s . toString ( ) ,
408+ map : s . generateMap ( { hires : 'boundary' } ) ,
409+ }
410+ }
411+ }
412+ } ,
413+ } ,
414+
420415 renderChunk ( code , chunk , outputOptions ) {
421416 let s : MagicString
422417 const result = ( ) => {
0 commit comments