@@ -2,7 +2,7 @@ import fs from 'fs'
2
2
import path from 'path'
3
3
import { pathToFileURL } from 'url'
4
4
import { ViteDevServer } from '..'
5
- import { dynamicImport , cleanUrl , isBuiltin , isObject , resolveFrom , unwrapId } from '../utils'
5
+ import { dynamicImport , cleanUrl , isBuiltin , resolveFrom , unwrapId , usingDynamicImport } from '../utils'
6
6
import { rebindErrorStacktrace , ssrRewriteStacktrace } from './ssrStacktrace'
7
7
import {
8
8
ssrExportAllKey ,
@@ -184,29 +184,22 @@ async function nodeImport(
184
184
if ( id . startsWith ( 'node:' ) || isBuiltin ( id ) ) {
185
185
url = id
186
186
} 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
+ }
190
191
}
191
192
const mod = await dynamicImport ( url )
192
193
return proxyESM ( id , mod )
193
194
}
194
195
195
196
// rollup-style default import interop for cjs
196
197
function proxyESM ( id : string , mod : any ) {
198
+ const defaultExport = mod . __esModule ? mod . default : mod
197
199
return new Proxy ( mod , {
198
200
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 ]
210
203
}
211
204
} )
212
205
}
0 commit comments