From 2c70d997538740319279945cb567383dd193d47a Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Fri, 15 Oct 2021 13:00:48 -0400 Subject: [PATCH] Get tests passing --- packages/vite/src/node/ssr/ssrExternal.ts | 11 ++++++++- packages/vite/src/node/ssr/ssrModuleLoader.ts | 23 +++++++------------ packages/vite/src/node/utils.ts | 3 ++- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/packages/vite/src/node/ssr/ssrExternal.ts b/packages/vite/src/node/ssr/ssrExternal.ts index 3b56d0779b985b..a0bfd782b8a251 100644 --- a/packages/vite/src/node/ssr/ssrExternal.ts +++ b/packages/vite/src/node/ssr/ssrExternal.ts @@ -1,3 +1,4 @@ +import fs from 'fs' import path from 'path' import { tryNodeResolve, InternalResolveOptions } from '../plugins/resolve' import { @@ -93,7 +94,15 @@ export function resolveSSRExternal( // entry is not js, cannot externalize continue } - ssrExternals.add(id) + if (pkg.type === "module" || entry.endsWith('.mjs')) { + ssrExternals.add(id) + continue + } + // check if the entry is cjs + const content = fs.readFileSync(entry, 'utf-8') + if (/\bmodule\.exports\b|\bexports[.\[]|\brequire\s*\(/.test(content)) { + ssrExternals.add(id) + } } } diff --git a/packages/vite/src/node/ssr/ssrModuleLoader.ts b/packages/vite/src/node/ssr/ssrModuleLoader.ts index 2ce2225717f4e6..f402e86cd210ec 100644 --- a/packages/vite/src/node/ssr/ssrModuleLoader.ts +++ b/packages/vite/src/node/ssr/ssrModuleLoader.ts @@ -2,7 +2,7 @@ import fs from 'fs' import path from 'path' import { pathToFileURL } from 'url' import { ViteDevServer } from '..' -import { dynamicImport, cleanUrl, isBuiltin, isObject, resolveFrom, unwrapId } from '../utils' +import { dynamicImport, cleanUrl, isBuiltin, resolveFrom, unwrapId, usingDynamicImport } from '../utils' import { rebindErrorStacktrace, ssrRewriteStacktrace } from './ssrStacktrace' import { ssrExportAllKey, @@ -184,9 +184,10 @@ async function nodeImport( if (id.startsWith('node:') || isBuiltin(id)) { url = id } else { - url = pathToFileURL( - resolve(id, importer, config.root, !!config.resolve.preserveSymlinks) - ).toString() + url = resolve(id, importer, config.root, !!config.resolve.preserveSymlinks) + if (usingDynamicImport) { + url = pathToFileURL(url).toString() + } } const mod = await dynamicImport(url) return proxyESM(id, mod) @@ -194,19 +195,11 @@ async function nodeImport( // rollup-style default import interop for cjs function proxyESM(id: string, mod: any) { + const defaultExport = mod.__esModule ? mod.default : mod return new Proxy(mod, { get(mod, prop) { - if (prop in mod) { - return mod[prop] - } - // commonjs interop: module whose exports are not statically analyzable - if (isObject(mod.default) && prop in mod.default) { - return mod.default[prop] - } - // throw an error like ESM import does - throw new SyntaxError( - `The requested module '${id}' does not provide an export named '${prop.toString()}'` - ) + if (prop === 'default') return defaultExport + return mod[prop] } }) } diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index beae26fca4da26..c62e29da900e05 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -569,6 +569,7 @@ export function toUpperCaseDriveLetter(pathName: string): string { export const multilineCommentsRE = /\/\*(.|[\r\n])*?\*\//gm export const singlelineCommentsRE = /\/\/.*/g +export const usingDynamicImport = typeof jest === 'undefined'; /** * Dynamically import files. It will make sure it's not being compiled away by TS/Rollup. * @@ -578,4 +579,4 @@ export const singlelineCommentsRE = /\/\/.*/g * * @param file File path to import. */ -export const dynamicImport = typeof jest === 'undefined' ? new Function('file', 'return import(file)') : require; +export const dynamicImport = usingDynamicImport ? new Function('file', 'return import(file)') : require;