@@ -189,19 +189,14 @@ export function cjsPlugin(
189189 ) ;
190190 }
191191
192- const exportNamed = new Map < string , string > ( ) ;
193-
194192 const idExports : types . ExportSpecifier [ ] = [ ] ;
195193 for ( const name of exported ) {
196194 if ( name === "default" ) {
197- exportNamed . set ( name , name ) ;
198195 continue ;
199196 }
200197
201198 const id = path . scope . generateUidIdentifier ( name ) ;
202199
203- exportNamed . set ( id . name , name ) ;
204-
205200 path . pushContainer (
206201 "body" ,
207202 t . variableDeclaration (
@@ -227,7 +222,7 @@ export function cjsPlugin(
227222 ) ;
228223 }
229224
230- if ( exportNamed . size > 0 || exportedNs . size > 0 || hasEsModule ) {
225+ if ( exported . size > 0 || exportedNs . size > 0 || hasEsModule ) {
231226 const id = path . scope . generateUidIdentifier ( "__default" ) ;
232227
233228 path . pushContainer (
@@ -244,10 +239,18 @@ export function cjsPlugin(
244239 t . ifStatement (
245240 t . logicalExpression (
246241 "&&" ,
247- t . binaryExpression (
248- "===" ,
249- t . unaryExpression ( "typeof" , t . identifier ( "exports" ) ) ,
250- t . stringLiteral ( "object" ) ,
242+ t . logicalExpression (
243+ "&&" ,
244+ t . binaryExpression (
245+ "===" ,
246+ t . unaryExpression ( "typeof" , t . identifier ( "exports" ) ) ,
247+ t . stringLiteral ( "object" ) ,
248+ ) ,
249+ t . binaryExpression (
250+ "!==" ,
251+ t . identifier ( "exports" ) ,
252+ t . nullLiteral ( ) ,
253+ ) ,
251254 ) ,
252255 t . binaryExpression (
253256 "in" ,
@@ -284,10 +287,18 @@ export function cjsPlugin(
284287 t . ifStatement (
285288 t . logicalExpression (
286289 "&&" ,
287- t . binaryExpression (
288- "===" ,
289- t . unaryExpression ( "typeof" , t . identifier ( "exports" ) ) ,
290- t . stringLiteral ( "object" ) ,
290+ t . logicalExpression (
291+ "&&" ,
292+ t . binaryExpression (
293+ "===" ,
294+ t . unaryExpression ( "typeof" , t . identifier ( "exports" ) ) ,
295+ t . stringLiteral ( "object" ) ,
296+ ) ,
297+ t . binaryExpression (
298+ "!==" ,
299+ t . identifier ( "exports" ) ,
300+ t . nullLiteral ( ) ,
301+ ) ,
291302 ) ,
292303 t . unaryExpression (
293304 "!" ,
@@ -694,6 +705,7 @@ export function cjsPlugin(
694705 state . set ( HAS_ES_MODULE , true ) ;
695706 } else if ( left . isMemberExpression ( ) ) {
696707 if ( isModuleExports ( t , left . node ) ) {
708+ // Should always try to create synthetic default export in this case.
697709 exported . add ( "default" ) ;
698710
699711 if ( t . isObjectExpression ( expr . node . right ) ) {
0 commit comments