Skip to content

Commit

Permalink
fix(worker): worker import.meta.url should not depends on document
Browse files Browse the repository at this point in the history
…in iife mode
  • Loading branch information
sun0day committed Mar 28, 2023
1 parent 0f9ad68 commit 69f048b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
14 changes: 13 additions & 1 deletion packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ 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 iife generate it
if (property === 'url' && format === 'iife') {
return `new URL('${chunkId}', self.location.href).href`
}

return null
},
}

async function serialBundleWorkerEntry(
config: ResolvedConfig,
id: string,
Expand All @@ -75,7 +87,7 @@ async function serialBundleWorkerEntry(
const bundle = await rollup({
...rollupOptions,
input: cleanUrl(id),
plugins,
plugins: plugins.concat(workerPostPlugin),
onwarn(warning, warn) {
onRollupWarning(warning, warn, config)
},
Expand Down
9 changes: 7 additions & 2 deletions playground/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,19 @@ export function readManifest(base = ''): Manifest {
*/
export async function untilUpdated(
poll: () => string | Promise<string>,
expected: string,
expected: string | RegExp,
runInBuild = false,
): Promise<void> {
if (isBuild && !runInBuild) return
const maxTries = process.env.CI ? 200 : 50
for (let tries = 0; tries < maxTries; tries++) {
const actual = (await poll()) ?? ''
if (actual.indexOf(expected) > -1 || tries === maxTries - 1) {
if (
(typeof expected === 'string'
? actual.indexOf(expected) > -1
: actual.match(expected)) ||
tries === maxTries - 1
) {
expect(actual).toMatch(expected)
break
} else {
Expand Down
4 changes: 2 additions & 2 deletions playground/worker/__tests__/iife/iife-worker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ describe.runIf(isBuild)('build', () => {
test('module worker', async () => {
await untilUpdated(
() => page.textContent('.worker-import-meta-url'),
'A string',
/A\sstring.*\/iife\/.+\/url-worker\.js\?type=ignore&worker_file/,
)
await untilUpdated(
() => page.textContent('.worker-import-meta-url-resolve'),
'A string',
/A\sstring.*\/iife\/.+\/url-worker\.js\?type=ignore&worker_file/,
)
await untilUpdated(
() => page.textContent('.shared-worker-import-meta-url'),
Expand Down
9 changes: 8 additions & 1 deletion playground/worker/url-worker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
self.postMessage('A string' + import.meta.env.BASE_URL + self.location.url)
self.postMessage(
[
'A string',
import.meta.env.BASE_URL,
self.location.url,
import.meta.url,
].join(' '),
)

// for sourcemap
console.log('url-worker.js')

0 comments on commit 69f048b

Please sign in to comment.