[6a3a178] | 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 | */
|
---|
| 9 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
---|
| 10 | if (k2 === undefined) k2 = k;
|
---|
| 11 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
---|
| 12 | }) : (function(o, m, k, k2) {
|
---|
| 13 | if (k2 === undefined) k2 = k;
|
---|
| 14 | o[k2] = m[k];
|
---|
| 15 | }));
|
---|
| 16 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
---|
| 17 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
---|
| 18 | }) : function(o, v) {
|
---|
| 19 | o["default"] = v;
|
---|
| 20 | });
|
---|
| 21 | var __importStar = (this && this.__importStar) || function (mod) {
|
---|
| 22 | if (mod && mod.__esModule) return mod;
|
---|
| 23 | var result = {};
|
---|
| 24 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
---|
| 25 | __setModuleDefault(result, mod);
|
---|
| 26 | return result;
|
---|
| 27 | };
|
---|
| 28 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 29 | exports.replaceBootstrap = exports.mergeTransformers = exports.createJitTransformers = exports.createAotTransformers = void 0;
|
---|
| 30 | const compiler_cli_1 = require("@angular/compiler-cli");
|
---|
| 31 | const ts = __importStar(require("typescript"));
|
---|
| 32 | const elide_imports_1 = require("../transformers/elide_imports");
|
---|
| 33 | const remove_ivy_jit_support_calls_1 = require("../transformers/remove-ivy-jit-support-calls");
|
---|
| 34 | const replace_resources_1 = require("../transformers/replace_resources");
|
---|
| 35 | function createAotTransformers(builder, options) {
|
---|
| 36 | const getTypeChecker = () => builder.getProgram().getTypeChecker();
|
---|
| 37 | const transformers = {
|
---|
| 38 | before: [replaceBootstrap(getTypeChecker)],
|
---|
| 39 | after: [],
|
---|
| 40 | };
|
---|
| 41 | const removeClassMetadata = !options.emitClassMetadata;
|
---|
| 42 | const removeNgModuleScope = !options.emitNgModuleScope;
|
---|
| 43 | if (removeClassMetadata || removeNgModuleScope) {
|
---|
| 44 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
---|
| 45 | transformers.before.push(remove_ivy_jit_support_calls_1.removeIvyJitSupportCalls(removeClassMetadata, removeNgModuleScope, getTypeChecker));
|
---|
| 46 | }
|
---|
| 47 | return transformers;
|
---|
| 48 | }
|
---|
| 49 | exports.createAotTransformers = createAotTransformers;
|
---|
| 50 | function createJitTransformers(builder, options) {
|
---|
| 51 | const getTypeChecker = () => builder.getProgram().getTypeChecker();
|
---|
| 52 | return {
|
---|
| 53 | before: [
|
---|
| 54 | replace_resources_1.replaceResources(() => true, getTypeChecker, options.directTemplateLoading, options.inlineStyleMimeType, options.inlineStyleFileExtension),
|
---|
| 55 | compiler_cli_1.constructorParametersDownlevelTransform(builder.getProgram()),
|
---|
| 56 | ],
|
---|
| 57 | };
|
---|
| 58 | }
|
---|
| 59 | exports.createJitTransformers = createJitTransformers;
|
---|
| 60 | function mergeTransformers(first, second) {
|
---|
| 61 | const result = {};
|
---|
| 62 | if (first.before || second.before) {
|
---|
| 63 | result.before = [...(first.before || []), ...(second.before || [])];
|
---|
| 64 | }
|
---|
| 65 | if (first.after || second.after) {
|
---|
| 66 | result.after = [...(first.after || []), ...(second.after || [])];
|
---|
| 67 | }
|
---|
| 68 | if (first.afterDeclarations || second.afterDeclarations) {
|
---|
| 69 | result.afterDeclarations = [
|
---|
| 70 | ...(first.afterDeclarations || []),
|
---|
| 71 | ...(second.afterDeclarations || []),
|
---|
| 72 | ];
|
---|
| 73 | }
|
---|
| 74 | return result;
|
---|
| 75 | }
|
---|
| 76 | exports.mergeTransformers = mergeTransformers;
|
---|
| 77 | function replaceBootstrap(getTypeChecker) {
|
---|
| 78 | return (context) => {
|
---|
| 79 | let bootstrapImport;
|
---|
| 80 | let bootstrapNamespace;
|
---|
| 81 | const replacedNodes = [];
|
---|
| 82 | const nodeFactory = context.factory;
|
---|
| 83 | const visitNode = (node) => {
|
---|
| 84 | if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) {
|
---|
| 85 | const target = node.expression;
|
---|
| 86 | if (target.text === 'platformBrowserDynamic') {
|
---|
| 87 | if (!bootstrapNamespace) {
|
---|
| 88 | bootstrapNamespace = nodeFactory.createUniqueName('__NgCli_bootstrap_');
|
---|
| 89 | bootstrapImport = nodeFactory.createImportDeclaration(undefined, undefined, nodeFactory.createImportClause(false, undefined, nodeFactory.createNamespaceImport(bootstrapNamespace)), nodeFactory.createStringLiteral('@angular/platform-browser'));
|
---|
| 90 | }
|
---|
| 91 | replacedNodes.push(target);
|
---|
| 92 | return nodeFactory.updateCallExpression(node, nodeFactory.createPropertyAccessExpression(bootstrapNamespace, 'platformBrowser'), node.typeArguments, node.arguments);
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | return ts.visitEachChild(node, visitNode, context);
|
---|
| 96 | };
|
---|
| 97 | return (sourceFile) => {
|
---|
| 98 | let updatedSourceFile = ts.visitEachChild(sourceFile, visitNode, context);
|
---|
| 99 | if (bootstrapImport) {
|
---|
| 100 | // Remove any unused platform browser dynamic imports
|
---|
| 101 | const removals = elide_imports_1.elideImports(updatedSourceFile, replacedNodes, getTypeChecker, context.getCompilerOptions());
|
---|
| 102 | if (removals.size > 0) {
|
---|
| 103 | updatedSourceFile = ts.visitEachChild(updatedSourceFile, (node) => (removals.has(node) ? undefined : node), context);
|
---|
| 104 | }
|
---|
| 105 | // Add new platform browser import
|
---|
| 106 | return nodeFactory.updateSourceFile(updatedSourceFile, ts.setTextRange(nodeFactory.createNodeArray([bootstrapImport, ...updatedSourceFile.statements]), sourceFile.statements));
|
---|
| 107 | }
|
---|
| 108 | else {
|
---|
| 109 | return updatedSourceFile;
|
---|
| 110 | }
|
---|
| 111 | };
|
---|
| 112 | };
|
---|
| 113 | }
|
---|
| 114 | exports.replaceBootstrap = replaceBootstrap;
|
---|