|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import { Rule } from '@angular-devkit/schematics'; |
| 9 | +import * as ts from '../../third_party/github.com/Microsoft/TypeScript/lib/typescript'; |
| 10 | +import { findNodes } from '../../utility/ast-utils'; |
| 11 | +import { findPropertyInAstObject } from '../../utility/json-utils'; |
| 12 | +import { Builders } from '../../utility/workspace-models'; |
| 13 | +import { getTargets, getWorkspace } from './utils'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Update the `main.server.ts` file by adding exports to `renderModule` and `renderModuleFactory` which are |
| 17 | + * now required for Universal and App-Shell for Ivy and `bundleDependencies`. |
| 18 | + */ |
| 19 | +export function updateServerMainFile(): Rule { |
| 20 | + return tree => { |
| 21 | + const workspace = getWorkspace(tree); |
| 22 | + |
| 23 | + for (const { target } of getTargets(workspace, 'server', Builders.Server)) { |
| 24 | + const options = findPropertyInAstObject(target, 'options'); |
| 25 | + if (!options || options.kind !== 'object') { |
| 26 | + continue; |
| 27 | + } |
| 28 | + |
| 29 | + // find the main server file |
| 30 | + const mainFile = findPropertyInAstObject(options, 'main'); |
| 31 | + if (!mainFile || typeof mainFile.value !== 'string') { |
| 32 | + continue; |
| 33 | + } |
| 34 | + |
| 35 | + const mainFilePath = mainFile.value; |
| 36 | + |
| 37 | + const content = tree.read(mainFilePath); |
| 38 | + if (!content) { |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + const source = ts.createSourceFile( |
| 43 | + mainFilePath, |
| 44 | + content.toString().replace(/^\uFEFF/, ''), |
| 45 | + ts.ScriptTarget.Latest, |
| 46 | + true, |
| 47 | + ); |
| 48 | + |
| 49 | + // find exports in main server file |
| 50 | + const exportDeclarations = findNodes(source, ts.SyntaxKind.ExportDeclaration) as ts.ExportDeclaration[]; |
| 51 | + |
| 52 | + const platformServerExports = exportDeclarations.filter(({ moduleSpecifier }) => ( |
| 53 | + moduleSpecifier && ts.isStringLiteral(moduleSpecifier) && moduleSpecifier.text === '@angular/platform-server' |
| 54 | + )); |
| 55 | + |
| 56 | + let hasRenderModule = false; |
| 57 | + let hasRenderModuleFactory = false; |
| 58 | + |
| 59 | + // find exports of renderModule or renderModuleFactory |
| 60 | + for (const { exportClause } of platformServerExports) { |
| 61 | + if (exportClause && ts.isNamedExports(exportClause)) { |
| 62 | + if (!hasRenderModuleFactory) { |
| 63 | + hasRenderModuleFactory = exportClause.elements.some(({ name }) => name.text === 'renderModuleFactory'); |
| 64 | + } |
| 65 | + |
| 66 | + if (!hasRenderModule) { |
| 67 | + hasRenderModule = exportClause.elements.some(({ name }) => name.text === 'renderModule'); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (hasRenderModule && hasRenderModuleFactory) { |
| 73 | + // We have both required exports |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + let exportSpecifiers: ts.ExportSpecifier[] = []; |
| 78 | + let updateExisting = false; |
| 79 | + |
| 80 | + // Add missing exports |
| 81 | + if (platformServerExports.length) { |
| 82 | + const { exportClause } = platformServerExports[0] as ts.ExportDeclaration; |
| 83 | + if (!exportClause) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + exportSpecifiers = [...exportClause.elements]; |
| 88 | + updateExisting = true; |
| 89 | + } |
| 90 | + |
| 91 | + if (!hasRenderModule) { |
| 92 | + exportSpecifiers.push(ts.createExportSpecifier( |
| 93 | + undefined, |
| 94 | + ts.createIdentifier('renderModule'), |
| 95 | + )); |
| 96 | + } |
| 97 | + |
| 98 | + if (!hasRenderModuleFactory) { |
| 99 | + exportSpecifiers.push(ts.createExportSpecifier( |
| 100 | + undefined, |
| 101 | + ts.createIdentifier('renderModuleFactory'), |
| 102 | + )); |
| 103 | + } |
| 104 | + |
| 105 | + // Create a TS printer to get the text of the export node |
| 106 | + const printer = ts.createPrinter(); |
| 107 | + |
| 108 | + const moduleSpecifier = ts.createStringLiteral('@angular/platform-server'); |
| 109 | + |
| 110 | + // TypeScript will emit the Node with double quotes. |
| 111 | + // In schematics we usually write code with a single quotes |
| 112 | + // tslint:disable-next-line: no-any |
| 113 | + (moduleSpecifier as any).singleQuote = true; |
| 114 | + |
| 115 | + const newExportDeclarationText = printer.printNode( |
| 116 | + ts.EmitHint.Unspecified, |
| 117 | + ts.createExportDeclaration( |
| 118 | + undefined, |
| 119 | + undefined, |
| 120 | + ts.createNamedExports(exportSpecifiers), |
| 121 | + moduleSpecifier, |
| 122 | + ), |
| 123 | + source, |
| 124 | + ); |
| 125 | + |
| 126 | + const recorder = tree.beginUpdate(mainFilePath); |
| 127 | + if (updateExisting) { |
| 128 | + const start = platformServerExports[0].getStart(); |
| 129 | + const width = platformServerExports[0].getWidth(); |
| 130 | + |
| 131 | + recorder |
| 132 | + .remove(start, width) |
| 133 | + .insertLeft(start, newExportDeclarationText); |
| 134 | + } else { |
| 135 | + recorder.insertLeft(source.getWidth(), '\n' + newExportDeclarationText); |
| 136 | + } |
| 137 | + |
| 138 | + tree.commitUpdate(recorder); |
| 139 | + } |
| 140 | + |
| 141 | + return tree; |
| 142 | + }; |
| 143 | +} |
0 commit comments