@@ -33,6 +33,28 @@ export function cjsPlugin(
3333
3434 if ( body . length === 0 && state . get ( HAS_ES_MODULE ) ) {
3535 path . pushContainer ( "body" , t . exportNamedDeclaration ( null ) ) ;
36+ } else {
37+ const seen = new Set < string > ( ) ;
38+
39+ const children = path . get ( "body" ) ;
40+ for ( let i = children . length - 1 ; i >= 0 ; i -- ) {
41+ const child = children [ i ] ;
42+ if ( child . isExportNamedDeclaration ( ) ) {
43+ if (
44+ t . isVariableDeclaration ( child . node . declaration ) &&
45+ child . node . declaration . declarations . length > 0 &&
46+ t . isIdentifier ( child . node . declaration . declarations [ 0 ] . id )
47+ ) {
48+ const name = child . node . declaration . declarations [ 0 ] . id . name ;
49+
50+ if ( seen . has ( name ) ) {
51+ child . remove ( ) ;
52+ } else {
53+ seen . add ( name ) ;
54+ }
55+ }
56+ }
57+ }
3658 }
3759 } ,
3860 } ,
@@ -112,6 +134,70 @@ export function cjsPlugin(
112134 }
113135 }
114136 }
137+ } else if ( expr . isCallExpression ( ) ) {
138+ if (
139+ t . isMemberExpression ( expr . node . callee ) &&
140+ t . isIdentifier ( expr . node . callee . object ) &&
141+ expr . node . callee . object . name === "Object" &&
142+ t . isIdentifier ( expr . node . callee . property ) &&
143+ expr . node . callee . property . name === "defineProperty" &&
144+ expr . node . arguments . length === 3 &&
145+ t . isIdentifier ( expr . node . arguments [ 0 ] ) &&
146+ expr . node . arguments [ 0 ] . name === "exports" &&
147+ t . isStringLiteral ( expr . node . arguments [ 1 ] ) &&
148+ expr . node . arguments [ 1 ] . value !== "__esModule" &&
149+ t . isObjectExpression ( expr . node . arguments [ 2 ] )
150+ ) {
151+ const named = expr . node . arguments [ 1 ] . value ;
152+ const obj = expr . node . arguments [ 2 ] ;
153+
154+ let right : types . Expression = t . nullLiteral ( ) ;
155+
156+ for ( let i = 0 ; i < obj . properties . length ; i ++ ) {
157+ const prop = obj . properties [ i ] ;
158+
159+ if ( t . isObjectProperty ( prop ) ) {
160+ if ( t . isIdentifier ( prop . key ) ) {
161+ if ( prop . key . name === "get" ) {
162+ if (
163+ t . isFunctionExpression ( prop . value ) ||
164+ t . isArrowFunctionExpression ( prop . value )
165+ ) {
166+ right = t . callExpression (
167+ t . parenthesizedExpression (
168+ t . cloneNode ( prop . value , true ) ,
169+ ) ,
170+ [ ] ,
171+ ) ;
172+ }
173+ }
174+ }
175+ } else if ( t . isObjectMethod ( prop ) ) {
176+ if ( t . isIdentifier ( prop . key ) ) {
177+ if ( prop . key . name === "get" ) {
178+ right = t . callExpression (
179+ t . parenthesizedExpression (
180+ t . functionExpression (
181+ null ,
182+ [ ] ,
183+ t . cloneNode ( prop . body , true ) ,
184+ ) ,
185+ ) ,
186+ [ ] ,
187+ ) ;
188+ }
189+ }
190+ }
191+ }
192+
193+ path . replaceWith (
194+ t . exportNamedDeclaration (
195+ t . variableDeclaration ( "let" , [
196+ t . variableDeclarator ( t . identifier ( named ) , right ) ,
197+ ] ) ,
198+ ) ,
199+ ) ;
200+ }
115201 }
116202 } ,
117203 } ,
0 commit comments