Skip to content

Commit 2c70d99

Browse files
committed
Get tests passing
1 parent 9c6ab45 commit 2c70d99

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fs from 'fs'
12
import path from 'path'
23
import { tryNodeResolve, InternalResolveOptions } from '../plugins/resolve'
34
import {
@@ -93,7 +94,15 @@ export function resolveSSRExternal(
9394
// entry is not js, cannot externalize
9495
continue
9596
}
96-
ssrExternals.add(id)
97+
if (pkg.type === "module" || entry.endsWith('.mjs')) {
98+
ssrExternals.add(id)
99+
continue
100+
}
101+
// check if the entry is cjs
102+
const content = fs.readFileSync(entry, 'utf-8')
103+
if (/\bmodule\.exports\b|\bexports[.\[]|\brequire\s*\(/.test(content)) {
104+
ssrExternals.add(id)
105+
}
97106
}
98107
}
99108

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

+8-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'fs'
22
import path from 'path'
33
import { pathToFileURL } from 'url'
44
import { ViteDevServer } from '..'
5-
import { dynamicImport, cleanUrl, isBuiltin, isObject, resolveFrom, unwrapId } from '../utils'
5+
import { dynamicImport, cleanUrl, isBuiltin, resolveFrom, unwrapId, usingDynamicImport } from '../utils'
66
import { rebindErrorStacktrace, ssrRewriteStacktrace } from './ssrStacktrace'
77
import {
88
ssrExportAllKey,
@@ -184,29 +184,22 @@ async function nodeImport(
184184
if (id.startsWith('node:') || isBuiltin(id)) {
185185
url = id
186186
} else {
187-
url = pathToFileURL(
188-
resolve(id, importer, config.root, !!config.resolve.preserveSymlinks)
189-
).toString()
187+
url = resolve(id, importer, config.root, !!config.resolve.preserveSymlinks)
188+
if (usingDynamicImport) {
189+
url = pathToFileURL(url).toString()
190+
}
190191
}
191192
const mod = await dynamicImport(url)
192193
return proxyESM(id, mod)
193194
}
194195

195196
// rollup-style default import interop for cjs
196197
function proxyESM(id: string, mod: any) {
198+
const defaultExport = mod.__esModule ? mod.default : mod
197199
return new Proxy(mod, {
198200
get(mod, prop) {
199-
if (prop in mod) {
200-
return mod[prop]
201-
}
202-
// commonjs interop: module whose exports are not statically analyzable
203-
if (isObject(mod.default) && prop in mod.default) {
204-
return mod.default[prop]
205-
}
206-
// throw an error like ESM import does
207-
throw new SyntaxError(
208-
`The requested module '${id}' does not provide an export named '${prop.toString()}'`
209-
)
201+
if (prop === 'default') return defaultExport
202+
return mod[prop]
210203
}
211204
})
212205
}

packages/vite/src/node/utils.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ export function toUpperCaseDriveLetter(pathName: string): string {
569569
export const multilineCommentsRE = /\/\*(.|[\r\n])*?\*\//gm
570570
export const singlelineCommentsRE = /\/\/.*/g
571571

572+
export const usingDynamicImport = typeof jest === 'undefined';
572573
/**
573574
* Dynamically import files. It will make sure it's not being compiled away by TS/Rollup.
574575
*
@@ -578,4 +579,4 @@ export const singlelineCommentsRE = /\/\/.*/g
578579
*
579580
* @param file File path to import.
580581
*/
581-
export const dynamicImport = typeof jest === 'undefined' ? new Function('file', 'return import(file)') : require;
582+
export const dynamicImport = usingDynamicImport ? new Function('file', 'return import(file)') : require;

0 commit comments

Comments
 (0)