From 30a7acc3d54305906306f124ea1a577bd8175b1e Mon Sep 17 00:00:00 2001 From: yoho Date: Mon, 23 May 2022 21:20:34 +0800 Subject: [PATCH] chore: remove unneeded worker context param (#8268) --- packages/vite/src/node/plugins/worker.ts | 39 ++++++++++--------- .../src/node/plugins/workerImportMetaUrl.ts | 2 +- playground/worker/worker/main-url.js | 2 +- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 99eef51a4a2bd7..5fa6311e820100 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -1,6 +1,6 @@ import path from 'path' import MagicString from 'magic-string' -import type { EmittedAsset, OutputChunk, TransformPluginContext } from 'rollup' +import type { EmittedAsset, OutputChunk } from 'rollup' import type { ResolvedConfig } from '../config' import type { Plugin } from '../plugin' import type { ViteDevServer } from '../server' @@ -43,7 +43,6 @@ function saveEmitWorkerAsset( } export async function bundleWorkerEntry( - ctx: TransformPluginContext, config: ResolvedConfig, id: string, query: Record | null @@ -102,11 +101,10 @@ export async function bundleWorkerEntry( } finally { await bundle.close() } - return emitSourcemapForWorkerEntry(ctx, config, query, chunk) + return emitSourcemapForWorkerEntry(config, query, chunk) } function emitSourcemapForWorkerEntry( - ctx: TransformPluginContext, config: ResolvedConfig, query: Record | null, chunk: OutputChunk @@ -166,7 +164,6 @@ function encodeWorkerAssetFileName( } export async function workerFileToUrl( - ctx: TransformPluginContext, config: ResolvedConfig, id: string, query: Record | null @@ -174,7 +171,7 @@ export async function workerFileToUrl( const workerMap = workerCache.get(config.mainConfig || config)! let fileName = workerMap.bundle.get(id) if (!fileName) { - const outputChunk = await bundleWorkerEntry(ctx, config, id, query) + const outputChunk = await bundleWorkerEntry(config, id, query) fileName = outputChunk.fileName saveEmitWorkerAsset(config, { fileName, @@ -226,8 +223,10 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { async transform(raw, id) { const query = parseRequest(id) - if (query && query[WORKER_FILE_ID] != null && query['type'] != null) { - const workerType = query['type'] as WorkerType + if (query && query[WORKER_FILE_ID] != null) { + // if import worker by worker constructor will had query.type + // other type will be import worker by esm + const workerType = query['type']! as WorkerType let injectEnv = '' if (workerType === 'classic') { @@ -259,11 +258,18 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { // stringified url or `new URL(...)` let url: string + const { format } = config.worker + const workerConstructor = + query.sharedworker != null ? 'SharedWorker' : 'Worker' + const workerType = isBuild + ? format === 'es' + ? 'module' + : 'classic' + : 'module' + const workerOptions = workerType === 'classic' ? '' : ',{type: "module"}' if (isBuild) { if (query.inline != null) { - const chunk = await bundleWorkerEntry(this, config, id, query) - const { format } = config.worker - const workerOptions = format === 'es' ? '{type: "module"}' : '{}' + const chunk = await bundleWorkerEntry(config, id, query) // inline as blob data url return { code: `const encodedJs = "${Buffer.from(chunk.code).toString( @@ -273,7 +279,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { export default function WorkerWrapper() { const objURL = blob && (window.URL || window.webkitURL).createObjectURL(blob); try { - return objURL ? new Worker(objURL, ${workerOptions}) : new Worker("data:application/javascript;base64," + encodedJs, {type: "module"}); + return objURL ? new ${workerConstructor}(objURL${workerOptions}) : new ${workerConstructor}("data:application/javascript;base64," + encodedJs${workerOptions}); } finally { objURL && (window.URL || window.webkitURL).revokeObjectURL(objURL); } @@ -283,11 +289,12 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { map: { mappings: '' } } } else { - url = await workerFileToUrl(this, config, id, query) + url = await workerFileToUrl(config, id, query) } } else { url = await fileToUrl(cleanUrl(id), config, this) url = injectQuery(url, WORKER_FILE_ID) + url = injectQuery(url, `type=${workerType}`) } if (query.url != null) { @@ -297,15 +304,11 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { } } - const workerConstructor = - query.sharedworker != null ? 'SharedWorker' : 'Worker' - const workerOptions = { type: 'module' } - return { code: `export default function WorkerWrapper() { return new ${workerConstructor}(${JSON.stringify( url - )}, ${JSON.stringify(workerOptions)}) + )}${workerOptions}) }`, map: { mappings: '' } // Empty sourcemap to suppress Rollup warning } diff --git a/packages/vite/src/node/plugins/workerImportMetaUrl.ts b/packages/vite/src/node/plugins/workerImportMetaUrl.ts index 3e7426f52cda24..3b8300296f0520 100644 --- a/packages/vite/src/node/plugins/workerImportMetaUrl.ts +++ b/packages/vite/src/node/plugins/workerImportMetaUrl.ts @@ -115,7 +115,7 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin { ) let url: string if (isBuild) { - url = await workerFileToUrl(this, config, file, query) + url = await workerFileToUrl(config, file, query) } else { url = await fileToUrl(cleanUrl(file), config, this) url = injectQuery(url, WORKER_FILE_ID) diff --git a/playground/worker/worker/main-url.js b/playground/worker/worker/main-url.js index ba395926009754..d3a80ea6c80dd4 100644 --- a/playground/worker/worker/main-url.js +++ b/playground/worker/worker/main-url.js @@ -4,7 +4,7 @@ function text(el, text) { document.querySelector(el).textContent = text } -const worker = new Worker(workerUrl, { type: 'classic' }) +const worker = new Worker(workerUrl, { type: 'module' }) worker.addEventListener('message', (ev) => { text('.simple-worker-url', JSON.stringify(ev.data))