Skip to content

Commit e8d6a4d

Browse files
authored
fix(build): keep hash placeholders as-is in resolveFileUrl hook (#23422)
1 parent 07d6570 commit e8d6a4d

11 files changed

Lines changed: 82 additions & 1 deletion

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ import { getImportMapFilename } from './html'
4848
// referenceId is base64url but replaces - with $
4949
export const assetUrlRE: RegExp = /__VITE_ASSET__([\w$]+)__/g
5050

51+
const encodedHashPlaceholderRE = /!~%7B([\w$]{1,17})%7D~/g
52+
function unescapeHashPlaceholders(uri: string): string {
53+
return uri.replace(encodedHashPlaceholderRE, '!~{$1}~')
54+
}
55+
5156
interface FileUrlMetadata {
5257
asFileUrl: boolean
5358
}
@@ -329,7 +334,9 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
329334
toRelativeRuntime,
330335
)
331336
return typeof replacement === 'string'
332-
? JSON.stringify(encodeURIPath(replacement))
337+
? JSON.stringify(
338+
unescapeHashPlaceholders(encodeURIPath(replacement)),
339+
)
333340
: replacement.runtime
334341
},
335342

‎playground/assets/__tests__/assets.spec.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,20 @@ describe('asset imports from js', () => {
218218
})
219219
})
220220

221+
describe.runIf(isBuild)('file URL emitted by an external plugin', () => {
222+
test('replaces the preliminary hash', async () => {
223+
expect(await page.textContent('.emitted-worker-url')).toMatch(
224+
/^\/foo\/bar\/assets\/emitted-worker-[-\w]{8}\.js$/,
225+
)
226+
})
227+
228+
test('loads the emitted chunk', async () => {
229+
await expect
230+
.poll(() => page.textContent('.emitted-worker-result'))
231+
.toBe('worker loaded')
232+
})
233+
})
234+
221235
describe('css url() references', () => {
222236
test('fonts', async () => {
223237
expect(

‎playground/assets/__tests__/encoded-base/assets-encoded-base.spec.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ describe('asset imports from js', () => {
7373
})
7474
})
7575

76+
test.runIf(isBuild)('loads the emitted worker', async () => {
77+
await expect
78+
.poll(() => page.textContent('.emitted-worker-result'))
79+
.toBe('worker loaded')
80+
})
81+
7682
describe('css url() references', () => {
7783
test('fonts', async () => {
7884
expect(

‎playground/assets/__tests__/relative-base/assets-relative-base.spec.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ describe('asset imports from js', () => {
7777
})
7878
})
7979

80+
test.runIf(isBuild)('loads the emitted worker', async () => {
81+
await expect
82+
.poll(() => page.textContent('.emitted-worker-result'))
83+
.toBe('worker loaded')
84+
})
85+
8086
describe('css url() references', () => {
8187
test('fonts', async () => {
8288
expect(

‎playground/assets/__tests__/runtime-base/assets-runtime-base.spec.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ describe('asset imports from js', () => {
7070
})
7171
})
7272

73+
test.runIf(isBuild)('loads the emitted worker', async () => {
74+
await expect
75+
.poll(() => page.textContent('.emitted-worker-result'))
76+
.toBe('worker loaded')
77+
})
78+
7379
describe('css url() references', () => {
7480
test('fonts', async () => {
7581
expect(

‎playground/assets/__tests__/url-base/assets-url-base.spec.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ describe('asset imports from js', () => {
7575
})
7676
})
7777

78+
test.runIf(isBuild)('loads the emitted worker', async () => {
79+
await expect
80+
.poll(() => page.textContent('.emitted-worker-result'))
81+
.toBe('worker loaded')
82+
})
83+
7884
describe('css url() references', () => {
7985
test('fonts', async () => {
8086
expect(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
self.postMessage('worker loaded')

‎playground/assets/asset/main.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1+
import emittedWorkerUrl from 'virtual:emitted-worker-url'
2+
13
function text(el, text) {
24
document.querySelector(el).textContent = text
35
}
46
text('.relative-js', 'hello')
7+
8+
if (emittedWorkerUrl) {
9+
text('.emitted-worker-url', emittedWorkerUrl)
10+
const worker = new Worker(emittedWorkerUrl, { type: 'module' })
11+
worker.addEventListener('message', ({ data }) => {
12+
text('.emitted-worker-result', data)
13+
})
14+
}

‎playground/assets/index.html‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ <h2>Asset Imports from JS</h2>
5454
</li>
5555
</ul>
5656

57+
<h2>File URL emitted by an external plugin</h2>
58+
<code class="emitted-worker-url">pending</code>
59+
<code class="emitted-worker-result">pending</code>
60+
5761
<h2>CSS url references</h2>
5862
<div>Font should be loaded (all text should be italic)</div>
5963
<div class="css-url-absolute">

‎playground/assets/vite.config-runtime-base.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default defineConfig({
2424
},
2525
},
2626
plugins: [
27+
...baseConfig.plugins,
2728
{
2829
name: 'dynamic-base-assets-globals',
2930
transformIndexHtml(_, ctx) {

0 commit comments

Comments
 (0)