mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 22:59:10 +00:00
fix(worker): worker import.meta.url should not depends on document in iife mode (#12629)
Co-authored-by: 翠 / green <green@sapphi.red>
This commit is contained in:
parent
81e44dda57
commit
65f5ed2e35
@ -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 {
|
||||
/**
|
||||
@ -445,6 +446,7 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
|
||||
: [rollupOptionsPlugins],
|
||||
)
|
||||
).filter(Boolean) as Plugin[]),
|
||||
...(config.isWorker ? [webWorkerPostPlugin()] : []),
|
||||
],
|
||||
post: [
|
||||
buildImportAnalysisPlugin(config),
|
||||
|
@ -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 'self.location.href'
|
||||
}
|
||||
|
||||
return null
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export function webWorkerPlugin(config: ResolvedConfig): Plugin {
|
||||
const isBuild = config.command === 'build'
|
||||
let server: ViteDevServer
|
||||
|
@ -157,14 +157,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 {
|
||||
|
@ -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,
|
||||
)
|
||||
})
|
||||
|
||||
|
@ -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')
|
||||
|
Loading…
Reference in New Issue
Block a user