Skip to content

Commit 968a865

Browse files
Clarified IIFE Wrapper Derived Configuration (#7)
1 parent cc65857 commit 968a865

File tree

6 files changed

+82
-11
lines changed

6 files changed

+82
-11
lines changed

src/index.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,25 @@ export const defaultCompileOptions = (outputOptions: OutputOptions): CompileOpti
2828
// - When Rollup is configured to output an iife, ensure Closure Compiler does not
2929
// mangle the name of the iife wrapper.
3030

31+
let externs: string = `
32+
/**
33+
* @fileoverview Externs built via derived configuration from Rollup or input code.
34+
* @externs
35+
*/`;
36+
3137
const options: CompileOptions = {
3238
language_out: 'NO_TRANSPILE',
3339
assume_function_wrapper: outputOptions.format === 'es' ? true : false,
3440
warning_level: 'QUIET',
3541
};
3642
if (outputOptions.format === 'iife' && outputOptions.name) {
37-
options['externs'] = sync(`function ${outputOptions.name}(){}`);
43+
externs = `${externs}\nvar ${outputOptions.name} = function(){};`;
44+
45+
options['externs'] = sync(externs);
3846
}
3947

4048
return options;
41-
}
49+
};
4250

4351
export default function closureCompiler(compileOptions: CompileOptions = {}): Plugin {
4452
return {
@@ -58,11 +66,7 @@ export default function closureCompiler(compileOptions: CompileOptions = {}): Pl
5866
});
5967

6068
const compile: Promise<string> = new Promise((resolve, reject) => {
61-
new compiler(compileOptions).run((
62-
exitCode: number,
63-
stdOut: string,
64-
stdErr: string,
65-
) => {
69+
new compiler(compileOptions).run((exitCode: number, stdOut: string, stdErr: string) => {
6670
if (exitCode !== 0) {
6771
reject(new Error(`Google Closure Compiler exit ${exitCode}: ${stdErr}`));
6872
} else {

test/iife-wrapped-safely.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright 2018 The AMP HTML Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS-IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import test from 'ava';
18+
import compiler from '../dist/index';
19+
import { rollup } from 'rollup';
20+
import { readFileSync } from 'fs';
21+
import { join } from 'path';
22+
23+
test('preserves iife wrapper name', async t => {
24+
const minifiedBundle = readFileSync(join('test/input/iife-wrapped-minified.js'), 'utf8');
25+
const compilerBundle = await rollup({
26+
input: 'test/input/iife-wrapped.js',
27+
plugins: [
28+
compiler(),
29+
],
30+
});
31+
32+
const compilerResults = await compilerBundle.generate({
33+
format: 'iife',
34+
name: 'wrapper',
35+
sourcemap: true,
36+
});
37+
38+
t.is(compilerResults.code, minifiedBundle);
39+
});

test/input/es5.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
function greeting(name) {
18-
return 'hello ' + name;
17+
function changeContent(name) {
18+
document.body.innerHTML = 'hello ' + name;
1919
}
2020

21-
console.log(greeting('superuser'));
21+
export function greeting(name) {
22+
changeContent(name || 'you');
23+
24+
document.body.addEventListener('click', greeting('superuser'), false);
25+
}

test/input/iife-wrapped-minified.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'use strict';var wrapper=function(a){a.greeting=function(a){document.body.innerHTML="hello "+a};return a}({});

test/input/iife-wrapped.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright 2018 The AMP HTML Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS-IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
export function greeting(name) {
18+
document.body.innerHTML = 'hello ' + name;
19+
}

test/rollup-config-to-flags.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ test('when rollup configuration specifies format iife with a name, an extern is
3636
t.not(options.externs, undefined);
3737

3838
const externs = readFileSync(options.externs, 'utf8');
39-
t.is(externs, `function Wrapper(){}`);
39+
t.is(externs, `
40+
/**
41+
* @fileoverview Externs built via derived configuration from Rollup or input code.
42+
* @externs
43+
*/\nvar Wrapper = function(){};`);
4044
});
4145

4246
test('when rollup configuration specifies format es, assume_function_wrapper is true', t => {

0 commit comments

Comments
 (0)