fix(ssr): apply alias to resolvable dependencies during dev (#15602)

This commit is contained in:
Hiroshi Ogawa 2024-03-12 23:14:51 +09:00 committed by GitHub
parent e030f4bfd1
commit 8e54af67db
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 93 additions and 1 deletions

View File

@ -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
}

View File

@ -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))
}

View File

@ -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',
})
})

View File

@ -0,0 +1 @@
export default 'original'

View File

@ -0,0 +1,9 @@
{
"name": "@vitejs/test-alias-original",
"version": "0.0.0",
"private": true,
"type": "module",
"exports": {
".": "./index.js"
}
}

View File

@ -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"
}
}

View File

@ -0,0 +1,3 @@
export default {
env: { __TEST_ALIAS__: 'ok' },
}

View File

@ -0,0 +1 @@
export default 'ok'

View File

@ -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__'],
}

View File

@ -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',
},
},
})

View File

@ -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'