1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | exports.buildOptimizer = void 0;
|
11 | const fs_1 = require("fs");
|
12 | const transform_javascript_1 = require("../helpers/transform-javascript");
|
13 | const prefix_classes_1 = require("../transforms/prefix-classes");
|
14 | const prefix_functions_1 = require("../transforms/prefix-functions");
|
15 | const scrub_file_1 = require("../transforms/scrub-file");
|
16 | const wrap_enums_1 = require("../transforms/wrap-enums");
|
17 |
|
18 | const knownSideEffectFreeAngularModules = [
|
19 | /[\\/]node_modules[\\/]@angular[\\/]animations[\\/]/,
|
20 | /[\\/]node_modules[\\/]@angular[\\/]common[\\/]/,
|
21 | /[\\/]node_modules[\\/]@angular[\\/]compiler[\\/]/,
|
22 | /[\\/]node_modules[\\/]@angular[\\/]core[\\/]/,
|
23 | /[\\/]node_modules[\\/]@angular[\\/]forms[\\/]/,
|
24 | /[\\/]node_modules[\\/]@angular[\\/]http[\\/]/,
|
25 | /[\\/]node_modules[\\/]@angular[\\/]platform-browser-dynamic[\\/]/,
|
26 | /[\\/]node_modules[\\/]@angular[\\/]platform-browser[\\/]/,
|
27 | /[\\/]node_modules[\\/]@angular[\\/]platform-webworker-dynamic[\\/]/,
|
28 | /[\\/]node_modules[\\/]@angular[\\/]platform-webworker[\\/]/,
|
29 | /[\\/]node_modules[\\/]@angular[\\/]router[\\/]/,
|
30 | /[\\/]node_modules[\\/]@angular[\\/]upgrade[\\/]/,
|
31 | /[\\/]node_modules[\\/]@angular[\\/]material[\\/]/,
|
32 | /[\\/]node_modules[\\/]@angular[\\/]cdk[\\/]/,
|
33 | /[\\/]node_modules[\\/]rxjs[\\/]/,
|
34 | ];
|
35 |
|
36 | const coreFilesRegex = /[\\/]node_modules[\\/]@angular[\\/]core[\\/][f]?esm2015[\\/]/;
|
37 | function isKnownCoreFile(filePath) {
|
38 | return coreFilesRegex.test(filePath);
|
39 | }
|
40 | function isKnownSideEffectFree(filePath) {
|
41 |
|
42 | if (/[\\/]node_modules[\\/]rxjs[\\/]add[\\/]/.test(filePath)) {
|
43 | return false;
|
44 | }
|
45 | return knownSideEffectFreeAngularModules.some((re) => re.test(filePath));
|
46 | }
|
47 | function buildOptimizer(options) {
|
48 | const { inputFilePath } = options;
|
49 | let { originalFilePath, content, isAngularCoreFile } = options;
|
50 | if (!originalFilePath && inputFilePath) {
|
51 | originalFilePath = inputFilePath;
|
52 | }
|
53 | if (!inputFilePath && content === undefined) {
|
54 | throw new Error('Either filePath or content must be specified in options.');
|
55 | }
|
56 | if (content === undefined) {
|
57 | content = fs_1.readFileSync(inputFilePath, 'UTF-8');
|
58 | }
|
59 | if (!content) {
|
60 | return {
|
61 | content: null,
|
62 | sourceMap: null,
|
63 | emitSkipped: true,
|
64 | };
|
65 | }
|
66 | if (isAngularCoreFile === undefined) {
|
67 | isAngularCoreFile = !!originalFilePath && isKnownCoreFile(originalFilePath);
|
68 | }
|
69 | const hasSafeSideEffects = originalFilePath && isKnownSideEffectFree(originalFilePath);
|
70 |
|
71 | const getTransforms = [];
|
72 | let typeCheck = false;
|
73 | if (hasSafeSideEffects) {
|
74 |
|
75 | getTransforms.push(
|
76 |
|
77 |
|
78 |
|
79 | prefix_functions_1.getPrefixFunctionsTransformer);
|
80 | typeCheck = true;
|
81 | }
|
82 | else if (prefix_classes_1.testPrefixClasses(content)) {
|
83 |
|
84 | getTransforms.unshift(prefix_classes_1.getPrefixClassesTransformer);
|
85 | }
|
86 | if (scrub_file_1.testScrubFile(content)) {
|
87 |
|
88 | getTransforms.push(scrub_file_1.createScrubFileTransformerFactory(isAngularCoreFile));
|
89 | typeCheck = true;
|
90 | }
|
91 | getTransforms.push(wrap_enums_1.getWrapEnumsTransformer);
|
92 | const transformJavascriptOpts = {
|
93 | content: content,
|
94 | inputFilePath: options.inputFilePath,
|
95 | outputFilePath: options.outputFilePath,
|
96 | emitSourceMap: options.emitSourceMap,
|
97 | strict: options.strict,
|
98 | getTransforms,
|
99 | typeCheck,
|
100 | };
|
101 | return transform_javascript_1.transformJavascript(transformJavascriptOpts);
|
102 | }
|
103 | exports.buildOptimizer = buildOptimizer;
|