Skip to content

Commit 30a7acc

Browse files
authored
chore: remove unneeded worker context param (#8268)
1 parent ec52baa commit 30a7acc

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

packages/vite/src/node/plugins/worker.ts

+21-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'path'
22
import MagicString from 'magic-string'
3-
import type { EmittedAsset, OutputChunk, TransformPluginContext } from 'rollup'
3+
import type { EmittedAsset, OutputChunk } from 'rollup'
44
import type { ResolvedConfig } from '../config'
55
import type { Plugin } from '../plugin'
66
import type { ViteDevServer } from '../server'
@@ -43,7 +43,6 @@ function saveEmitWorkerAsset(
4343
}
4444

4545
export async function bundleWorkerEntry(
46-
ctx: TransformPluginContext,
4746
config: ResolvedConfig,
4847
id: string,
4948
query: Record<string, string> | null
@@ -102,11 +101,10 @@ export async function bundleWorkerEntry(
102101
} finally {
103102
await bundle.close()
104103
}
105-
return emitSourcemapForWorkerEntry(ctx, config, query, chunk)
104+
return emitSourcemapForWorkerEntry(config, query, chunk)
106105
}
107106

108107
function emitSourcemapForWorkerEntry(
109-
ctx: TransformPluginContext,
110108
config: ResolvedConfig,
111109
query: Record<string, string> | null,
112110
chunk: OutputChunk
@@ -166,15 +164,14 @@ function encodeWorkerAssetFileName(
166164
}
167165

168166
export async function workerFileToUrl(
169-
ctx: TransformPluginContext,
170167
config: ResolvedConfig,
171168
id: string,
172169
query: Record<string, string> | null
173170
): Promise<string> {
174171
const workerMap = workerCache.get(config.mainConfig || config)!
175172
let fileName = workerMap.bundle.get(id)
176173
if (!fileName) {
177-
const outputChunk = await bundleWorkerEntry(ctx, config, id, query)
174+
const outputChunk = await bundleWorkerEntry(config, id, query)
178175
fileName = outputChunk.fileName
179176
saveEmitWorkerAsset(config, {
180177
fileName,
@@ -226,8 +223,10 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
226223

227224
async transform(raw, id) {
228225
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
231230
let injectEnv = ''
232231

233232
if (workerType === 'classic') {
@@ -259,11 +258,18 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
259258

260259
// stringified url or `new URL(...)`
261260
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"}'
262270
if (isBuild) {
263271
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)
267273
// inline as blob data url
268274
return {
269275
code: `const encodedJs = "${Buffer.from(chunk.code).toString(
@@ -273,7 +279,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
273279
export default function WorkerWrapper() {
274280
const objURL = blob && (window.URL || window.webkitURL).createObjectURL(blob);
275281
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});
277283
} finally {
278284
objURL && (window.URL || window.webkitURL).revokeObjectURL(objURL);
279285
}
@@ -283,11 +289,12 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
283289
map: { mappings: '' }
284290
}
285291
} else {
286-
url = await workerFileToUrl(this, config, id, query)
292+
url = await workerFileToUrl(config, id, query)
287293
}
288294
} else {
289295
url = await fileToUrl(cleanUrl(id), config, this)
290296
url = injectQuery(url, WORKER_FILE_ID)
297+
url = injectQuery(url, `type=${workerType}`)
291298
}
292299

293300
if (query.url != null) {
@@ -297,15 +304,11 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
297304
}
298305
}
299306

300-
const workerConstructor =
301-
query.sharedworker != null ? 'SharedWorker' : 'Worker'
302-
const workerOptions = { type: 'module' }
303-
304307
return {
305308
code: `export default function WorkerWrapper() {
306309
return new ${workerConstructor}(${JSON.stringify(
307310
url
308-
)}, ${JSON.stringify(workerOptions)})
311+
)}${workerOptions})
309312
}`,
310313
map: { mappings: '' } // Empty sourcemap to suppress Rollup warning
311314
}

packages/vite/src/node/plugins/workerImportMetaUrl.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
115115
)
116116
let url: string
117117
if (isBuild) {
118-
url = await workerFileToUrl(this, config, file, query)
118+
url = await workerFileToUrl(config, file, query)
119119
} else {
120120
url = await fileToUrl(cleanUrl(file), config, this)
121121
url = injectQuery(url, WORKER_FILE_ID)

playground/worker/worker/main-url.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function text(el, text) {
44
document.querySelector(el).textContent = text
55
}
66

7-
const worker = new Worker(workerUrl, { type: 'classic' })
7+
const worker = new Worker(workerUrl, { type: 'module' })
88

99
worker.addEventListener('message', (ev) => {
1010
text('.simple-worker-url', JSON.stringify(ev.data))

0 commit comments

Comments
 (0)