From 9c8451e9345cecd32f409141b09b7400b2ec9a8f Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 14 Mar 2022 15:12:49 -0700 Subject: [PATCH] fix: externalize dependencies by default during SSR --- packages/vite/src/node/ssr/ssrExternal.ts | 47 +---------------------- 1 file changed, 2 insertions(+), 45 deletions(-) diff --git a/packages/vite/src/node/ssr/ssrExternal.ts b/packages/vite/src/node/ssr/ssrExternal.ts index 800a2307dabae6..e26667b314071f 100644 --- a/packages/vite/src/node/ssr/ssrExternal.ts +++ b/packages/vite/src/node/ssr/ssrExternal.ts @@ -1,14 +1,7 @@ -import fs from 'fs' import path from 'path' import type { InternalResolveOptions } from '../plugins/resolve' import { tryNodeResolve } from '../plugins/resolve' -import { - createDebugger, - isDefined, - lookupFile, - normalizePath, - resolveFrom -} from '../utils' +import { createDebugger, isDefined, lookupFile, resolveFrom } from '../utils' import type { Logger, ResolvedConfig } from '..' import { createFilter } from '@rollup/pluginutils' @@ -79,9 +72,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, @@ -115,7 +105,6 @@ function collectExternals( seen.add(id) let esmEntry: string | undefined - let requireEntry: string try { esmEntry = tryNodeResolve( id, @@ -125,9 +114,6 @@ function collectExternals( undefined, true )?.id - // normalizePath required for windows. tryNodeResolve uses normalizePath - // which returns with '/', require.resolve returns with '\\' - requireEntry = normalizePath(require.resolve(id, { paths: [root] })) } catch (e) { try { // no main entry, but deep imports may be allowed @@ -152,38 +138,9 @@ function collectExternals( 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) { + } else { 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) - - if (pkg.type === 'module' || esmEntry.endsWith('.mjs')) { - ssrExternals.add(id) - continue - } - // check if the entry is cjs - const content = fs.readFileSync(esmEntry, 'utf-8') - if (CJS_CONTENT_RE.test(content)) { - ssrExternals.add(id) - continue - } - - 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.` - ) - } } for (const depRoot of depsToTrace) {