Skip to content

Commit

Permalink
fix(ssr): apply alias to resolvable dependencies during dev (#15602)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Mar 12, 2024
1 parent e030f4b commit 8e54af6
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 8 additions & 0 deletions packages/vite/src/node/plugins/preAlias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 20 additions & 0 deletions playground/ssr-alias/__tests__/ssr-alias.spec.ts
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions playground/ssr-alias/alias-original/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'original'
9 changes: 9 additions & 0 deletions playground/ssr-alias/alias-original/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@vitejs/test-alias-original",
"version": "0.0.0",
"private": true,
"type": "module",
"exports": {
".": "./index.js"
}
}
12 changes: 12 additions & 0 deletions playground/ssr-alias/package.json
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions playground/ssr-alias/src/alias-process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
env: { __TEST_ALIAS__: 'ok' },
}
1 change: 1 addition & 0 deletions playground/ssr-alias/src/alias-replaced.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'ok'
9 changes: 9 additions & 0 deletions playground/ssr-alias/src/main.js
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 0 deletions playground/ssr-alias/vite.config.js
Original file line number Diff line number Diff line change
@@ -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',
},
},
})
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8e54af6

Please sign in to comment.