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', () => {
7474 ) ,
7575 ) . toBe (
7676 '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)` ,
7878 )
7979 } )
8080
Original file line number Diff line number Diff line change @@ -847,6 +847,8 @@ export function createParseErrorInfo(
847847 showCodeFrame : ! probablyBinary ,
848848 }
849849}
850+ // prettier-ignore
851+ const interopHelper = ( m : any ) => m ?. __esModule ? m : { ...( typeof m === 'object' && ! Array . isArray ( m ) ? m : { } ) , default : m }
850852
851853export function interopNamedImports (
852854 str : MagicString ,
@@ -870,7 +872,7 @@ export function interopNamedImports(
870872 str . overwrite (
871873 expStart ,
872874 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))` +
874876 getLineBreaks ( exp ) ,
875877 { contentOnly : true } ,
876878 )
@@ -1006,7 +1008,9 @@ export function transformCjsImport(
10061008 const lines : string [ ] = [ `import ${ cjsModuleName } from "${ url } "` ]
10071009 importNames . forEach ( ( { importedName, localName } ) => {
10081010 if ( importedName === '*' ) {
1009- lines . push ( `const ${ localName } = ${ cjsModuleName } ` )
1011+ lines . push (
1012+ `const ${ localName } = (${ interopHelper . toString ( ) } )(${ cjsModuleName } )` ,
1013+ )
10101014 } else if ( importedName === 'default' ) {
10111015 lines . push (
10121016 `const ${ localName } = ${ cjsModuleName } .__esModule ? ${ cjsModuleName } .default : ${ cjsModuleName } ` ,
You can’t perform that action at this time.
0 commit comments