"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * @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
 */
const fs_1 = require("fs");
const transform_javascript_1 = require("../helpers/transform-javascript");
const class_fold_1 = require("../transforms/class-fold");
const import_tslib_1 = require("../transforms/import-tslib");
const prefix_classes_1 = require("../transforms/prefix-classes");
const prefix_functions_1 = require("../transforms/prefix-functions");
const scrub_file_1 = require("../transforms/scrub-file");
const wrap_enums_1 = require("../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(\\|\/)/,
];
function buildOptimizer(options) {
    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 = fs_1.readFileSync(inputFilePath, 'UTF-8');
    }
    // Determine which transforms to apply.
    const getTransforms = [];
    if (wrap_enums_1.testWrapEnums(content)) {
        getTransforms.push(wrap_enums_1.getWrapEnumsTransformer);
    }
    if (import_tslib_1.testImportTslib(content)) {
        getTransforms.push(import_tslib_1.getImportTslibTransformer);
    }
    if (prefix_classes_1.testPrefixClasses(content)) {
        getTransforms.push(prefix_classes_1.getPrefixClassesTransformer);
    }
    if (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.
        prefix_functions_1.getPrefixFunctionsTransformer, scrub_file_1.getScrubFileTransformer, class_fold_1.getFoldFileTransformer);
    }
    else if (scrub_file_1.testScrubFile(content)) {
        getTransforms.push(scrub_file_1.getScrubFileTransformer, class_fold_1.getFoldFileTransformer);
    }
    return transform_javascript_1.transformJavascript(Object.assign({}, options, { getTransforms, content }));
}
exports.buildOptimizer = buildOptimizer;
//# sourceMappingURL=data:application/json;base64,[encoded data, 7256 characters]