Skip to content

Commit 9c8451e

Browse files
committed
fix: externalize dependencies by default during SSR
1 parent 560fc4d commit 9c8451e

File tree

1 file changed

+2
-45
lines changed

1 file changed

+2
-45
lines changed

packages/vite/src/node/ssr/ssrExternal.ts

+2-45
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
import fs from 'fs'
21
import path from 'path'
32
import type { InternalResolveOptions } from '../plugins/resolve'
43
import { tryNodeResolve } from '../plugins/resolve'
5-
import {
6-
createDebugger,
7-
isDefined,
8-
lookupFile,
9-
normalizePath,
10-
resolveFrom
11-
} from '../utils'
4+
import { createDebugger, isDefined, lookupFile, resolveFrom } from '../utils'
125
import type { Logger, ResolvedConfig } from '..'
136
import { createFilter } from '@rollup/pluginutils'
147

@@ -79,9 +72,6 @@ export function resolveSSRExternal(
7972
return externals
8073
}
8174

82-
const CJS_CONTENT_RE =
83-
/\bmodule\.exports\b|\bexports[.\[]|\brequire\s*\(|\bObject\.(defineProperty|defineProperties|assign)\s*\(\s*exports\b/
84-
8575
// do we need to do this ahead of time or could we do it lazily?
8676
function collectExternals(
8777
root: string,
@@ -115,7 +105,6 @@ function collectExternals(
115105
seen.add(id)
116106

117107
let esmEntry: string | undefined
118-
let requireEntry: string
119108
try {
120109
esmEntry = tryNodeResolve(
121110
id,
@@ -125,9 +114,6 @@ function collectExternals(
125114
undefined,
126115
true
127116
)?.id
128-
// normalizePath required for windows. tryNodeResolve uses normalizePath
129-
// which returns with '/', require.resolve returns with '\\'
130-
requireEntry = normalizePath(require.resolve(id, { paths: [root] }))
131117
} catch (e) {
132118
try {
133119
// no main entry, but deep imports may be allowed
@@ -152,38 +138,9 @@ function collectExternals(
152138
else if (!esmEntry.includes('node_modules')) {
153139
const pkgPath = resolveFrom(`${id}/package.json`, root)
154140
depsToTrace.add(path.dirname(pkgPath))
155-
}
156-
// has separate esm/require entry, assume require entry is cjs
157-
else if (esmEntry !== requireEntry) {
141+
} else {
158142
ssrExternals.add(id)
159143
}
160-
// if we're externalizing ESM and CJS should basically just always do it?
161-
// or are there others like SystemJS / AMD that we'd need to handle?
162-
// for now, we'll just leave this as is
163-
else if (/\.m?js$/.test(esmEntry)) {
164-
const pkgPath = resolveFrom(`${id}/package.json`, root)
165-
const pkgContent = fs.readFileSync(pkgPath, 'utf-8')
166-
167-
if (!pkgContent) {
168-
continue
169-
}
170-
const pkg = JSON.parse(pkgContent)
171-
172-
if (pkg.type === 'module' || esmEntry.endsWith('.mjs')) {
173-
ssrExternals.add(id)
174-
continue
175-
}
176-
// check if the entry is cjs
177-
const content = fs.readFileSync(esmEntry, 'utf-8')
178-
if (CJS_CONTENT_RE.test(content)) {
179-
ssrExternals.add(id)
180-
continue
181-
}
182-
183-
logger.warn(
184-
`${id} doesn't appear to be written in CJS, but also doesn't appear to be a valid ES module (i.e. it doesn't have "type": "module" or an .mjs extension for the entry point). Please contact the package author to fix.`
185-
)
186-
}
187144
}
188145

189146
for (const depRoot of depsToTrace) {

0 commit comments

Comments
 (0)