@@ -5,7 +5,7 @@ import type { ResolvedConfig } from '../config'
55import type { Plugin } from '../plugin'
66import type { ViteDevServer } from '../server'
77import { ENV_ENTRY , ENV_PUBLIC_PATH } from '../constants'
8- import { getHash , injectQuery , urlRE } from '../utils'
8+ import { getHash , injectQuery , prettifyUrl , urlRE } from '../utils'
99import {
1010 createToImportMetaURLBasedRelativeRuntime ,
1111 onRollupWarning ,
@@ -50,13 +50,22 @@ async function bundleWorkerEntry(
5050 config : ResolvedConfig ,
5151 id : string ,
5252) : Promise < OutputChunk > {
53+ const input = cleanUrl ( id )
54+ const newBundleChain = [ ...config . bundleChain , input ]
55+ if ( config . bundleChain . includes ( input ) ) {
56+ throw new Error (
57+ 'Circular worker imports detected. Vite does not support it. ' +
58+ `Import chain: ${ newBundleChain . map ( ( id ) => prettifyUrl ( id , config . root ) ) . join ( ' -> ' ) } ` ,
59+ )
60+ }
61+
5362 // bundle the file as entry to support imports
5463 const { rollup } = await import ( 'rollup' )
5564 const { plugins, rollupOptions, format } = config . worker
5665 const bundle = await rollup ( {
5766 ...rollupOptions ,
58- input : cleanUrl ( id ) ,
59- plugins : await plugins ( ) ,
67+ input,
68+ plugins : await plugins ( newBundleChain ) ,
6069 onwarn ( warning , warn ) {
6170 onRollupWarning ( warning , warn , config )
6271 } ,
@@ -262,8 +271,6 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
262271 const workerMatch = workerOrSharedWorkerRE . exec ( id )
263272 if ( ! workerMatch ) return
264273
265- // stringified url or `new URL(...)`
266- let url : string
267274 const { format } = config . worker
268275 const workerConstructor =
269276 workerMatch [ 1 ] === 'sharedworker' ? 'SharedWorker' : 'Worker'
@@ -277,8 +284,11 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
277284 name: options?.name
278285 }`
279286
287+ let urlCode : string
280288 if ( isBuild ) {
281- if ( inlineRE . test ( id ) ) {
289+ if ( isWorker && this . getModuleInfo ( cleanUrl ( id ) ) ?. isEntry ) {
290+ urlCode = 'self.location.href'
291+ } else if ( inlineRE . test ( id ) ) {
282292 const chunk = await bundleWorkerEntry ( config , id )
283293 const encodedJs = `const encodedJs = "${ Buffer . from (
284294 chunk . code ,
@@ -335,24 +345,25 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
335345 map : { mappings : '' } ,
336346 }
337347 } else {
338- url = await workerFileToUrl ( config , id )
348+ urlCode = JSON . stringify ( await workerFileToUrl ( config , id ) )
339349 }
340350 } else {
341- url = await fileToUrl ( cleanUrl ( id ) , config , this )
351+ let url = await fileToUrl ( cleanUrl ( id ) , config , this )
342352 url = injectQuery ( url , `${ WORKER_FILE_ID } &type=${ workerType } ` )
353+ urlCode = JSON . stringify ( url )
343354 }
344355
345356 if ( urlRE . test ( id ) ) {
346357 return {
347- code : `export default ${ JSON . stringify ( url ) } ` ,
358+ code : `export default ${ urlCode } ` ,
348359 map : { mappings : '' } , // Empty sourcemap to suppress Rollup warning
349360 }
350361 }
351362
352363 return {
353364 code : `export default function WorkerWrapper(options) {
354365 return new ${ workerConstructor } (
355- ${ JSON . stringify ( url ) } ,
366+ ${ urlCode } ,
356367 ${ workerTypeOption }
357368 );
358369 }` ,
0 commit comments