@@ -22,7 +22,7 @@ import {
22
22
Node ,
23
23
ClassDeclaration ,
24
24
} from 'estree' ;
25
- import { TransformSourceDescription } from 'rollup' ;
25
+ import { TransformSourceDescription , OutputOptions } from 'rollup' ;
26
26
import { NamedDeclaration , DefaultDeclaration } from './parsing-utilities' ;
27
27
import { isESMFormat } from '../options' ;
28
28
import {
@@ -34,6 +34,20 @@ import {
34
34
import MagicString from 'magic-string' ;
35
35
import { parse , walk } from '../acorn' ;
36
36
37
+ const CJS_EXTERN = `/**
38
+ * @fileoverview Externs built via derived configuration from Rollup or input code.
39
+ * This extern contains the export global object so Closure doesn't get confused by its presence.
40
+ * @externs
41
+ */
42
+
43
+ /**
44
+ * @typedef {{
45
+ * __esModule: boolean,
46
+ * }}
47
+ */
48
+ let exports;
49
+ ` ;
50
+
37
51
/**
38
52
* This Transform will apply only if the Rollup configuration is for 'esm' output.
39
53
*
@@ -76,6 +90,14 @@ export default class ExportTransform extends Transform implements TransformInter
76
90
return originalExports ;
77
91
}
78
92
93
+ public extern ( options : OutputOptions ) : string {
94
+ if ( options . format === 'cjs' ) {
95
+ return CJS_EXTERN ;
96
+ }
97
+
98
+ return '' ;
99
+ }
100
+
79
101
/**
80
102
* Before Closure Compiler modifies the source, we need to ensure it has window scoped
81
103
* references to the named exports. This prevents Closure from mangling their names.
@@ -225,9 +247,7 @@ export default class ExportTransform extends Transform implements TransformInter
225
247
226
248
if ( originalExports [ exportName ] . alias !== null ) {
227
249
collectedExportsToAppend . push (
228
- `${ ancestor . expression . left . property . name } as ${
229
- originalExports [ exportName ] . alias
230
- } `,
250
+ `${ ancestor . expression . left . property . name } as ${ originalExports [ exportName ] . alias } ` ,
231
251
) ;
232
252
} else {
233
253
collectedExportsToAppend . push ( ancestor . expression . left . property . name ) ;
@@ -250,9 +270,7 @@ export default class ExportTransform extends Transform implements TransformInter
250
270
251
271
if ( ancestor . expression . right . type === 'Identifier' ) {
252
272
collectedExportsToAppend . push (
253
- `${ ancestor . expression . right . name } as ${
254
- ancestor . expression . left . property . name
255
- } `,
273
+ `${ ancestor . expression . right . name } as ${ ancestor . expression . left . property . name } ` ,
256
274
) ;
257
275
}
258
276
break ;
0 commit comments