This repository was archived by the owner on Apr 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathbuild-optimizer.ts
101 lines (87 loc) · 3.64 KB
/
build-optimizer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { readFileSync } from 'fs';
import { TransformJavascriptOutput, transformJavascript } from '../helpers/transform-javascript';
import { getFoldFileTransformer } from '../transforms/class-fold';
import { getImportTslibTransformer, testImportTslib } from '../transforms/import-tslib';
import { getPrefixClassesTransformer, testPrefixClasses } from '../transforms/prefix-classes';
import { getPrefixFunctionsTransformer } from '../transforms/prefix-functions';
import { getScrubFileTransformer, testScrubFile } from '../transforms/scrub-file';
import { getWrapEnumsTransformer, testWrapEnums } from '../transforms/wrap-enums';
const whitelistedAngularModules = [
/[\\\/]node_modules[\\\/]@angular[\\\/]animations[\\\/]/,
/[\\\/]node_modules[\\\/]@angular[\\\/]common[\\\/]/,
/[\\\/]node_modules[\\\/]@angular[\\\/]compiler[\\\/]/,
/[\\\/]node_modules[\\\/]@angular[\\\/]core[\\\/]/,
/[\\\/]node_modules[\\\/]@angular[\\\/]forms[\\\/]/,
/[\\\/]node_modules[\\\/]@angular[\\\/]http[\\\/]/,
/[\\\/]node_modules[\\\/]@angular[\\\/]platform-browser-dynamic[\\\/]/,
/[\\\/]node_modules[\\\/]@angular[\\\/]platform-browser[\\\/]/,
/[\\\/]node_modules[\\\/]@angular[\\\/]platform-webworker-dynamic[\\\/]/,
/[\\\/]node_modules[\\\/]@angular[\\\/]platform-webworker[\\\/]/,
/[\\\/]node_modules[\\\/]@angular[\\\/]router[\\\/]/,
/[\\\/]node_modules[\\\/]@angular[\\\/]upgrade[\\\/]/,
/[\\\/]node_modules[\\\/]@angular[\\\/]material[\\\/]/,
/[\\\/]node_modules[\\\/]@angular[\\\/]cdk[\\\/]/,
];
const es5AngularModules = [
// Angular 4 packaging format has .es5.js as the extension.
/.es5.js$/, // Angular 4
// Angular 5 has esm5 folders.
/[\\\/]node_modules[\\\/]@angular[\\\/][^\\/][\\\/]esm5[\\\/]/,
// All Angular versions have UMD with es5.
/.umd.js$/,
];
export interface BuildOptimizerOptions {
content?: string;
inputFilePath?: string;
outputFilePath?: string;
emitSourceMap?: boolean;
strict?: boolean;
}
export function buildOptimizer(options: BuildOptimizerOptions): TransformJavascriptOutput {
const { inputFilePath } = options;
let { content } = options;
if (!inputFilePath && content === undefined) {
throw new Error('Either filePath or content must be specified in options.');
}
if (content === undefined) {
content = readFileSync(inputFilePath as string, 'UTF-8');
}
// Determine which transforms to apply.
const getTransforms = [];
if (testWrapEnums(content)) {
getTransforms.push(getWrapEnumsTransformer);
}
if (testImportTslib(content)) {
getTransforms.push(getImportTslibTransformer);
}
if (testPrefixClasses(content)) {
getTransforms.push(getPrefixClassesTransformer);
}
if (inputFilePath
&& whitelistedAngularModules.some((re) => re.test(inputFilePath))
&& es5AngularModules.some((re) => re.test(inputFilePath))
) {
getTransforms.push(
// getPrefixFunctionsTransformer is rather dangerous, apply only to known pure es5 modules.
// It will mark both `require()` calls and `console.log(stuff)` as pure.
// We only apply it to whitelisted modules, since we know they are safe.
// getPrefixFunctionsTransformer needs to be before getFoldFileTransformer.
getPrefixFunctionsTransformer,
getScrubFileTransformer,
getFoldFileTransformer,
);
} else if (testScrubFile(content)) {
getTransforms.push(
getScrubFileTransformer,
getFoldFileTransformer,
);
}
return transformJavascript({ ...options, getTransforms, content });
}