diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index 64a8fdce9b2826..b5d1b9ee1b4bf7 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -512,8 +512,9 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { if (specifier !== undefined) { // skip external / data uri if ( - (isExternalUrl(specifier) && !specifier.startsWith('file://')) || - isDataUrl(specifier) + ((isExternalUrl(specifier) && !specifier.startsWith('file://')) || + isDataUrl(specifier)) && + !matchAlias(specifier) ) { return } diff --git a/playground/alias/__tests__/alias.spec.ts b/playground/alias/__tests__/alias.spec.ts index 7c77e57a7497df..f8bbea6474b379 100644 --- a/playground/alias/__tests__/alias.spec.ts +++ b/playground/alias/__tests__/alias.spec.ts @@ -46,6 +46,12 @@ test('aliased module', async () => { ) }) +test('url conflict alias', async () => { + expect(await page.textContent('.url-conflict')).toMatch( + '[success] url conflict alias', + ) +}) + test('custom resolver', async () => { expect(await page.textContent('.custom-resolver')).toMatch( '[success] alias to custom-resolver path', diff --git a/playground/alias/dir/url_conflict.js b/playground/alias/dir/url_conflict.js new file mode 100644 index 00000000000000..a46c1beba908f5 --- /dev/null +++ b/playground/alias/dir/url_conflict.js @@ -0,0 +1 @@ +export const msg = `[success] url conflict alias` diff --git a/playground/alias/index.html b/playground/alias/index.html index f292ea5c8ad07a..58d51f680751b0 100644 --- a/playground/alias/index.html +++ b/playground/alias/index.html @@ -5,6 +5,7 @@

Alias

+

@@ -17,6 +18,7 @@

Alias

import { msg as regexMsg } from 'regex/test' import { msg as depMsg } from 'dep' import { msg as moduleMsg } from 'aliased-module/index.js' + import { msg as urlConflictMsg } from '//url_conflict.js' import { msg as customResolverMsg } from 'custom-resolver' function text(el, text) { @@ -28,6 +30,7 @@

Alias

text('.regex', regexMsg + ' via regex') text('.dep', depMsg) text('.aliased-module', moduleMsg) + text('.url-conflict', urlConflictMsg) text('.custom-resolver', customResolverMsg) import { createApp } from 'vue' diff --git a/playground/alias/vite.config.js b/playground/alias/vite.config.js index 6d7cbc32413433..3585320b88f141 100644 --- a/playground/alias/vite.config.js +++ b/playground/alias/vite.config.js @@ -15,6 +15,8 @@ export default defineConfig({ replacement: `${path.resolve(__dirname, 'dir')}/$1`, }, { find: '/@', replacement: path.resolve(__dirname, 'dir') }, + // aliasing a pattern that conflicts with url schemes + { find: /^\/\//, replacement: path.join(__dirname, 'dir/') }, // aliasing an optimized dep { find: 'vue', replacement: 'vue/dist/vue.esm-bundler.js' }, // aliasing an optimized dep to absolute URL