File tree Expand file tree Collapse file tree 2 files changed +7
-3
lines changed Expand file tree Collapse file tree 2 files changed +7
-3
lines changed Original file line number Diff line number Diff line change @@ -74,7 +74,7 @@ describe('transformCjsImport', () => {
74
74
) ,
75
75
) . toBe (
76
76
'import __vite__cjsImport0_react from "./node_modules/.vite/deps/react.js"; ' +
77
- ' const react = __vite__cjsImport0_react' ,
77
+ ` const react = ((m) => m?.__esModule ? m : { ...typeof m === "object" && !Array.isArray(m) ? m : {}, default: m })( __vite__cjsImport0_react)` ,
78
78
)
79
79
} )
80
80
Original file line number Diff line number Diff line change @@ -847,6 +847,8 @@ export function createParseErrorInfo(
847
847
showCodeFrame : ! probablyBinary ,
848
848
}
849
849
}
850
+ // prettier-ignore
851
+ const interopHelper = ( m : any ) => m ?. __esModule ? m : { ...( typeof m === 'object' && ! Array . isArray ( m ) ? m : { } ) , default : m }
850
852
851
853
export function interopNamedImports (
852
854
str : MagicString ,
@@ -870,7 +872,7 @@ export function interopNamedImports(
870
872
str . overwrite (
871
873
expStart ,
872
874
expEnd ,
873
- `import('${ rewrittenUrl } ').then(m => m.default && m.default.__esModule ? m.default : ({ ... m.default, default: m.default } ))` +
875
+ `import('${ rewrittenUrl } ').then(m => ( ${ interopHelper . toString ( ) } )( m.default))` +
874
876
getLineBreaks ( exp ) ,
875
877
{ contentOnly : true } ,
876
878
)
@@ -1006,7 +1008,9 @@ export function transformCjsImport(
1006
1008
const lines : string [ ] = [ `import ${ cjsModuleName } from "${ url } "` ]
1007
1009
importNames . forEach ( ( { importedName, localName } ) => {
1008
1010
if ( importedName === '*' ) {
1009
- lines . push ( `const ${ localName } = ${ cjsModuleName } ` )
1011
+ lines . push (
1012
+ `const ${ localName } = (${ interopHelper . toString ( ) } )(${ cjsModuleName } )` ,
1013
+ )
1010
1014
} else if ( importedName === 'default' ) {
1011
1015
lines . push (
1012
1016
`const ${ localName } = ${ cjsModuleName } .__esModule ? ${ cjsModuleName } .default : ${ cjsModuleName } ` ,
You can’t perform that action at this time.
0 commit comments