diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index ba68db9fa..db8256ec3 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -68,6 +68,7 @@ import { isCSSRequest, isDirectCSSRequest } from './css' import { browserExternalId } from './resolve' import { serializeDefine } from './define' import { WORKER_FILE_ID } from './worker' +import { getAliasPatternMatcher } from './preAlias' const debug = createDebugger('vite:import-analysis') @@ -172,6 +173,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { const fsUtils = getFsUtils(config) const clientPublicPath = path.posix.join(base, CLIENT_PUBLIC_PATH) const enablePartialAccept = config.experimental?.hmrPartialAccept + const matchAlias = getAliasPatternMatcher(config.resolve.alias) let server: ViteDevServer let _env: string | undefined @@ -487,7 +489,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { return } // skip ssr external - if (ssr) { + if (ssr && !matchAlias(specifier)) { if (shouldExternalizeForSSR(specifier, importer, config)) { return } diff --git a/packages/vite/src/node/plugins/preAlias.ts b/packages/vite/src/node/plugins/preAlias.ts index e88173061..eaefdb7e6 100644 --- a/packages/vite/src/node/plugins/preAlias.ts +++ b/packages/vite/src/node/plugins/preAlias.ts @@ -128,3 +128,11 @@ function getAliasPatterns( } return Object.entries(entries).map(([find]) => find) } + +export function getAliasPatternMatcher( + entries: (AliasOptions | undefined) & Alias[], +): (importee: string) => boolean { + const patterns = getAliasPatterns(entries) + return (importee: string) => + patterns.some((pattern) => matches(pattern, importee)) +} diff --git a/playground/ssr-alias/__tests__/ssr-alias.spec.ts b/playground/ssr-alias/__tests__/ssr-alias.spec.ts new file mode 100644 index 000000000..93001865c --- /dev/null +++ b/playground/ssr-alias/__tests__/ssr-alias.spec.ts @@ -0,0 +1,20 @@ +import { expect, test } from 'vitest' +import { isServe, testDir, viteServer } from '~utils' + +test.runIf(isServe)('dev', async () => { + const mod = await viteServer.ssrLoadModule('/src/main.js') + expect(mod.default).toEqual({ + dep: 'ok', + nonDep: 'ok', + builtin: 'ok', + }) +}) + +test.runIf(!isServe)('build', async () => { + const mod = await import(`${testDir}/dist/main.js`) + expect(mod.default).toEqual({ + dep: 'ok', + nonDep: 'ok', + builtin: 'ok', + }) +}) diff --git a/playground/ssr-alias/alias-original/index.js b/playground/ssr-alias/alias-original/index.js new file mode 100644 index 000000000..cc9a88ac5 --- /dev/null +++ b/playground/ssr-alias/alias-original/index.js @@ -0,0 +1 @@ +export default 'original' diff --git a/playground/ssr-alias/alias-original/package.json b/playground/ssr-alias/alias-original/package.json new file mode 100644 index 000000000..a8a86b500 --- /dev/null +++ b/playground/ssr-alias/alias-original/package.json @@ -0,0 +1,9 @@ +{ + "name": "@vitejs/test-alias-original", + "version": "0.0.0", + "private": true, + "type": "module", + "exports": { + ".": "./index.js" + } +} diff --git a/playground/ssr-alias/package.json b/playground/ssr-alias/package.json new file mode 100644 index 000000000..f765f8ac7 --- /dev/null +++ b/playground/ssr-alias/package.json @@ -0,0 +1,12 @@ +{ + "name": "@vitejs/test-ssr-html", + "private": true, + "version": "0.0.0", + "type": "module", + "scripts": { + "build": "vite build" + }, + "dependencies": { + "@vitejs/test-alias-original": "file:./alias-original" + } +} diff --git a/playground/ssr-alias/src/alias-process.js b/playground/ssr-alias/src/alias-process.js new file mode 100644 index 000000000..9cd9e7dde --- /dev/null +++ b/playground/ssr-alias/src/alias-process.js @@ -0,0 +1,3 @@ +export default { + env: { __TEST_ALIAS__: 'ok' }, +} diff --git a/playground/ssr-alias/src/alias-replaced.js b/playground/ssr-alias/src/alias-replaced.js new file mode 100644 index 000000000..60c71f346 --- /dev/null +++ b/playground/ssr-alias/src/alias-replaced.js @@ -0,0 +1 @@ +export default 'ok' diff --git a/playground/ssr-alias/src/main.js b/playground/ssr-alias/src/main.js new file mode 100644 index 000000000..14ded7d14 --- /dev/null +++ b/playground/ssr-alias/src/main.js @@ -0,0 +1,9 @@ +import process from 'node:process' +import dep from '@vitejs/test-alias-original' +import nonDep from '@vitejs/test-alias-non-dep' + +export default { + dep, + nonDep, + builtin: process.env['__TEST_ALIAS__'], +} diff --git a/playground/ssr-alias/vite.config.js b/playground/ssr-alias/vite.config.js new file mode 100644 index 000000000..deb71aee5 --- /dev/null +++ b/playground/ssr-alias/vite.config.js @@ -0,0 +1,14 @@ +import { defineConfig } from 'vite' + +export default defineConfig({ + build: { + ssr: './src/main.js', + }, + resolve: { + alias: { + '@vitejs/test-alias-original': '/src/alias-replaced.js', + '@vitejs/test-alias-non-dep': '/src/alias-replaced.js', + 'node:process': '/src/alias-process.js', + }, + }, +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4618dc802..e8b218fc5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1263,6 +1263,14 @@ importers: specifier: ^4.18.3 version: 4.18.3 + playground/ssr-alias: + dependencies: + '@vitejs/test-alias-original': + specifier: file:./alias-original + version: file:playground/ssr-alias/alias-original + + playground/ssr-alias/alias-original: {} + playground/ssr-conditions: dependencies: '@vitejs/test-ssr-conditions-external': @@ -9990,6 +9998,11 @@ packages: dep-a: file:playground/preload/dep-a dev: true + file:playground/ssr-alias/alias-original: + resolution: {directory: playground/ssr-alias/alias-original, type: directory} + name: '@vitejs/test-alias-original' + dev: false + file:playground/ssr-conditions/external: resolution: {directory: playground/ssr-conditions/external, type: directory} name: '@vitejs/test-ssr-conditions-external'