Skip to content

Commit

Permalink
refactor: add webWorkerPostPlugin to resolveBuildPlugins
Browse files Browse the repository at this point in the history
  • Loading branch information
sun0day committed Apr 2, 2023
1 parent 8a76fec commit d67aac4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
2 changes: 2 additions & 0 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import { ESBUILD_MODULES_TARGET, VERSION } from './constants'
import { resolveChokidarOptions } from './watch'
import { completeSystemWrapPlugin } from './plugins/completeSystemWrap'
import { mergeConfig } from './publicUtils'
import { webWorkerPostPlugin } from './plugins/worker'

export interface BuildOptions {
/**
Expand Down Expand Up @@ -445,6 +446,7 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
: [rollupOptionsPlugins],
)
).filter(Boolean) as Plugin[]),
...(config.isWorker ? [webWorkerPostPlugin()] : []),
],
post: [
buildImportAnalysisPlugin(config),
Expand Down
28 changes: 15 additions & 13 deletions packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,6 @@ export async function bundleWorkerEntry(
return promise
}

const workerPostPlugin: Plugin = {
name: 'vite:worker-post',
resolveImportMeta(property, { chunkId, format }) {
// document is undefined in the worker, so we need to avoid it in iife
if (property === 'url' && format === 'iife') {
return `new URL('${chunkId}', self.location.href).href`
}

return null
},
}

async function serialBundleWorkerEntry(
config: ResolvedConfig,
id: string,
Expand All @@ -87,7 +75,7 @@ async function serialBundleWorkerEntry(
const bundle = await rollup({
...rollupOptions,
input: cleanUrl(id),
plugins: plugins.concat(workerPostPlugin),
plugins,
onwarn(warning, warn) {
onRollupWarning(warning, warn, config)
},
Expand Down Expand Up @@ -197,6 +185,20 @@ export async function workerFileToUrl(
return encodeWorkerAssetFileName(fileName, workerMap)
}

export function webWorkerPostPlugin(): Plugin {
return {
name: 'vite:worker-post',
resolveImportMeta(property, { chunkId, format }) {
// document is undefined in the worker, so we need to avoid it in iife
if (property === 'url' && format === 'iife') {
return `new URL('${chunkId}', self.location.href).href`
}

return null
},
}
}

export function webWorkerPlugin(config: ResolvedConfig): Plugin {
const isBuild = config.command === 'build'
let server: ViteDevServer
Expand Down
9 changes: 6 additions & 3 deletions playground/worker/__tests__/iife/iife-worker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,19 @@ describe.runIf(isBuild)('build', () => {

test('module worker', async () => {
await untilUpdated(
() => page.textContent('.worker-import-meta-url'),
/A\sstring.*\/iife\/.+\/url-worker\.js\?type=ignore&worker_file/,
async () => page.textContent('.worker-import-meta-url'),
/A\sstring.*\/iife\/.+url-worker\.js/,
true,
)
await untilUpdated(
() => page.textContent('.worker-import-meta-url-resolve'),
/A\sstring.*\/iife\/.+\/url-worker\.js\?type=ignore&worker_file/,
/A\sstring.*\/iife\/.+url-worker\.js/,
true,
)
await untilUpdated(
() => page.textContent('.shared-worker-import-meta-url'),
'A string',
true,
)
})

Expand Down

0 comments on commit d67aac4

Please sign in to comment.