Skip to content

Commit

Permalink
fix(worker): force rollup to build workerImportMetaUrl under watch mo…
Browse files Browse the repository at this point in the history
…de (#14712)
  • Loading branch information
ihch committed Oct 30, 2023
1 parent 7234021 commit 8db40ee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
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)) {
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

0 comments on commit 8db40ee

Please sign in to comment.