Skip to content

Commit 517b97f

Browse files
feat: searched params attached to workers are now preserved (#22280)
Co-authored-by: 翠 <[email protected]>
1 parent 1b5cfe3 commit 517b97f

5 files changed

Lines changed: 71 additions & 13 deletions

File tree

‎packages/vite/src/node/__tests__/plugins/worker.spec.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
11
import { resolve } from 'node:path'
2-
import { expect, test } from 'vitest'
2+
import { describe, expect, test } from 'vitest'
33
import type { OutputChunk, RolldownOutput } from 'rolldown'
44
import { build } from '../../build'
5+
import { splitWorkerRequest } from '../../plugins/worker'
56

67
const fixturesDir = resolve(import.meta.dirname, 'fixtures')
78

9+
describe('splitWorkerRequest', () => {
10+
for (const [id, postfix] of [
11+
['/worker.js', ''],
12+
['/worker.js#hash', ''],
13+
['/worker.js?worker', ''],
14+
['/worker.js?sharedworker', ''],
15+
['/worker.js?inline', ''],
16+
['/worker.js?url', ''],
17+
['/worker.js?worker&url', ''],
18+
['/worker.js?worker&inline', ''],
19+
['/worker.js?worker&foo&url&bar', '?foo&bar'],
20+
['/worker.js?worker&foo&inline', '?foo'],
21+
['/worker.js?foo&bar', '?foo&bar'],
22+
['/worker.js?foo=foo&worker&bar=bar', '?foo=foo&bar=bar'],
23+
['/worker.js?foo&bar&worker', '?foo&bar'],
24+
]) {
25+
test(`splits ${id}`, () => {
26+
expect(splitWorkerRequest(id)).toEqual({ file: '/worker.js', postfix })
27+
})
28+
}
29+
})
30+
831
test('?worker&url should produce the same hash in client and SSR builds', async () => {
932
const root = resolve(fixturesDir, 'worker-url')
1033

‎packages/vite/src/node/__tests__/plugins/workerImportMetaUrl.spec.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,14 @@ new Worker(
253253
)"
254254
`)
255255
})
256+
257+
test('preserves custom search params in worker URL', async () => {
258+
expect(
259+
await transform(
260+
'new Worker(new URL("./worker.js?foo=bar&baz=qux", import.meta.url), { type: "module" })',
261+
),
262+
).toMatchInlineSnapshot(
263+
'"new Worker(new URL(/* @vite-ignore */ "/worker.js?worker_file&type=module&foo=bar&baz=qux", \'\' + import.meta.url), { type: "module" })"',
264+
)
265+
})
256266
})

‎packages/vite/src/node/plugins/worker.ts‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
injectQuery,
2020
normalizePath,
2121
prettifyUrl,
22+
trailingSeparatorRE,
2223
urlRE,
2324
} from '../utils'
2425
import {
@@ -29,7 +30,7 @@ import {
2930
onRollupLog,
3031
toOutputFilePathInJS,
3132
} from '../build'
32-
import { cleanUrl } from '../../shared/utils'
33+
import { cleanUrl, splitFileAndPostfix } from '../../shared/utils'
3334
import type { Logger } from '../logger'
3435
import { fileToUrl, toOutputFilePathInJSForBundledDev } from './asset'
3536

@@ -238,6 +239,24 @@ export const workerOrSharedWorkerRE: RegExp =
238239
/(?:\?|&)(worker|sharedworker)(?:&|$)/
239240
const workerFileRE = /(?:\?|&)worker_file&type=(\w+)(?:&|$)/
240241
const inlineRE = /[?&]inline\b/
242+
const workerQueriesRE =
243+
/(\?|&)(?:(?:worker|sharedworker|inline|url)=?(?:&|$))+/g
244+
245+
export function splitWorkerRequest(id: string): {
246+
file: string
247+
postfix: string
248+
} {
249+
const { file, postfix } = splitFileAndPostfix(id)
250+
if (!postfix || postfix[0] !== '?') {
251+
return { file, postfix: '' }
252+
}
253+
return {
254+
file,
255+
postfix: postfix
256+
.replace(workerQueriesRE, '$1')
257+
.replace(trailingSeparatorRE, ''),
258+
}
259+
}
241260

242261
export const WORKER_FILE_ID = 'worker_file'
243262
const workerOutputCaches = new WeakMap<ResolvedConfig, WorkerOutputCache>()
@@ -631,8 +650,12 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
631650
}
632651
}
633652
} else {
634-
let url = await fileToUrl(this, cleanUrl(id))
635-
url = injectQuery(url, `${WORKER_FILE_ID}&type=${workerType}`)
653+
const { file, postfix } = splitWorkerRequest(id)
654+
let url = await fileToUrl(this, file)
655+
url = injectQuery(
656+
`${url}${postfix}`,
657+
`${WORKER_FILE_ID}&type=${workerType}`,
658+
)
636659
urlCode = JSON.stringify(url)
637660
}
638661

‎packages/vite/src/node/plugins/workerImportMetaUrl.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { Plugin } from '../plugin'
99
import { evalValue, injectQuery, transformStableResult } from '../utils'
1010
import { createBackCompatIdResolver } from '../idResolver'
1111
import type { ResolveIdFn } from '../idResolver'
12-
import { cleanUrl, slash } from '../../shared/utils'
12+
import { cleanUrl, slash, splitFileAndPostfix } from '../../shared/utils'
1313
import type { WorkerType } from './worker'
1414
import {
1515
WORKER_FILE_ID,
@@ -234,21 +234,23 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
234234
s ||= new MagicString(code)
235235
const workerType = await getWorkerType(code, cleanString, endIndex)
236236
const url = rawUrl.slice(1, -1)
237+
const { file: urlWithoutPostfix, postfix } = splitFileAndPostfix(url)
238+
const queryPostfix = postfix[0] === '?' ? postfix : ''
237239
let file: string | undefined
238-
if (url[0] === '.') {
239-
file = path.resolve(path.dirname(id), url)
240+
if (urlWithoutPostfix[0] === '.') {
241+
file = path.resolve(path.dirname(id), urlWithoutPostfix)
240242
file = slash(tryFsResolve(file, fsResolveOptions) ?? file)
241243
} else {
242244
workerResolver ??= createBackCompatIdResolver(config, {
243245
extensions: [],
244246
tryIndex: false,
245247
preferRelative: true,
246248
})
247-
file = await workerResolver(this.environment, url, id)
249+
file = await workerResolver(this.environment, urlWithoutPostfix, id)
248250
file ??=
249-
url[0] === '/'
250-
? slash(path.join(config.publicDir, url))
251-
: slash(path.resolve(path.dirname(id), url))
251+
urlWithoutPostfix[0] === '/'
252+
? slash(path.join(config.publicDir, urlWithoutPostfix))
253+
: slash(path.resolve(path.dirname(id), urlWithoutPostfix))
252254
}
253255

254256
if (
@@ -282,7 +284,7 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
282284
} else {
283285
builtUrl = await fileToUrl(this, cleanUrl(file))
284286
builtUrl = injectQuery(
285-
builtUrl,
287+
`${builtUrl}${queryPostfix}`,
286288
`${WORKER_FILE_ID}&type=${workerType}`,
287289
)
288290
}

‎packages/vite/src/node/utils.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ const internalPrefixes = [
314314
ENV_PUBLIC_PATH,
315315
]
316316
const InternalPrefixRE = new RegExp(`^(?:${internalPrefixes.join('|')})`)
317-
const trailingSeparatorRE = /[?&]$/
317+
export const trailingSeparatorRE: RegExp = /[?&]$/
318318
export const isImportRequest = (url: string): boolean => importQueryRE.test(url)
319319
export const isInternalRequest = (url: string): boolean =>
320320
InternalPrefixRE.test(url)

0 commit comments

Comments
 (0)