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: externalize dependencies by default during SSR #8033

Closed
wants to merge 1 commit into from
Closed
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
61 changes: 24 additions & 37 deletions packages/vite/src/node/ssr/ssrExternal.ts
Expand Up @@ -79,9 +79,6 @@ export function resolveSSRExternal(
return externals
}

const CJS_CONTENT_RE =
/\bmodule\.exports\b|\bexports[.\[]|\brequire\s*\(|\bObject\.(defineProperty|defineProperties|assign)\s*\(\s*exports\b/

// do we need to do this ahead of time or could we do it lazily?
function collectExternals(
root: string,
Expand Down Expand Up @@ -144,45 +141,35 @@ function collectExternals(
debug(`Failed to resolve entries for package "${id}"\n`, e)
continue
}
// no esm entry but has require entry
if (!esmEntry) {
ssrExternals.add(id)
}
// trace the dependencies of linked packages
else if (!esmEntry.includes('node_modules')) {
const pkgPath = resolveFrom(`${id}/package.json`, root)
depsToTrace.add(path.dirname(pkgPath))
}
// has separate esm/require entry, assume require entry is cjs
else if (esmEntry !== requireEntry) {
ssrExternals.add(id)
}
// if we're externalizing ESM and CJS should basically just always do it?
// or are there others like SystemJS / AMD that we'd need to handle?
// for now, we'll just leave this as is
else if (/\.m?js$/.test(esmEntry)) {
const pkgPath = resolveFrom(`${id}/package.json`, root)
const pkgContent = fs.readFileSync(pkgPath, 'utf-8')

if (!pkgContent) {
continue
}
const pkg = JSON.parse(pkgContent)
const pkgPath = resolveFrom(`${id}/package.json`, root)
const pkgContent = fs.readFileSync(pkgPath, 'utf-8')

if (pkg.type === 'module' || esmEntry.endsWith('.mjs')) {
ssrExternals.add(id)
continue
if (!pkgContent) {
continue
}
const pkg = JSON.parse(pkgContent)

if (pkg.type === 'module') {
if (requireEntry && !requireEntry.endsWith('.cjs')) {
logger.warn(
`${id} must use a .cjs extension for the CJS entry point when "type": "module" is set. Please contact the package author to fix.`
)
}
// check if the entry is cjs
const content = fs.readFileSync(esmEntry, 'utf-8')
if (CJS_CONTENT_RE.test(content)) {
ssrExternals.add(id)
continue
} else {
if (esmEntry && !esmEntry.endsWith('.mjs')) {
logger.warn(
`${id} must either set "type": "module" or use an .mjs extension for the ESM entry point. Please contact the package author to fix.`
)
}
}

logger.warn(
`${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.`
)
if (esmEntry && !esmEntry.includes('node_modules')) {
// trace the dependencies of linked packages
const pkgPath = resolveFrom(`${id}/package.json`, root)
depsToTrace.add(path.dirname(pkgPath))
} else {
benmccann marked this conversation as resolved.
Show resolved Hide resolved
ssrExternals.add(id)
}
}

Expand Down