Skip to content

Commit

Permalink
fix(ssr): improve ssr external heuristics
Browse files Browse the repository at this point in the history
fix #1854
  • Loading branch information
yyx990803 committed Feb 3, 2021
1 parent e089eff commit 928fc33
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
7 changes: 2 additions & 5 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { TransformOptions } from 'esbuild'
import { CleanCSS } from 'types/clean-css'
import { dataURIPlugin } from './plugins/dataUri'
import { buildImportAnalysisPlugin } from './plugins/importAnaysisBuild'
import { resolveSSRExternal } from './ssr/ssrExternal'
import { resolveSSRExternal, shouldExternalizeForSSR } from './ssr/ssrExternal'
import { ssrManifestPlugin } from './ssr/ssrManifestPlugin'
import { isCSSRequest } from './plugins/css'

Expand Down Expand Up @@ -576,10 +576,7 @@ function resolveExternal(
user: ExternalOption | undefined
): ExternalOption {
return ((id, parentId, isResolved) => {
if (
ssrExternals.includes(id) ||
ssrExternals.some((e) => id.startsWith(e + '/'))
) {
if (shouldExternalizeForSSR(id, ssrExternals)) {
return true
}
if (user) {
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { parse as parseJS } from 'acorn'
import type { Node } from 'estree'
import { transformImportGlob } from '../importGlob'
import { makeLegalIdentifier } from '@rollup/pluginutils'
import { shouldExternalizeForSSR } from '../ssr/ssrExternal'

const isDebug = !!process.env.DEBUG
const debugRewrite = createDebugger('vite:rewrite')
Expand Down Expand Up @@ -323,9 +324,8 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
// skip ssr external
if (ssr) {
if (
server._ssrExternals?.some((id) => {
return url === id || url.startsWith(id + '/')
})
server._ssrExternals &&
shouldExternalizeForSSR(url, server._ssrExternals)
) {
continue
}
Expand Down
21 changes: 20 additions & 1 deletion packages/vite/src/node/ssr/ssrExternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ export function resolveSSRExternal(
}

for (const id of deps) {
const entry = tryNodeResolve(id, undefined, root, false)?.id
let entry
let requireEntry
try {
entry = tryNodeResolve(id, undefined, root, false)?.id
requireEntry = require.resolve(id, { paths: [root] })
} catch (e) {
// resolve failed, assume include
continue
}
if (!entry) {
Expand Down Expand Up @@ -81,3 +83,20 @@ export function resolveSSRExternal(
}
return externals
}

export function shouldExternalizeForSSR(
id: string,
externals: string[]
): boolean {
const should = externals.some((e) => {
if (id === e) {
return true
}
// deep imports, check ext before externalizing - only externalize
// extension-less imports and explicit .js imports
if (id.startsWith(e + '/') && (!path.extname(id) || id.endsWith('.js'))) {
return true
}
})
return should
}

0 comments on commit 928fc33

Please sign in to comment.