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): worker import.meta.url should not depends on document in iife mode #12629

Merged
merged 4 commits into from
Apr 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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: 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
14 changes: 14 additions & 0 deletions packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,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`
Copy link
Member

@sapphi-red sapphi-red Apr 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return `new URL('${chunkId}', self.location.href).href`
return 'self.location.href'

I just noticed that we should use self.location.href here. location.href returns the location of the worker and not the document.
https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/location

new URL('${chunkId}', self.location.href).href is be like http://localhost:4173/assets/assets/worker.js which is not correct, because chunkId is like /assets/worker.js and self.location.href is like http://localhost:4173/assets/worker.js.
image

}

return null
},
}
}

export function webWorkerPlugin(config: ResolvedConfig): Plugin {
const isBuild = config.command === 'build'
let server: ViteDevServer
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
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 string',
async () => page.textContent('.worker-import-meta-url'),
/A\sstring.*\/iife\/.+url-worker\.js/,
true,
)
await untilUpdated(
() => page.textContent('.worker-import-meta-url-resolve'),
'A string',
/A\sstring.*\/iife\/.+url-worker\.js/,
true,
)
await untilUpdated(
() => page.textContent('.shared-worker-import-meta-url'),
'A string',
true,
)
})

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')