@@ -273,9 +273,17 @@ export class ViteNodeRunner {
273273 enumerable : false ,
274274 configurable : false ,
275275 } )
276- // this prosxy is triggered only on exports.name and module.exports access
276+ // this prosxy is triggered only on exports.{ name} and module.exports access
277277 const cjsExports = new Proxy ( exports , {
278- set ( _ , p , value ) {
278+ set : ( _ , p , value ) => {
279+ // treat "module.exports =" the same as "exports.default =" to not have nested "default.default",
280+ // so "exports.default" becomes the actual module
281+ if ( p === 'default' && this . shouldInterop ( url , { default : value } ) ) {
282+ exportAll ( cjsExports , value )
283+ exports . default = value
284+ return true
285+ }
286+
279287 if ( ! Reflect . has ( exports , 'default' ) )
280288 exports . default = { }
281289
@@ -378,35 +386,45 @@ export class ViteNodeRunner {
378386 * Import a module and interop it
379387 */
380388 async interopedImport ( path : string ) {
381- const mod = await import ( path )
382-
383- if ( this . shouldInterop ( path , mod ) ) {
384- const tryDefault = this . hasNestedDefault ( mod )
385- return new Proxy ( mod , {
386- get : proxyMethod ( 'get' , tryDefault ) ,
387- set : proxyMethod ( 'set' , tryDefault ) ,
388- has : proxyMethod ( 'has' , tryDefault ) ,
389- deleteProperty : proxyMethod ( 'deleteProperty' , tryDefault ) ,
390- } )
391- }
389+ const importedModule = await import ( path )
392390
393- return mod
394- }
391+ if ( ! this . shouldInterop ( path , importedModule ) )
392+ return importedModule
395393
396- hasNestedDefault ( target : any ) {
397- return '__esModule' in target && target . __esModule && 'default' in target . default
394+ const { mod, defaultExport } = interopModule ( importedModule )
395+
396+ return new Proxy ( mod , {
397+ get ( mod , prop ) {
398+ if ( prop === 'default' )
399+ return defaultExport
400+ return mod [ prop ] ?? defaultExport ?. [ prop ]
401+ } ,
402+ has ( mod , prop ) {
403+ if ( prop === 'default' )
404+ return defaultExport !== undefined
405+ return prop in mod || ( defaultExport && prop in defaultExport )
406+ } ,
407+ } )
398408 }
399409}
400410
401- function proxyMethod ( name : 'get' | 'set' | 'has' | 'deleteProperty' , tryDefault : boolean ) {
402- return function ( target : any , key : string | symbol , ...args : [ any ?, any ?] ) : any {
403- const result = Reflect [ name ] ( target , key , ...args )
404- if ( isPrimitive ( target . default ) )
405- return result
406- if ( ( tryDefault && key === 'default' ) || typeof result === 'undefined' )
407- return Reflect [ name ] ( target . default , key , ...args )
408- return result
411+ function interopModule ( mod : any ) {
412+ if ( isPrimitive ( mod ) ) {
413+ return {
414+ mod : { default : mod } ,
415+ defaultExport : mod ,
416+ }
409417 }
418+
419+ let defaultExport = 'default' in mod ? mod . default : mod
420+
421+ if ( ! isPrimitive ( defaultExport ) && '__esModule' in defaultExport ) {
422+ mod = defaultExport
423+ if ( 'default' in defaultExport )
424+ defaultExport = defaultExport . default
425+ }
426+
427+ return { mod, defaultExport }
410428}
411429
412430// keep consistency with Vite on how exports are defined
0 commit comments