Skip to content

Commit a8f8794

Browse files
Andaristkristoferbaxter
authored andcommitted
Support esm output format alias (#10)
1 parent 381e7c5 commit a8f8794

File tree

5 files changed

+74
-52
lines changed

5 files changed

+74
-52
lines changed

src/options.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,20 @@
1515
*/
1616

1717
import { Transform } from './types';
18-
import { OutputOptions } from 'rollup';
18+
import { ModuleFormat, OutputOptions } from 'rollup';
1919
import { CompileOptions } from 'google-closure-compiler';
2020
import { sync } from 'temp-write';
2121

22+
/**
23+
* Checks if output format is ESM
24+
* @param format
25+
* @return boolean
26+
*/
27+
export const isESMFormat = (format?: ModuleFormat | 'esm'): boolean => {
28+
// TODO: remove `| 'esm'` when rollup upgrades its typings
29+
return format === 'esm' || format === 'es';
30+
};
31+
2232
/**
2333
* Generate default Closure Compiler CompileOptions an author can override if they wish.
2434
* These must be derived from configuration or input sources.
@@ -44,7 +54,7 @@ export const defaults = (
4454

4555
return {
4656
language_out: 'NO_TRANSPILE',
47-
assume_function_wrapper: options.format === 'es' ? true : false,
57+
assume_function_wrapper: isESMFormat(options.format) ? true : false,
4858
warning_level: 'QUIET',
4959
externs,
5060
};

src/transformers/exports.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import { ModuleDeclaration, ExportNamedDeclaration, ExportDefaultDeclaration } from 'estree';
1818
import { TransformSourceDescription, OutputOptions } from 'rollup';
1919
import { NamedDeclaration, DefaultDeclaration } from './parsing-utilities';
20+
import { isESMFormat } from '../options';
2021
import {
2122
ExportNameToClosureMapping,
2223
ALL_EXPORT_TYPES,
@@ -30,13 +31,13 @@ import {
3031

3132
const HEADER = `/**
3233
* @fileoverview Externs built via derived configuration from Rollup or input code.
33-
* This extern contains top level exported members.
34+
* This extern contains top level exported members.
3435
* @externs
3536
*/
3637
`;
3738

3839
/**
39-
* This Transform will apply only if the Rollup configuration is for 'es' output.
40+
* This Transform will apply only if the Rollup configuration is for 'esm' output.
4041
*
4142
* In order to preserve the export statements:
4243
* 1. Create extern definitions for them (to keep them their names from being mangled).
@@ -48,7 +49,7 @@ export default class ExportTransform extends Transform implements TransformInter
4849

4950
public extern(options: OutputOptions): string {
5051
let content = HEADER;
51-
if (options.format === 'es') {
52+
if (isESMFormat(options.format)) {
5253
Object.keys(this.exported).forEach(key => {
5354
content += `window['${key}'] = ${key};\n`;
5455
});
@@ -123,7 +124,7 @@ export default class ExportTransform extends Transform implements TransformInter
123124
this.context.warn(
124125
'Rollup Plugin Closure Compiler, OutputOptions not known before Closure Compiler invocation.',
125126
);
126-
} else if (this.outputOptions.format === 'es') {
127+
} else if (isESMFormat(this.outputOptions.format)) {
127128
Object.keys(this.exported).forEach(key => {
128129
code += `\nwindow['${key}'] = ${key}`;
129130
});
@@ -147,7 +148,7 @@ export default class ExportTransform extends Transform implements TransformInter
147148
this.context.warn(
148149
'Rollup Plugin Closure Compiler, OutputOptions not known before Closure Compiler invocation.',
149150
);
150-
} else if (this.outputOptions.format === 'es') {
151+
} else if (isESMFormat(this.outputOptions.format)) {
151152
const exportedConstants: Array<string> = [];
152153

153154
Object.keys(this.exported).forEach(key => {

src/transformers/strict.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import { Transform } from '../types';
18+
import { isESMFormat } from '../options';
1819
import { TransformSourceDescription } from 'rollup';
1920

2021
const STRICT_MODE_DECLARATION = `'use strict';`;
@@ -33,7 +34,7 @@ export default class StrictTransform extends Transform {
3334
this.context.warn(
3435
'Rollup Plugin Closure Compiler, OutputOptions not known before Closure Compiler invocation.',
3536
);
36-
} else if (this.outputOptions.format === 'es' && code.startsWith(STRICT_MODE_DECLARATION)) {
37+
} else if (isESMFormat(this.outputOptions.format) && code.startsWith(STRICT_MODE_DECLARATION)) {
3738
// This will only remove the top level 'use strict' directive since we cannot
3839
// be certain source does not contain strings with the intended content.
3940
code = code.slice(STRICT_MODE_DECLARATION_LENGTH, code.length);

test/esmodules/esmodules.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,23 @@ import { promisify } from 'util';
2323

2424
const readFile = promisify(fs.readFile);
2525

26-
test('esm does minify', async t => {
27-
const source = await readFile(join('test/esmodules/fixtures/esm.js'), 'utf8');
28-
const compilerBundle = await rollup({
29-
input: 'test/esmodules/fixtures/esm.js',
30-
plugins: [compiler()],
31-
});
32-
const compilerResults = await compilerBundle.generate({
33-
format: 'es',
34-
sourcemap: true,
26+
const createTests = format => {
27+
test(`${format} - does minify`, async t => {
28+
const source = await readFile(join('test/esmodules/fixtures/esm.js'), 'utf8');
29+
const compilerBundle = await rollup({
30+
input: 'test/esmodules/fixtures/esm.js',
31+
plugins: [compiler()],
32+
});
33+
const compilerResults = await compilerBundle.generate({
34+
format,
35+
sourcemap: true,
36+
});
37+
38+
t.truthy(compilerResults.code.length < source.length);
3539
});
40+
}
3641

37-
t.truthy(compilerResults.code.length < source.length);
38-
});
42+
createTests('esm');
43+
createTests('es');
3944

4045
// TODO(KB): Tests verifying exported code contains the correct exported members via acorn AST parse.

test/esmodules/import.js

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,47 +23,52 @@ import { promisify } from 'util';
2323

2424
const readFile = promisify(fs.readFile);
2525

26-
async function input(input) {
27-
const bundle = await rollup.rollup({
28-
input: `test/esmodules/fixtures/${input}.js`,
29-
plugins: [compiler()],
30-
});
26+
const createTests = format => {
27+
async function input(input) {
28+
const bundle = await rollup.rollup({
29+
input: `test/esmodules/fixtures/${input}.js`,
30+
plugins: [compiler()],
31+
});
3132

32-
return {
33-
minified: await readFile(join(`test/esmodules/fixtures/${input}.minified.js`), 'utf8'),
34-
code: (await bundle.generate({
35-
format: 'es',
36-
sourcemap: true,
37-
})).code,
38-
};
39-
}
33+
return {
34+
minified: await readFile(join(`test/esmodules/fixtures/${input}.minified.js`), 'utf8'),
35+
code: (await bundle.generate({
36+
format,
37+
sourcemap: true,
38+
})).code,
39+
};
40+
}
4041

41-
test('input with import exports the only exported function', async t => {
42-
const { minified, code } = await input('importer');
42+
test(`${format} - input with import exports the only exported function`, async t => {
43+
const { minified, code } = await input('importer');
4344

44-
t.is(code, minified);
45-
});
45+
t.is(code, minified);
46+
});
4647

47-
test('export default named function exports the default named function', async t => {
48-
const { minified, code } = await input('export-default');
48+
test(`${format} - export default named function exports the default named function`, async t => {
49+
const { minified, code } = await input('export-default');
4950

50-
t.is(code, minified);
51-
});
51+
t.is(code, minified);
52+
});
5253

53-
test('export const values exports the const values', async t => {
54-
const { minified, code } = await input('export-const');
54+
test(`${format} - export const values exports the const values`, async t => {
55+
const { minified, code } = await input('export-const');
5556

56-
t.is(code, minified);
57-
});
57+
t.is(code, minified);
58+
});
5859

59-
test('export class exports the class', async t => {
60-
const { minified, code } = await input('export-class');
60+
test(`${format} - export class exports the class`, async t => {
61+
const { minified, code } = await input('export-class');
6162

62-
t.is(code, minified);
63-
});
63+
t.is(code, minified);
64+
});
65+
66+
test(`${format} - export default class exports the class`, async t => {
67+
const { minified, code } = await input('export-default-class');
6468

65-
test('export default class exports the class', async t => {
66-
const { minified, code } = await input('export-default-class');
69+
t.is(code, minified);
70+
});
71+
}
6772

68-
t.is(code, minified);
69-
});
73+
createTests('esm');
74+
createTests('es');

0 commit comments

Comments
 (0)