Skip to content

Commit 2731853

Browse files
Inline single export from current source (#259)
* Partial support for inlining single export from current source * Fix remainder of tests
1 parent 31042d7 commit 2731853

File tree

65 files changed

+91
-76
lines changed

Some content is hidden

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

65 files changed

+91
-76
lines changed

src/options.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ import { log, logSource } from './debug';
2323
export const ERROR_WARNINGS_ENABLED_LANGUAGE_OUT_UNSPECIFIED =
2424
'Providing the warning_level=VERBOSE compile option also requires a valid language_out compile option.';
2525
export const ERROR_WARNINGS_ENABLED_LANGUAGE_OUT_INVALID =
26-
'Providing the warning_level=VERBOSE and language_out=NO_TRANSPILE compile option will remove warnings.';
26+
'Providing the warning_level=VERBOSE and language_out=NO_TRANSPILE compile options will remove warnings.';
2727

2828
/**
2929
* Checks if output format is ESM
3030
* @param format
3131
* @return boolean
3232
*/
33-
export const isESMFormat = (format?: ModuleFormat | 'esm'): boolean => {
33+
export const isESMFormat = (format?: ModuleFormat): boolean => {
3434
// TODO: remove `| 'esm'` when rollup upgrades its typings
3535
return format === 'esm' || format === 'es';
3636
};
@@ -64,10 +64,8 @@ export const defaults = (
6464
// Defaults for Rollup Projects are slightly different than Closure Compiler defaults.
6565
// - Users of Rollup tend to transpile their code before handing it to a minifier,
6666
// so no transpile is default.
67-
// - When Rollup output is set to "es" it is expected the code will live in a ES Module,
67+
// - When Rollup output is set to "es|esm" it is expected the code will live in a ES Module,
6868
// so safely be more aggressive in minification.
69-
// - When Rollup is configured to output an iife, ensure Closure Compiler does not
70-
// mangle the name of the iife wrapper.
7169

7270
const externs = transformers
7371
? transformers
@@ -139,5 +137,7 @@ export default function(
139137
create_source_map: mapFile,
140138
};
141139

140+
log('compile options', options);
141+
142142
return [options, mapFile];
143143
}

src/transformers/exports.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,19 @@ var exports;`;
5151
export default class ExportTransform extends Transform implements TransformInterface {
5252
public name = 'ExportTransform';
5353
private originalExports: Map<string, ExportDetails> = new Map();
54+
private currentSourceExportCount: number = 0;
5455

5556
/**
5657
* Store an export from a source into the originalExports Map.
5758
* @param mapping mapping of details from this declaration.
5859
*/
5960
private storeExport = (mapping: Array<ExportDetails>): void =>
60-
mapping.forEach(map => this.originalExports.set(map.closureName, map));
61+
mapping.forEach(map => {
62+
if (map.source === null) {
63+
this.currentSourceExportCount++;
64+
}
65+
this.originalExports.set(map.closureName, map);
66+
});
6167

6268
private static storeExportToAppend(
6369
collected: Map<string | null, Array<string>>,
@@ -173,7 +179,7 @@ export default class ExportTransform extends Transform implements TransformInter
173179
const source = new MagicString(code);
174180
const program = parse(code);
175181
let collectedExportsToAppend: Map<string | null, Array<string>> = new Map();
176-
const { originalExports } = this;
182+
const { originalExports, currentSourceExportCount } = this;
177183

178184
source.trimEnd();
179185

@@ -218,10 +224,17 @@ export default class ExportTransform extends Transform implements TransformInter
218224
}
219225
break;
220226
case ExportClosureMapping.NAMED_CONSTANT:
221-
if (exportDetails.source === null) {
227+
const exportFromCurrentSource = exportDetails.source === null;
228+
const inlineExport =
229+
exportFromCurrentSource && currentSourceExportCount === 1;
230+
// KRIS YOU'RE HERE!
231+
// When there is only a single export from the current source (source=null)
232+
// then inline the export statement instead of sending it into the collection for appending.
233+
if (exportFromCurrentSource) {
222234
const { object: leftObject } = ancestor.expression.left;
223235
if (leftObject.range) {
224-
source.overwrite(leftObject.range[0], leftObject.range[1] + 1, 'var ');
236+
const statement = inlineExport ? 'export var ' : 'var ';
237+
source.overwrite(leftObject.range[0], leftObject.range[1] + 1, statement);
225238
}
226239
if (exportDetails.local !== exportDetails.exported) {
227240
exportDetails.local = exportDetails.exported;
@@ -237,10 +250,12 @@ export default class ExportTransform extends Transform implements TransformInter
237250
);
238251
}
239252

240-
collectedExportsToAppend = ExportTransform.storeExportToAppend(
241-
collectedExportsToAppend,
242-
exportDetails,
243-
);
253+
if (!inlineExport) {
254+
collectedExportsToAppend = ExportTransform.storeExportToAppend(
255+
collectedExportsToAppend,
256+
exportDetails,
257+
);
258+
}
244259
break;
245260
}
246261
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
var multipleArguments=(a,b)=>console.log(a,b);export{multipleArguments};
1+
export var multipleArguments=(a,b)=>console.log(a,b);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
var multipleArguments=(a,b)=>console.log(a,b);export{multipleArguments};
1+
export var multipleArguments=(a,b)=>console.log(a,b);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
var multipleArguments=function(a,b){return console.log(a,b)};export{multipleArguments};
1+
export var multipleArguments=function(a,b){return console.log(a,b)};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
var singleArgument=a=>console.log(a);export{singleArgument};
1+
export var singleArgument=a=>console.log(a);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
var singleArgument=a=>console.log(a);export{singleArgument};
1+
export var singleArgument=a=>console.log(a);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
var singleArgument=function(a){return console.log(a)};export{singleArgument};
1+
export var singleArgument=function(a){return console.log(a)};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
let b=[3,4],c=[5,6];var yes=function(a){return 0<=c.indexOf(a)&&0<=b.indexOf(a)};export{yes};
1+
let b=[3,4],c=[5,6];export var yes=function(a){return 0<=c.indexOf(a)&&0<=b.indexOf(a)};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
let b=[3,4],c=[5,6];var yes=function(a){return 0<=c.indexOf(a)&&0<=b.indexOf(a)};export{yes};
1+
let b=[3,4],c=[5,6];export var yes=function(a){return 0<=c.indexOf(a)&&0<=b.indexOf(a)};

0 commit comments

Comments
 (0)