Skip to content

Commit

Permalink
chore: remove unneeded worker context param (#8268)
Browse files Browse the repository at this point in the history
  • Loading branch information
poyoho authored May 23, 2022
1 parent ec52baa commit 30a7acc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
39 changes: 21 additions & 18 deletions packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -43,7 +43,6 @@ function saveEmitWorkerAsset(
}

export async function bundleWorkerEntry(
ctx: TransformPluginContext,
config: ResolvedConfig,
id: string,
query: Record<string, string> | null
Expand Down Expand Up @@ -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<string, string> | null,
chunk: OutputChunk
Expand Down Expand Up @@ -166,15 +164,14 @@ function encodeWorkerAssetFileName(
}

export async function workerFileToUrl(
ctx: TransformPluginContext,
config: ResolvedConfig,
id: string,
query: Record<string, string> | null
): Promise<string> {
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,
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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(
Expand All @@ -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);
}
Expand All @@ -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) {
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/workerImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion playground/worker/worker/main-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 30a7acc

Please sign in to comment.