Skip to content

Commit b95fa2a

Browse files
authored
fix(dev): allow aliases starting with // (#20760)
1 parent 74dca99 commit b95fa2a

File tree

5 files changed

+15
-2
lines changed

5 files changed

+15
-2
lines changed

packages/vite/src/node/plugins/importAnalysis.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,9 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
512512
if (specifier !== undefined) {
513513
// skip external / data uri
514514
if (
515-
(isExternalUrl(specifier) && !specifier.startsWith('file://')) ||
516-
isDataUrl(specifier)
515+
((isExternalUrl(specifier) && !specifier.startsWith('file://')) ||
516+
isDataUrl(specifier)) &&
517+
!matchAlias(specifier)
517518
) {
518519
return
519520
}

playground/alias/__tests__/alias.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ test('aliased module', async () => {
4646
)
4747
})
4848

49+
test('url conflict alias', async () => {
50+
expect(await page.textContent('.url-conflict')).toMatch(
51+
'[success] url conflict alias',
52+
)
53+
})
54+
4955
test('custom resolver', async () => {
5056
expect(await page.textContent('.custom-resolver')).toMatch(
5157
'[success] alias to custom-resolver path',
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const msg = `[success] url conflict alias`

playground/alias/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ <h1>Alias</h1>
55
<p class="regex"></p>
66
<p class="dep"></p>
77
<p class="from-script-src"></p>
8+
<p class="url-conflict"></p>
89
<p class="aliased-module"></p>
910
<p class="custom-resolver"></p>
1011

@@ -17,6 +18,7 @@ <h1>Alias</h1>
1718
import { msg as regexMsg } from 'regex/test'
1819
import { msg as depMsg } from 'dep'
1920
import { msg as moduleMsg } from 'aliased-module/index.js'
21+
import { msg as urlConflictMsg } from '//url_conflict.js'
2022
import { msg as customResolverMsg } from 'custom-resolver'
2123

2224
function text(el, text) {
@@ -28,6 +30,7 @@ <h1>Alias</h1>
2830
text('.regex', regexMsg + ' via regex')
2931
text('.dep', depMsg)
3032
text('.aliased-module', moduleMsg)
33+
text('.url-conflict', urlConflictMsg)
3134
text('.custom-resolver', customResolverMsg)
3235

3336
import { createApp } from 'vue'

playground/alias/vite.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export default defineConfig({
1515
replacement: `${path.resolve(__dirname, 'dir')}/$1`,
1616
},
1717
{ find: '/@', replacement: path.resolve(__dirname, 'dir') },
18+
// aliasing a pattern that conflicts with url schemes
19+
{ find: /^\/\//, replacement: path.join(__dirname, 'dir/') },
1820
// aliasing an optimized dep
1921
{ find: 'vue', replacement: 'vue/dist/vue.esm-bundler.js' },
2022
// aliasing an optimized dep to absolute URL

0 commit comments

Comments
 (0)