[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.updateServerMainFile = void 0;
|
---|
| 30 | const ts = __importStar(require("../../third_party/github.com/Microsoft/TypeScript/lib/typescript"));
|
---|
| 31 | const ast_utils_1 = require("../../utility/ast-utils");
|
---|
| 32 | const workspace_1 = require("../../utility/workspace");
|
---|
| 33 | const workspace_models_1 = require("../../utility/workspace-models");
|
---|
| 34 | /**
|
---|
| 35 | * Update the `main.server.ts` file by adding exports to `renderModule` and `renderModuleFactory` which are
|
---|
| 36 | * now required for Universal and App-Shell for Ivy and `bundleDependencies`.
|
---|
| 37 | */
|
---|
| 38 | function updateServerMainFile() {
|
---|
| 39 | return async (tree) => {
|
---|
| 40 | var _a;
|
---|
| 41 | const workspace = await workspace_1.getWorkspace(tree);
|
---|
| 42 | for (const [targetName, target] of workspace_1.allWorkspaceTargets(workspace)) {
|
---|
| 43 | if (targetName !== 'server' || target.builder !== workspace_models_1.Builders.Server) {
|
---|
| 44 | continue;
|
---|
| 45 | }
|
---|
| 46 | // find the main server file
|
---|
| 47 | const mainFilePath = (_a = target.options) === null || _a === void 0 ? void 0 : _a.main;
|
---|
| 48 | if (!mainFilePath || typeof mainFilePath !== 'string') {
|
---|
| 49 | continue;
|
---|
| 50 | }
|
---|
| 51 | const content = tree.read(mainFilePath);
|
---|
| 52 | if (!content) {
|
---|
| 53 | continue;
|
---|
| 54 | }
|
---|
| 55 | const source = ts.createSourceFile(mainFilePath, content.toString().replace(/^\uFEFF/, ''), ts.ScriptTarget.Latest, true);
|
---|
| 56 | // find exports in main server file
|
---|
| 57 | const exportDeclarations = ast_utils_1.findNodes(source, ts.SyntaxKind.ExportDeclaration);
|
---|
| 58 | const platformServerExports = exportDeclarations.filter(({ moduleSpecifier }) => moduleSpecifier &&
|
---|
| 59 | ts.isStringLiteral(moduleSpecifier) &&
|
---|
| 60 | moduleSpecifier.text === '@angular/platform-server');
|
---|
| 61 | let hasRenderModule = false;
|
---|
| 62 | let hasRenderModuleFactory = false;
|
---|
| 63 | // find exports of renderModule or renderModuleFactory
|
---|
| 64 | for (const { exportClause } of platformServerExports) {
|
---|
| 65 | if (exportClause && ts.isNamedExports(exportClause)) {
|
---|
| 66 | if (!hasRenderModuleFactory) {
|
---|
| 67 | hasRenderModuleFactory = exportClause.elements.some(({ name }) => name.text === 'renderModuleFactory');
|
---|
| 68 | }
|
---|
| 69 | if (!hasRenderModule) {
|
---|
| 70 | hasRenderModule = exportClause.elements.some(({ name }) => name.text === 'renderModule');
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 | if (hasRenderModule && hasRenderModuleFactory) {
|
---|
| 75 | // We have both required exports
|
---|
| 76 | continue;
|
---|
| 77 | }
|
---|
| 78 | let exportSpecifiers = [];
|
---|
| 79 | let updateExisting = false;
|
---|
| 80 | // Add missing exports
|
---|
| 81 | if (platformServerExports.length) {
|
---|
| 82 | const { exportClause } = platformServerExports[0];
|
---|
| 83 | if (!exportClause || ts.isNamespaceExport(exportClause)) {
|
---|
| 84 | continue;
|
---|
| 85 | }
|
---|
| 86 | exportSpecifiers = [...exportClause.elements];
|
---|
| 87 | updateExisting = true;
|
---|
| 88 | }
|
---|
| 89 | if (!hasRenderModule) {
|
---|
| 90 | exportSpecifiers.push(ts.createExportSpecifier(undefined, ts.createIdentifier('renderModule')));
|
---|
| 91 | }
|
---|
| 92 | if (!hasRenderModuleFactory) {
|
---|
| 93 | exportSpecifiers.push(ts.createExportSpecifier(undefined, ts.createIdentifier('renderModuleFactory')));
|
---|
| 94 | }
|
---|
| 95 | // Create a TS printer to get the text of the export node
|
---|
| 96 | const printer = ts.createPrinter();
|
---|
| 97 | const moduleSpecifier = ts.createStringLiteral('@angular/platform-server');
|
---|
| 98 | // TypeScript will emit the Node with double quotes.
|
---|
| 99 | // In schematics we usually write code with a single quotes
|
---|
| 100 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
| 101 | moduleSpecifier.singleQuote = true;
|
---|
| 102 | const newExportDeclarationText = printer.printNode(ts.EmitHint.Unspecified, ts.createExportDeclaration(undefined, undefined, ts.createNamedExports(exportSpecifiers), moduleSpecifier), source);
|
---|
| 103 | const recorder = tree.beginUpdate(mainFilePath);
|
---|
| 104 | if (updateExisting) {
|
---|
| 105 | const start = platformServerExports[0].getStart();
|
---|
| 106 | const width = platformServerExports[0].getWidth();
|
---|
| 107 | recorder.remove(start, width).insertLeft(start, newExportDeclarationText);
|
---|
| 108 | }
|
---|
| 109 | else {
|
---|
| 110 | recorder.insertLeft(source.getWidth(), '\n' + newExportDeclarationText);
|
---|
| 111 | }
|
---|
| 112 | tree.commitUpdate(recorder);
|
---|
| 113 | }
|
---|
| 114 | };
|
---|
| 115 | }
|
---|
| 116 | exports.updateServerMainFile = updateServerMainFile;
|
---|