Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(worker): force rollup to build workerImportMetaUrl under watch mode #14712

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
},

shouldTransformCachedModule({ id }) {
if (isBuild && isWorkerQueryId(id) && config.build.watch) {
if (isBuild && config.build.watch && isWorkerQueryId(id)) {
return true
}
},
Expand Down
24 changes: 18 additions & 6 deletions packages/vite/src/node/plugins/workerImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ function getWorkerType(raw: string, clean: string, i: number): WorkerType {
return 'classic'
}

function isIncludeWorkerImportMetaUrl(code: string): boolean {
if (
(code.includes('new Worker') || code.includes('new SharedWorker')) &&
code.includes('new URL') &&
code.includes(`import.meta.url`)
) {
return true
}
return false
}

export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
const isBuild = config.command === 'build'
let workerResolver: ResolveFn
Expand All @@ -113,14 +124,15 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
return {
name: 'vite:worker-import-meta-url',

shouldTransformCachedModule({ code }) {
if (isBuild && config.build.watch && isIncludeWorkerImportMetaUrl(code)) {
bluwy marked this conversation as resolved.
Show resolved Hide resolved
return true
}
},

async transform(code, id, options) {
const ssr = options?.ssr === true
if (
!options?.ssr &&
(code.includes('new Worker') || code.includes('new SharedWorker')) &&
code.includes('new URL') &&
code.includes(`import.meta.url`)
) {
if (!options?.ssr && isIncludeWorkerImportMetaUrl(code)) {
const query = parseRequest(id)
let s: MagicString | undefined
const cleanString = stripLiteral(code)
Expand Down