@@ -232,89 +232,115 @@ export function cjsPlugin(
232232
233233 path . pushContainer (
234234 "body" ,
235- t . variableDeclaration ( "const " , [
235+ t . variableDeclaration ( "let " , [
236236 t . variableDeclarator (
237237 id ,
238- t . logicalExpression (
239- "??" ,
240- t . memberExpression (
241- t . identifier ( "exports" ) ,
242- t . identifier ( "default" ) ,
243- ) ,
244- t . identifier ( "exports" ) ,
245- ) ,
246238 ) ,
247239 ] ) ,
248240 ) ;
249241
250- for ( const [ local , exported ] of exportNamed . entries ( ) ) {
251- if ( exported === "default" ) continue ;
252-
253- path . pushContainer (
254- "body" ,
255- t . expressionStatement (
256- t . assignmentExpression (
257- "=" ,
258- t . memberExpression ( id , t . identifier ( exported ) ) ,
259- t . identifier ( local ) ,
260- ) ,
242+ path . pushContainer (
243+ "body" ,
244+ t . ifStatement (
245+ t . binaryExpression (
246+ "in" ,
247+ t . stringLiteral ( "default" ) ,
248+ t . identifier ( "exports" ) ,
261249 ) ,
262- ) ;
263- }
250+ t . blockStatement ( [
251+ t . expressionStatement (
252+ t . assignmentExpression (
253+ "=" ,
254+ id ,
255+ t . memberExpression (
256+ t . identifier ( "exports" ) ,
257+ t . identifier ( "default" ) ,
258+ ) ,
259+ ) ,
260+ ) ,
261+ ] ) ,
262+ t . blockStatement ( [
263+ t . expressionStatement (
264+ t . assignmentExpression ( "=" , id , t . identifier ( "exports" ) ) ,
265+ ) ,
266+ ...exportNamed . entries ( ) . filter ( ( [ _ , exported ] ) =>
267+ exported != "default"
268+ ) . map ( ( [ local , exported ] ) =>
269+ t . expressionStatement (
270+ t . assignmentExpression (
271+ "=" ,
272+ t . memberExpression ( id , t . identifier ( exported ) ) ,
273+ t . identifier ( local ) ,
274+ ) ,
275+ )
276+ ) ,
277+ ] ) ,
278+ ) ,
279+ ) ;
264280
265281 for ( let i = 0 ; i < mappedNs . length ; i ++ ) {
266282 const mapped = mappedNs [ i ] ;
267283
268284 const key = path . scope . generateUid ( "k" ) ;
269285 path . pushContainer (
270286 "body" ,
271- t . forInStatement (
272- t . variableDeclaration ( "var" , [
273- t . variableDeclarator ( t . identifier ( key ) ) ,
274- ] ) ,
275- t . identifier ( mapped ) ,
276- t . ifStatement (
277- t . logicalExpression (
278- "&&" ,
287+ t . ifStatement (
288+ t . unaryExpression (
289+ "!" ,
290+ t . binaryExpression (
291+ "in" ,
292+ t . stringLiteral ( "default" ) ,
293+ t . identifier ( "exports" ) ,
294+ ) ,
295+ ) ,
296+ t . forInStatement (
297+ t . variableDeclaration ( "var" , [
298+ t . variableDeclarator ( t . identifier ( key ) ) ,
299+ ] ) ,
300+ t . identifier ( mapped ) ,
301+ t . ifStatement (
279302 t . logicalExpression (
280303 "&&" ,
281- t . binaryExpression (
282- "!==" ,
283- t . identifier ( key ) ,
284- t . stringLiteral ( "default" ) ,
285- ) ,
286- t . binaryExpression (
287- "!==" ,
288- t . identifier ( key ) ,
289- t . stringLiteral ( "__esModule" ) ,
304+ t . logicalExpression (
305+ "&&" ,
306+ t . binaryExpression (
307+ "!==" ,
308+ t . identifier ( key ) ,
309+ t . stringLiteral ( "default" ) ,
310+ ) ,
311+ t . binaryExpression (
312+ "!==" ,
313+ t . identifier ( key ) ,
314+ t . stringLiteral ( "__esModule" ) ,
315+ ) ,
290316 ) ,
291- ) ,
292- t . callExpression (
293- t . memberExpression (
317+ t . callExpression (
294318 t . memberExpression (
295319 t . memberExpression (
296- t . identifier ( "Object" ) ,
297- t . identifier ( "prototype" ) ,
320+ t . memberExpression (
321+ t . identifier ( "Object" ) ,
322+ t . identifier ( "prototype" ) ,
323+ ) ,
324+ t . identifier ( "hasOwnProperty" ) ,
298325 ) ,
299- t . identifier ( "hasOwnProperty " ) ,
326+ t . identifier ( "call " ) ,
300327 ) ,
301- t . identifier ( "call" ) ,
328+ [ t . identifier ( mapped ) , t . identifier ( key ) ] ,
302329 ) ,
303- [ t . identifier ( mapped ) , t . identifier ( key ) ] ,
304330 ) ,
305- ) ,
306- t . expressionStatement (
307- t . assignmentExpression (
308- "=" ,
309- t . memberExpression (
310- t . cloneNode ( id , true ) ,
311- t . identifier ( key ) ,
312- true ,
313- ) ,
314- t . memberExpression (
315- t . identifier ( mapped ) ,
316- t . identifier ( key ) ,
317- true ,
331+ t . expressionStatement (
332+ t . assignmentExpression (
333+ "=" ,
334+ t . memberExpression (
335+ t . cloneNode ( id , true ) ,
336+ t . identifier ( key ) ,
337+ true ,
338+ ) ,
339+ t . memberExpression (
340+ t . identifier ( mapped ) ,
341+ t . identifier ( key ) ,
342+ true ,
343+ ) ,
318344 ) ,
319345 ) ,
320346 ) ,
0 commit comments