Skip to content

Commit 6069883

Browse files
Remove exports before calling closure compiler (#68)
* Remove exports before calling closure compiler * Tests for export transpilation * Added an ES5 test for every scenario
1 parent e979a43 commit 6069883

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+152
-5
lines changed

src/transformers/exports.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,12 @@ export default class ExportTransform extends Transform implements TransformInter
9999
);
100100
} else if (isESMFormat(this.outputOptions.format)) {
101101
const source = new MagicString(code);
102-
// Window scoped references for each key are required to ensure Closure Compilre retains the code.
103102
Object.keys(this.originalExports).forEach(key => {
103+
// Remove export statements before Closure Compiler sees the code
104+
// This prevents CC from transpiling `export` statements when the language_out is set to a value
105+
// where exports were not part of the language.
106+
source.remove(this.originalExports[key].range[0], this.originalExports[key].range[1]);
107+
// Window scoped references for each key are required to ensure Closure Compilre retains the code.
104108
source.append(`\nwindow['${key}'] = ${key}`);
105109
});
106110

src/transformers/parsing-utilities.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,21 @@ export function NamedDeclaration(
7676
[functionName]: {
7777
alias: null,
7878
type: ExportClosureMapping.NAMED_FUNCTION,
79+
range: [
80+
declaration.range ? declaration.range[0] : 0,
81+
declaration.range ? declaration.range[1] : 0,
82+
],
7983
},
8084
};
8185
} else if (className !== null) {
8286
return {
8387
[className]: {
8488
alias: null,
8589
type: ExportClosureMapping.NAMED_CLASS,
90+
range: [
91+
declaration.range ? declaration.range[0] : 0,
92+
declaration.range ? declaration.range[1] : 0,
93+
],
8694
},
8795
};
8896
} else if (declaration.declaration && declaration.declaration.type === 'VariableDeclaration') {
@@ -94,6 +102,10 @@ export function NamedDeclaration(
94102
exportMap[variableDeclarator.id.name] = {
95103
alias: null,
96104
type: ExportClosureMapping.NAMED_CONSTANT,
105+
range: [
106+
declaration.range ? declaration.range[0] : 0,
107+
declaration.range ? declaration.range[1] : 0,
108+
],
97109
};
98110
}
99111
});
@@ -107,6 +119,10 @@ export function NamedDeclaration(
107119
exportMap[exportSpecifier.local.name] = {
108120
alias: null,
109121
type: ExportClosureMapping.DEFAULT,
122+
range: [
123+
declaration.range ? declaration.range[0] : 0,
124+
declaration.range ? declaration.range[1] : 0,
125+
],
110126
};
111127
} else {
112128
exportMap[exportSpecifier.local.name] = {
@@ -115,6 +131,10 @@ export function NamedDeclaration(
115131
? exportSpecifier.exported.name
116132
: null,
117133
type: ExportClosureMapping.NAMED_CONSTANT,
134+
range: [
135+
declaration.range ? declaration.range[0] : 0,
136+
declaration.range ? declaration.range[1] : 0,
137+
],
118138
};
119139
}
120140
});
@@ -137,6 +157,10 @@ export function DefaultDeclaration(
137157
[functionName]: {
138158
alias: null,
139159
type: ExportClosureMapping.NAMED_DEFAULT_FUNCTION,
160+
range: [
161+
declaration.range ? declaration.range[0] : 0,
162+
declaration.range ? declaration.range[1] : 0,
163+
],
140164
},
141165
};
142166
}
@@ -148,6 +172,10 @@ export function DefaultDeclaration(
148172
[className]: {
149173
alias: null,
150174
type: ExportClosureMapping.NAMED_DEFAULT_CLASS,
175+
range: [
176+
declaration.range ? declaration.range[0] : 0,
177+
declaration.range ? declaration.range[1] : 0,
178+
],
151179
},
152180
};
153181
}
@@ -158,6 +186,10 @@ export function DefaultDeclaration(
158186
[declaration.declaration.name]: {
159187
alias: null,
160188
type: ExportClosureMapping.NAMED_DEFAULT_FUNCTION,
189+
range: [
190+
declaration.range ? declaration.range[0] : 0,
191+
declaration.range ? declaration.range[1] : 0,
192+
],
161193
},
162194
};
163195
}
@@ -168,6 +200,10 @@ export function DefaultDeclaration(
168200
[declaration.declaration.name]: {
169201
alias: null,
170202
type: ExportClosureMapping.NAMED_DEFAULT_FUNCTION,
203+
range: [
204+
declaration.range ? declaration.range[0] : 0,
205+
declaration.range ? declaration.range[1] : 0,
206+
],
171207
},
172208
};
173209
}

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export interface ExportNameToClosureMapping {
5858
[key: string]: {
5959
alias: string | null;
6060
type: ExportClosureMapping;
61+
range: [number, number];
6162
};
6263
}
6364

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(document.getElementById(1));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var export1=1;var export2=function(){return 2};export{export1,export2};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default [];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
function a(b){this.name_=b}a.prototype.console=function(){console.log(this.name_)};export default a;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default function(a){console.log(a);console.log(1)};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 1;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
function a(b){this.name_=b}a.prototype.console=function(){console.log(this.name_)};export default a;

0 commit comments

Comments
 (0)