1
1
import path from 'path'
2
2
import MagicString from 'magic-string'
3
- import type { EmittedAsset , OutputChunk , TransformPluginContext } from 'rollup'
3
+ import type { EmittedAsset , OutputChunk } from 'rollup'
4
4
import type { ResolvedConfig } from '../config'
5
5
import type { Plugin } from '../plugin'
6
6
import type { ViteDevServer } from '../server'
@@ -43,7 +43,6 @@ function saveEmitWorkerAsset(
43
43
}
44
44
45
45
export async function bundleWorkerEntry (
46
- ctx : TransformPluginContext ,
47
46
config : ResolvedConfig ,
48
47
id : string ,
49
48
query : Record < string , string > | null
@@ -102,11 +101,10 @@ export async function bundleWorkerEntry(
102
101
} finally {
103
102
await bundle . close ( )
104
103
}
105
- return emitSourcemapForWorkerEntry ( ctx , config , query , chunk )
104
+ return emitSourcemapForWorkerEntry ( config , query , chunk )
106
105
}
107
106
108
107
function emitSourcemapForWorkerEntry (
109
- ctx : TransformPluginContext ,
110
108
config : ResolvedConfig ,
111
109
query : Record < string , string > | null ,
112
110
chunk : OutputChunk
@@ -166,15 +164,14 @@ function encodeWorkerAssetFileName(
166
164
}
167
165
168
166
export async function workerFileToUrl (
169
- ctx : TransformPluginContext ,
170
167
config : ResolvedConfig ,
171
168
id : string ,
172
169
query : Record < string , string > | null
173
170
) : Promise < string > {
174
171
const workerMap = workerCache . get ( config . mainConfig || config ) !
175
172
let fileName = workerMap . bundle . get ( id )
176
173
if ( ! fileName ) {
177
- const outputChunk = await bundleWorkerEntry ( ctx , config , id , query )
174
+ const outputChunk = await bundleWorkerEntry ( config , id , query )
178
175
fileName = outputChunk . fileName
179
176
saveEmitWorkerAsset ( config , {
180
177
fileName,
@@ -226,8 +223,10 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
226
223
227
224
async transform ( raw , id ) {
228
225
const query = parseRequest ( id )
229
- if ( query && query [ WORKER_FILE_ID ] != null && query [ 'type' ] != null ) {
230
- const workerType = query [ 'type' ] as WorkerType
226
+ if ( query && query [ WORKER_FILE_ID ] != null ) {
227
+ // if import worker by worker constructor will had query.type
228
+ // other type will be import worker by esm
229
+ const workerType = query [ 'type' ] ! as WorkerType
231
230
let injectEnv = ''
232
231
233
232
if ( workerType === 'classic' ) {
@@ -259,11 +258,18 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
259
258
260
259
// stringified url or `new URL(...)`
261
260
let url : string
261
+ const { format } = config . worker
262
+ const workerConstructor =
263
+ query . sharedworker != null ? 'SharedWorker' : 'Worker'
264
+ const workerType = isBuild
265
+ ? format === 'es'
266
+ ? 'module'
267
+ : 'classic'
268
+ : 'module'
269
+ const workerOptions = workerType === 'classic' ? '' : ',{type: "module"}'
262
270
if ( isBuild ) {
263
271
if ( query . inline != null ) {
264
- const chunk = await bundleWorkerEntry ( this , config , id , query )
265
- const { format } = config . worker
266
- const workerOptions = format === 'es' ? '{type: "module"}' : '{}'
272
+ const chunk = await bundleWorkerEntry ( config , id , query )
267
273
// inline as blob data url
268
274
return {
269
275
code : `const encodedJs = "${ Buffer . from ( chunk . code ) . toString (
@@ -273,7 +279,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
273
279
export default function WorkerWrapper() {
274
280
const objURL = blob && (window.URL || window.webkitURL).createObjectURL(blob);
275
281
try {
276
- return objURL ? new Worker (objURL, ${ workerOptions } ) : new Worker ("data:application/javascript;base64," + encodedJs, {type: "module" });
282
+ return objURL ? new ${ workerConstructor } (objURL${ workerOptions } ) : new ${ workerConstructor } ("data:application/javascript;base64," + encodedJs${ workerOptions } );
277
283
} finally {
278
284
objURL && (window.URL || window.webkitURL).revokeObjectURL(objURL);
279
285
}
@@ -283,11 +289,12 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
283
289
map : { mappings : '' }
284
290
}
285
291
} else {
286
- url = await workerFileToUrl ( this , config , id , query )
292
+ url = await workerFileToUrl ( config , id , query )
287
293
}
288
294
} else {
289
295
url = await fileToUrl ( cleanUrl ( id ) , config , this )
290
296
url = injectQuery ( url , WORKER_FILE_ID )
297
+ url = injectQuery ( url , `type=${ workerType } ` )
291
298
}
292
299
293
300
if ( query . url != null ) {
@@ -297,15 +304,11 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
297
304
}
298
305
}
299
306
300
- const workerConstructor =
301
- query . sharedworker != null ? 'SharedWorker' : 'Worker'
302
- const workerOptions = { type : 'module' }
303
-
304
307
return {
305
308
code : `export default function WorkerWrapper() {
306
309
return new ${ workerConstructor } (${ JSON . stringify (
307
310
url
308
- ) } , ${ JSON . stringify ( workerOptions ) } )
311
+ ) } ${ workerOptions } )
309
312
}` ,
310
313
map : { mappings : '' } // Empty sourcemap to suppress Rollup warning
311
314
}
0 commit comments