Skip to content

Commit 1b6ed05

Browse files
authored
fix(server): preserve $ patterns in static alias replacements (#23703)
1 parent 6f2730e commit 1b6ed05

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎packages/vite/src/node/server/middlewares/__tests__/static.spec.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,30 @@ describe('static file aliases', () => {
4444
replacement: '/root/aliased/',
4545
expected: '/root/aliased/file.txt',
4646
},
47+
'replacement containing $$': {
48+
pathname: '/images/file.txt',
49+
find: '/images',
50+
replacement: '/root/$$assets',
51+
expected: '/root/$$assets/file.txt',
52+
},
53+
'replacement containing $&': {
54+
pathname: '/images/file.txt',
55+
find: '/images',
56+
replacement: '/root/$&',
57+
expected: '/root/$&/file.txt',
58+
},
59+
"replacement containing $'": {
60+
pathname: '/images/file.txt',
61+
find: '/images',
62+
replacement: "/root/$'",
63+
expected: "/root/$'/file.txt",
64+
},
65+
'regex alias capture group': {
66+
pathname: '/regex/file.txt',
67+
find: /^\/regex\/(.*)$/,
68+
replacement: '/root/aliased/$1',
69+
expected: '/root/aliased/file.txt',
70+
},
4771
}
4872

4973
for (const [

‎packages/vite/src/node/server/middlewares/static.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ export function applyStaticAlias(
202202
? pathname === find || pathname.startsWith(`${find}/`)
203203
: find.test(pathname)
204204
if (matches) {
205+
if (typeof find === 'string') {
206+
return replacement + pathname.slice(find.length)
207+
}
205208
return pathname.replace(find, replacement)
206209
}
207210
}

0 commit comments

Comments
 (0)