@@ -5,7 +5,7 @@ import type { ResolvedConfig } from '../config'
5
5
import type { Plugin } from '../plugin'
6
6
import type { ViteDevServer } from '../server'
7
7
import { ENV_ENTRY , ENV_PUBLIC_PATH } from '../constants'
8
- import { getHash , injectQuery , urlRE } from '../utils'
8
+ import { getHash , injectQuery , prettifyUrl , urlRE } from '../utils'
9
9
import {
10
10
createToImportMetaURLBasedRelativeRuntime ,
11
11
onRollupWarning ,
@@ -50,13 +50,22 @@ async function bundleWorkerEntry(
50
50
config : ResolvedConfig ,
51
51
id : string ,
52
52
) : 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
+
53
62
// bundle the file as entry to support imports
54
63
const { rollup } = await import ( 'rollup' )
55
64
const { plugins, rollupOptions, format } = config . worker
56
65
const bundle = await rollup ( {
57
66
...rollupOptions ,
58
- input : cleanUrl ( id ) ,
59
- plugins : await plugins ( ) ,
67
+ input,
68
+ plugins : await plugins ( newBundleChain ) ,
60
69
onwarn ( warning , warn ) {
61
70
onRollupWarning ( warning , warn , config )
62
71
} ,
@@ -262,8 +271,6 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
262
271
const workerMatch = workerOrSharedWorkerRE . exec ( id )
263
272
if ( ! workerMatch ) return
264
273
265
- // stringified url or `new URL(...)`
266
- let url : string
267
274
const { format } = config . worker
268
275
const workerConstructor =
269
276
workerMatch [ 1 ] === 'sharedworker' ? 'SharedWorker' : 'Worker'
@@ -277,8 +284,11 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
277
284
name: options?.name
278
285
}`
279
286
287
+ let urlCode : string
280
288
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 ) ) {
282
292
const chunk = await bundleWorkerEntry ( config , id )
283
293
const encodedJs = `const encodedJs = "${ Buffer . from (
284
294
chunk . code ,
@@ -335,24 +345,25 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
335
345
map : { mappings : '' } ,
336
346
}
337
347
} else {
338
- url = await workerFileToUrl ( config , id )
348
+ urlCode = JSON . stringify ( await workerFileToUrl ( config , id ) )
339
349
}
340
350
} else {
341
- url = await fileToUrl ( cleanUrl ( id ) , config , this )
351
+ let url = await fileToUrl ( cleanUrl ( id ) , config , this )
342
352
url = injectQuery ( url , `${ WORKER_FILE_ID } &type=${ workerType } ` )
353
+ urlCode = JSON . stringify ( url )
343
354
}
344
355
345
356
if ( urlRE . test ( id ) ) {
346
357
return {
347
- code : `export default ${ JSON . stringify ( url ) } ` ,
358
+ code : `export default ${ urlCode } ` ,
348
359
map : { mappings : '' } , // Empty sourcemap to suppress Rollup warning
349
360
}
350
361
}
351
362
352
363
return {
353
364
code : `export default function WorkerWrapper(options) {
354
365
return new ${ workerConstructor } (
355
- ${ JSON . stringify ( url ) } ,
366
+ ${ urlCode } ,
356
367
${ workerTypeOption }
357
368
);
358
369
}` ,
0 commit comments