Skip to content

Commit be1e31a

Browse files
Continued refactoring to simplify code
1 parent a586dee commit be1e31a

File tree

6 files changed

+27
-35
lines changed

6 files changed

+27
-35
lines changed

src/options.ts

Lines changed: 12 additions & 21 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 { log, logSource } from './debug';
21+
import { log } 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.';
@@ -39,15 +39,16 @@ export const isESMFormat = (format?: ModuleFormat): boolean => {
3939
* Throw Errors if compile options will result in unexpected behaviour.
4040
* @param compileOptions
4141
*/
42-
const validateCompileOptions = (compileOptions: CompileOptions): void => {
42+
function validateCompileOptions(compileOptions: CompileOptions): void {
4343
if ('warning_level' in compileOptions && compileOptions.warning_level === 'VERBOSE') {
4444
if (!('language_out' in compileOptions)) {
4545
throw new Error(ERROR_WARNINGS_ENABLED_LANGUAGE_OUT_UNSPECIFIED);
46-
} else if ('language_out' in compileOptions && compileOptions.language_out === 'NO_TRANSPILE') {
46+
}
47+
if (compileOptions.language_out === 'NO_TRANSPILE') {
4748
throw new Error(ERROR_WARNINGS_ENABLED_LANGUAGE_OUT_INVALID);
4849
}
4950
}
50-
};
51+
}
5152

5253
/**
5354
* Generate default Closure Compiler CompileOptions an author can override if they wish.
@@ -67,30 +68,20 @@ export const defaults = (
6768
// - When Rollup output is set to "es|esm" it is expected the code will live in a ES Module,
6869
// so safely be more aggressive in minification.
6970

70-
const externs = transformers
71-
? transformers
72-
.map(transform => {
73-
const extern = transform.extern(options);
74-
return extern !== '' ? sync(extern) : false;
75-
})
76-
.filter(Boolean)
77-
.concat(providedExterns)
78-
: providedExterns.length > 0
79-
? providedExterns
80-
: '';
81-
82-
if (typeof externs === 'string' && externs !== '') {
83-
logSource('externs', externs);
84-
} else if (Array.isArray(externs)) {
85-
log('externs', externs);
71+
const transformerExterns: Array<string> = [];
72+
for (const transform of transformers || []) {
73+
const extern = transform.extern(options);
74+
if (extern !== null) {
75+
transformerExterns.push(sync(extern));
76+
}
8677
}
8778

8879
return {
8980
language_out: 'NO_TRANSPILE',
9081
assume_function_wrapper: isESMFormat(options.format),
9182
warning_level: 'QUIET',
9283
module_resolution: 'NODE',
93-
externs,
84+
externs: transformerExterns.concat(providedExterns),
9485
};
9586
};
9687

src/transformers/cjs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ var exports;`;
3838
export default class CJSTransform extends Transform {
3939
public name = 'CJSTransform';
4040

41-
public extern(options: OutputOptions): string {
41+
public extern(options: OutputOptions): string | null {
4242
if (options.format === 'cjs') {
4343
return HEADER;
4444
}
4545

46-
return '';
46+
return null;
4747
}
4848
}

src/transformers/exports.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export default class ExportTransform extends Transform implements TransformInter
102102
});
103103
}
104104

105-
public extern(): string {
105+
public extern(): string | null {
106106
if (Array.from(this.originalExports.keys()).length > 0) {
107107
let output = EXTERN_OVERVIEW;
108108

@@ -116,7 +116,7 @@ export default class ExportTransform extends Transform implements TransformInter
116116
return output;
117117
}
118118

119-
return '';
119+
return null;
120120
}
121121

122122
/**

src/transformers/iife.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ const HEADER = `/**
3333
export default class IifeTransform extends Transform {
3434
public name = 'IifeTransform';
3535

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

41-
return '';
41+
return null;
4242
}
4343
}

src/transformers/imports.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,13 @@ export default class ImportTransform extends Transform {
6666
* Otherwise, advanced mode compilation will fail since the reference is unknown.
6767
* @return string representing content of generated extern.
6868
*/
69-
public extern(): string {
70-
let extern = HEADER;
69+
public extern(): string | null {
70+
let extern: string = HEADER;
71+
7172
if (this.importedExternalsLocalNames.length > 0) {
72-
this.importedExternalsLocalNames.forEach(name => {
73+
for (const name of this.importedExternalsLocalNames) {
7374
extern += `function ${name}(){};\n`;
74-
});
75+
}
7576
}
7677

7778
if (this.dynamicImportPresent) {
@@ -84,7 +85,7 @@ function ${DYNAMIC_IMPORT_REPLACEMENT}(path) { return Promise.resolve(path) };
8485
window['${DYNAMIC_IMPORT_REPLACEMENT}'] = ${DYNAMIC_IMPORT_REPLACEMENT};`;
8586
}
8687

87-
return extern;
88+
return extern === HEADER ? null : extern;
8889
}
8990

9091
/**

src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export interface ExportDetails {
6262
export type TransformMethod = (code: string) => Promise<TransformSourceDescription>;
6363
export interface TransformInterface {
6464
name: string;
65-
extern: (options: OutputOptions) => string;
65+
extern: (options: OutputOptions) => string | null;
6666
preCompilation: TransformMethod;
6767
postCompilation: TransformMethod;
6868
}
@@ -77,8 +77,8 @@ export class Transform implements TransformInterface {
7777
this.inputOptions = inputOptions;
7878
}
7979

80-
public extern(options: OutputOptions): string {
81-
return '';
80+
public extern(options: OutputOptions): string | null {
81+
return null;
8282
}
8383

8484
public async preCompilation(code: string): Promise<TransformSourceDescription> {

0 commit comments

Comments
 (0)