This repository has been archived by the owner on Apr 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 156
/
build-optimizer.ts
85 lines (74 loc) · 3.2 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
/**
* @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 } from '../transforms/import-tslib';
import { getPrefixFunctionsTransformer } from '../transforms/prefix-functions';
import { getScrubFileTransformer } from '../transforms/scrub-file';
const hasDecorators = /decorators/;
const hasCtorParameters = /ctorParameters/;
const hasTsHelpers = /var (__extends|__decorate|__metadata|__param) = /;
const isAngularModuleFile = /\.es5\.js$/;
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(\\|\/)/,
];
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 (hasTsHelpers.test(content)) {
getTransforms.push(getImportTslibTransformer);
}
if (inputFilePath
&& isAngularModuleFile.test(inputFilePath)
&& whitelistedAngularModules.some((re) => re.test(inputFilePath))
) {
getTransforms.push(
// getPrefixFunctionsTransformer is rather dangerous, apply only to known pure 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 (hasDecorators.test(content) || hasCtorParameters.test(content)) {
getTransforms.push(
getScrubFileTransformer,
getFoldFileTransformer,
);
}
return transformJavascript({ ...options, getTransforms, content });
}