mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 14:48:41 +00:00
fix(ssr): apply alias to resolvable dependencies during dev (#15602)
This commit is contained in:
parent
e030f4bfd1
commit
8e54af67db
@ -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
|
||||
}
|
||||
|
@ -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))
|
||||
}
|
||||
|
20
playground/ssr-alias/__tests__/ssr-alias.spec.ts
Normal file
20
playground/ssr-alias/__tests__/ssr-alias.spec.ts
Normal 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',
|
||||
})
|
||||
})
|
1
playground/ssr-alias/alias-original/index.js
Normal file
1
playground/ssr-alias/alias-original/index.js
Normal file
@ -0,0 +1 @@
|
||||
export default 'original'
|
9
playground/ssr-alias/alias-original/package.json
Normal file
9
playground/ssr-alias/alias-original/package.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"name": "@vitejs/test-alias-original",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": "./index.js"
|
||||
}
|
||||
}
|
12
playground/ssr-alias/package.json
Normal file
12
playground/ssr-alias/package.json
Normal 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"
|
||||
}
|
||||
}
|
3
playground/ssr-alias/src/alias-process.js
Normal file
3
playground/ssr-alias/src/alias-process.js
Normal file
@ -0,0 +1,3 @@
|
||||
export default {
|
||||
env: { __TEST_ALIAS__: 'ok' },
|
||||
}
|
1
playground/ssr-alias/src/alias-replaced.js
Normal file
1
playground/ssr-alias/src/alias-replaced.js
Normal file
@ -0,0 +1 @@
|
||||
export default 'ok'
|
9
playground/ssr-alias/src/main.js
Normal file
9
playground/ssr-alias/src/main.js
Normal 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__'],
|
||||
}
|
14
playground/ssr-alias/vite.config.js
Normal file
14
playground/ssr-alias/vite.config.js
Normal 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',
|
||||
},
|
||||
},
|
||||
})
|
@ -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'
|
||||
|
Loading…
Reference in New Issue
Block a user