Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -64,6 +64,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 @@ -177,6 +178,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 @@ -494,7 +496,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 @@ -129,3 +129,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',
})
})
Comment on lines +4 to +11
Copy link
Collaborator Author

@hi-ogawa hi-ogawa Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous behavior was that:

  {
    dep: 'original',
    nonDep: 'ok',
    builtin: undefined,
  }

which is inconsistent with build mode.


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.