Skip to content

Commit

Permalink
Get tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp committed Oct 15, 2021
1 parent 9c6ab45 commit 2c70d99
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
11 changes: 10 additions & 1 deletion packages/vite/src/node/ssr/ssrExternal.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs'
import path from 'path'
import { tryNodeResolve, InternalResolveOptions } from '../plugins/resolve'
import {
Expand Down Expand Up @@ -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)
}
}
}

Expand Down
23 changes: 8 additions & 15 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -184,29 +184,22 @@ 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)
}

// 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]
}
})
}
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;

0 comments on commit 2c70d99

Please sign in to comment.