Skip to content

Commit 0d7cd45

Browse files
Fix iife extern and tests (#254)
1 parent afa73ee commit 0d7cd45

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

src/options.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { Transform } from './types';
1818
import { ModuleFormat, OutputOptions } from 'rollup';
1919
import { CompileOptions } from 'google-closure-compiler';
2020
import { sync } from 'temp-write';
21-
import { logSource } from './debug';
21+
import { log, logSource } from './debug';
2222

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.';
@@ -78,11 +78,13 @@ export const defaults = (
7878
.filter(Boolean)
7979
.concat(providedExterns)
8080
: providedExterns.length > 0
81-
? providedExterns
82-
: '';
81+
? providedExterns
82+
: '';
8383

8484
if (typeof externs === 'string' && externs !== '') {
8585
logSource('externs', externs);
86+
} else if (Array.isArray(externs)) {
87+
log('externs', externs);
8688
}
8789

8890
return {

src/transformers/iife.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import { Transform } from '../types';
1818
import { OutputOptions } from 'rollup';
1919

2020
const HEADER = `/**
21-
* @fileoverview Externs built via derived configuration from Rollup or input code.
22-
* This extern contains the iife name so it does not get mangled at the top level.
23-
* @externs
24-
*/
21+
* @fileoverview Externs built via derived configuration from Rollup or input code.
22+
* This extern contains the iife name so it does not get mangled at the top level.
23+
* @externs
24+
*/
2525
`;
2626

2727
/**
@@ -35,7 +35,7 @@ export default class IifeTransform extends Transform {
3535

3636
public extern(options: OutputOptions): string {
3737
if (options.format === 'iife' && options.name) {
38-
return HEADER + `function ${options.name}(){};\n`;
38+
return HEADER + `window['${options.name}'] = ${options.name};\n`;
3939
}
4040

4141
return '';

test/iife/fixtures/iife.extern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
* This extern contains the iife name so it does not get mangled at the top level.
44
* @externs
55
*/
6-
function wrapper(){};
6+
window['wrapper'] = wrapper;

test/iife/iife-wrapped-safely.test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ test('generate extern for iife name', async t => {
3333
const transforms = createTransforms({});
3434
const options = defaults(outputOptions, [], transforms);
3535

36-
const contentMatch = options.externs.some(async externFilePath => {
36+
for (const externFilePath of options.externs) {
3737
const fileContent = await fsPromises.readFile(externFilePath, 'utf8');
38-
return fileContent === externFixtureContent;
39-
});
40-
41-
t.is(contentMatch, true);
38+
if (fileContent === externFixtureContent) {
39+
t.pass();
40+
return;
41+
}
42+
}
43+
t.fail('None of the externs match the expected format');
4244
});
4345

4446
generator('iife', 'iife-wrapped-safely', undefined, ['iife'], undefined, 'wrapper');

0 commit comments

Comments
 (0)