@@ -40,6 +40,7 @@ import {
4040 isDataUrl ,
4141 isDefined ,
4242 isExternalUrl ,
43+ isFilePathESM ,
4344 isInNodeModules ,
4445 isJSRequest ,
4546 joinUrlSegments ,
@@ -440,6 +441,12 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
440441 imports . length ,
441442 )
442443
444+ let _isNodeModeResult : boolean | undefined
445+ const isNodeMode = ( ) => {
446+ _isNodeModeResult ??= isFilePathESM ( importer , config . packageCache )
447+ return _isNodeModeResult
448+ }
449+
443450 await Promise . all (
444451 imports . map ( async ( importSpecifier , index ) => {
445452 const {
@@ -605,6 +612,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
605612 url ,
606613 index ,
607614 importer ,
615+ isNodeMode ( ) ,
608616 config ,
609617 )
610618 rewriteDone = true
@@ -623,6 +631,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
623631 url ,
624632 index ,
625633 importer ,
634+ isNodeMode ( ) ,
626635 config ,
627636 )
628637 rewriteDone = true
@@ -906,16 +915,16 @@ export function createParseErrorInfo(
906915 }
907916}
908917
909- const interopHelper = ( m : any ) =>
910- m ?. __esModule
911- ? m
912- : {
918+ const interopHelper = ( m : any , n : boolean ) =>
919+ n || ! m ?. __esModule
920+ ? {
913921 ...( ( typeof m === 'object' && ! Array . isArray ( m ) ) ||
914922 typeof m === 'function'
915923 ? m
916924 : { } ) ,
917925 default : m ,
918926 }
927+ : m
919928const interopHelperStr = interopHelper . toString ( ) . replaceAll ( '\n' , '' )
920929
921930export function interopNamedImports (
@@ -924,6 +933,7 @@ export function interopNamedImports(
924933 rewrittenUrl : string ,
925934 importIndex : number ,
926935 importer : string ,
936+ isNodeMode : boolean ,
927937 config : ResolvedConfig ,
928938) : void {
929939 const source = str . original
@@ -940,7 +950,7 @@ export function interopNamedImports(
940950 str . overwrite (
941951 expStart ,
942952 expEnd ,
943- `import('${ rewrittenUrl } ').then(m => (${ interopHelperStr } )(m.default))` +
953+ `import('${ rewrittenUrl } ').then(m => (${ interopHelperStr } )(m.default, 1 ))` +
944954 getLineBreaks ( exp ) ,
945955 { contentOnly : true } ,
946956 )
@@ -952,6 +962,7 @@ export function interopNamedImports(
952962 rawUrl ,
953963 importIndex ,
954964 importer ,
965+ isNodeMode ,
955966 config ,
956967 )
957968 if ( rewritten ) {
@@ -998,6 +1009,7 @@ export function transformCjsImport(
9981009 rawUrl : string ,
9991010 importIndex : number ,
10001011 importer : string ,
1012+ isNodeMode : boolean ,
10011013 config : ResolvedConfig ,
10021014) : string | undefined {
10031015 const node = ( parseAst ( importExp ) as Program ) . body [ 0 ]
@@ -1076,12 +1088,16 @@ export function transformCjsImport(
10761088 importNames . forEach ( ( { importedName, localName } ) => {
10771089 if ( importedName === '*' ) {
10781090 lines . push (
1079- `const ${ localName } = (${ interopHelperStr } )(${ cjsModuleName } )` ,
1091+ `const ${ localName } = (${ interopHelperStr } )(${ cjsModuleName } , ${ + isNodeMode } )` ,
10801092 )
10811093 } else if ( importedName === 'default' ) {
1082- lines . push (
1083- `const ${ localName } = ${ cjsModuleName } .__esModule ? ${ cjsModuleName } .default : ${ cjsModuleName } ` ,
1084- )
1094+ if ( isNodeMode ) {
1095+ lines . push ( `const ${ localName } = ${ cjsModuleName } ` )
1096+ } else {
1097+ lines . push (
1098+ `const ${ localName } = !${ cjsModuleName } .__esModule ? ${ cjsModuleName } : ${ cjsModuleName } .default` ,
1099+ )
1100+ }
10851101 } else {
10861102 lines . push ( `const ${ localName } = ${ cjsModuleName } ["${ importedName } "]` )
10871103 }
0 commit comments