UNPKG

4.52 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.buildOptimizer = void 0;
11const fs_1 = require("fs");
12const transform_javascript_1 = require("../helpers/transform-javascript");
13const prefix_classes_1 = require("../transforms/prefix-classes");
14const prefix_functions_1 = require("../transforms/prefix-functions");
15const scrub_file_1 = require("../transforms/scrub-file");
16const wrap_enums_1 = require("../transforms/wrap-enums");
17// Angular packages are known to have no side effects.
18const 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// Known locations for the source files of @angular/core.
36const coreFilesRegex = /[\\/]node_modules[\\/]@angular[\\/]core[\\/][f]?esm2015[\\/]/;
37function isKnownCoreFile(filePath) {
38 return coreFilesRegex.test(filePath);
39}
40function isKnownSideEffectFree(filePath) {
41 // rxjs add imports contain intentional side effects
42 if (/[\\/]node_modules[\\/]rxjs[\\/]add[\\/]/.test(filePath)) {
43 return false;
44 }
45 return knownSideEffectFreeAngularModules.some((re) => re.test(filePath));
46}
47function 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 // Determine which transforms to apply.
71 const getTransforms = [];
72 let typeCheck = false;
73 if (hasSafeSideEffects) {
74 // Angular modules have known safe side effects
75 getTransforms.push(
76 // getPrefixFunctionsTransformer is rather dangerous, apply only to known pure es5 modules.
77 // It will mark both `require()` calls and `console.log(stuff)` as pure.
78 // We only apply it to modules known to be side effect free, since we know they are safe.
79 prefix_functions_1.getPrefixFunctionsTransformer);
80 typeCheck = true;
81 }
82 else if (prefix_classes_1.testPrefixClasses(content)) {
83 // This is only relevant if prefix functions is not used since prefix functions will prefix IIFE wrapped classes.
84 getTransforms.unshift(prefix_classes_1.getPrefixClassesTransformer);
85 }
86 if (scrub_file_1.testScrubFile(content)) {
87 // Always test as these require the type checker
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}
103exports.buildOptimizer = buildOptimizer;