Skip to content

Commit 0195494

Browse files
committed
fix(ssr): handle invalid package resolution on runtime instead of import analysis
1 parent 4fd3a01 commit 0195494

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

packages/vite/src/node/plugins/importAnalysis.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ import {
7373
import type { TransformPluginContext } from '../server/pluginContainer'
7474
import { throwOutdatedRequest } from './optimizedDeps'
7575
import { isDirectCSSRequest } from './css'
76-
import { browserExternalId } from './resolve'
76+
import { ERR_RESOLVE_PACKAGE_ENTRY_FAIL, browserExternalId } from './resolve'
7777
import { serializeDefine } from './define'
7878
import { WORKER_FILE_ID } from './worker'
7979
import { getAliasPatternMatcher } from './preAlias'
@@ -357,6 +357,14 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
357357
const resolved = await this.resolve(url, importerFile).catch((e) => {
358358
if (e instanceof Error) {
359359
;(e as RollupError).pos ??= pos
360+
// ssr should be able to handle invalid package durnig runtime
361+
if (
362+
ssr &&
363+
'code' in e &&
364+
e.code === ERR_RESOLVE_PACKAGE_ENTRY_FAIL
365+
) {
366+
return null
367+
}
360368
}
361369
throw e
362370
})

packages/vite/src/node/plugins/resolve.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ import type { ResolvedConfig, ResolvedEnvironmentOptions } from '../config'
5656
const normalizedClientEntry = normalizePath(CLIENT_ENTRY)
5757
const normalizedEnvEntry = normalizePath(ENV_ENTRY)
5858

59-
const ERR_RESOLVE_PACKAGE_ENTRY_FAIL = 'ERR_RESOLVE_PACKAGE_ENTRY_FAIL'
59+
export const ERR_RESOLVE_PACKAGE_ENTRY_FAIL = 'ERR_RESOLVE_PACKAGE_ENTRY_FAIL'
6060

6161
// special id for paths marked with browser: false
6262
// https://github.com/defunctzombie/package-browser-field-spec#ignore-a-module

0 commit comments

Comments
 (0)