source: trip-planner-front/node_modules/@schematics/angular/universal/index.js@ fa375fe

Last change on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 12.8 KB
RevLine 
[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 */
9var __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}));
16var __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});
21var __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};
28Object.defineProperty(exports, "__esModule", { value: true });
29const core_1 = require("@angular-devkit/core");
30const schematics_1 = require("@angular-devkit/schematics");
31const tasks_1 = require("@angular-devkit/schematics/tasks");
32const ts = __importStar(require("../third_party/github.com/Microsoft/TypeScript/lib/typescript"));
33const ast_utils_1 = require("../utility/ast-utils");
34const change_1 = require("../utility/change");
35const dependencies_1 = require("../utility/dependencies");
36const ng_ast_utils_1 = require("../utility/ng-ast-utils");
37const paths_1 = require("../utility/paths");
38const project_targets_1 = require("../utility/project-targets");
39const workspace_1 = require("../utility/workspace");
40const workspace_models_1 = require("../utility/workspace-models");
41function updateConfigFile(options, tsConfigDirectory) {
42 return workspace_1.updateWorkspace((workspace) => {
43 const clientProject = workspace.projects.get(options.project);
44 if (clientProject) {
45 // In case the browser builder hashes the assets
46 // we need to add this setting to the server builder
47 // as otherwise when assets it will be requested twice.
48 // One for the server which will be unhashed, and other on the client which will be hashed.
49 const getServerOptions = (options = {}) => {
50 return {
51 outputHashing: (options === null || options === void 0 ? void 0 : options.outputHashing) === 'all' ? 'media' : options === null || options === void 0 ? void 0 : options.outputHashing,
52 fileReplacements: options === null || options === void 0 ? void 0 : options.fileReplacements,
53 optimization: (options === null || options === void 0 ? void 0 : options.optimization) === undefined ? undefined : !!(options === null || options === void 0 ? void 0 : options.optimization),
54 sourceMap: options === null || options === void 0 ? void 0 : options.sourceMap,
55 localization: options === null || options === void 0 ? void 0 : options.localization,
56 stylePreprocessorOptions: options === null || options === void 0 ? void 0 : options.stylePreprocessorOptions,
57 resourcesOutputPath: options === null || options === void 0 ? void 0 : options.resourcesOutputPath,
58 deployUrl: options === null || options === void 0 ? void 0 : options.deployUrl,
59 i18nMissingTranslation: options === null || options === void 0 ? void 0 : options.i18nMissingTranslation,
60 preserveSymlinks: options === null || options === void 0 ? void 0 : options.preserveSymlinks,
61 extractLicenses: options === null || options === void 0 ? void 0 : options.extractLicenses,
62 inlineStyleLanguage: options === null || options === void 0 ? void 0 : options.inlineStyleLanguage,
63 };
64 };
65 const buildTarget = clientProject.targets.get('build');
66 if (buildTarget === null || buildTarget === void 0 ? void 0 : buildTarget.options) {
67 buildTarget.options.outputPath = `dist/${options.project}/browser`;
68 }
69 const buildConfigurations = buildTarget === null || buildTarget === void 0 ? void 0 : buildTarget.configurations;
70 const configurations = {};
71 if (buildConfigurations) {
72 for (const [key, options] of Object.entries(buildConfigurations)) {
73 configurations[key] = getServerOptions(options);
74 }
75 }
76 const mainPath = options.main;
77 const serverTsConfig = core_1.join(tsConfigDirectory, 'tsconfig.server.json');
78 clientProject.targets.add({
79 name: 'server',
80 builder: workspace_models_1.Builders.Server,
81 defaultConfiguration: 'production',
82 options: {
83 outputPath: `dist/${options.project}/server`,
84 main: core_1.join(core_1.normalize(clientProject.root), 'src', mainPath.endsWith('.ts') ? mainPath : mainPath + '.ts'),
85 tsConfig: serverTsConfig,
86 ...((buildTarget === null || buildTarget === void 0 ? void 0 : buildTarget.options) ? getServerOptions(buildTarget === null || buildTarget === void 0 ? void 0 : buildTarget.options) : {}),
87 },
88 configurations,
89 });
90 }
91 });
92}
93function findBrowserModuleImport(host, modulePath) {
94 const moduleBuffer = host.read(modulePath);
95 if (!moduleBuffer) {
96 throw new schematics_1.SchematicsException(`Module file (${modulePath}) not found`);
97 }
98 const moduleFileText = moduleBuffer.toString('utf-8');
99 const source = ts.createSourceFile(modulePath, moduleFileText, ts.ScriptTarget.Latest, true);
100 const decoratorMetadata = ast_utils_1.getDecoratorMetadata(source, 'NgModule', '@angular/core')[0];
101 const browserModuleNode = ast_utils_1.findNode(decoratorMetadata, ts.SyntaxKind.Identifier, 'BrowserModule');
102 if (browserModuleNode === null) {
103 throw new schematics_1.SchematicsException(`Cannot find BrowserModule import in ${modulePath}`);
104 }
105 return browserModuleNode;
106}
107function wrapBootstrapCall(mainFile) {
108 return (host) => {
109 const mainPath = core_1.normalize('/' + mainFile);
110 let bootstrapCall = ng_ast_utils_1.findBootstrapModuleCall(host, mainPath);
111 if (bootstrapCall === null) {
112 throw new schematics_1.SchematicsException('Bootstrap module not found.');
113 }
114 let bootstrapCallExpression = null;
115 let currentCall = bootstrapCall;
116 while (bootstrapCallExpression === null && currentCall.parent) {
117 currentCall = currentCall.parent;
118 if (ts.isExpressionStatement(currentCall) || ts.isVariableStatement(currentCall)) {
119 bootstrapCallExpression = currentCall;
120 }
121 }
122 bootstrapCall = currentCall;
123 // In case the bootstrap code is a variable statement
124 // we need to determine it's usage
125 if (bootstrapCallExpression && ts.isVariableStatement(bootstrapCallExpression)) {
126 const declaration = bootstrapCallExpression.declarationList.declarations[0];
127 const bootstrapVar = declaration.name.text;
128 const sf = bootstrapCallExpression.getSourceFile();
129 bootstrapCall = findCallExpressionNode(sf, bootstrapVar) || currentCall;
130 }
131 // indent contents
132 const triviaWidth = bootstrapCall.getLeadingTriviaWidth();
133 const beforeText = `document.addEventListener('DOMContentLoaded', () => {\n` +
134 ' '.repeat(triviaWidth > 2 ? triviaWidth + 1 : triviaWidth);
135 const afterText = `\n${triviaWidth > 2 ? ' '.repeat(triviaWidth - 1) : ''}});`;
136 // in some cases we need to cater for a trailing semicolon such as;
137 // bootstrap().catch(err => console.log(err));
138 const lastToken = bootstrapCall.parent.getLastToken();
139 let endPos = bootstrapCall.getEnd();
140 if (lastToken && lastToken.kind === ts.SyntaxKind.SemicolonToken) {
141 endPos = lastToken.getEnd();
142 }
143 const recorder = host.beginUpdate(mainPath);
144 recorder.insertLeft(bootstrapCall.getStart(), beforeText);
145 recorder.insertRight(endPos, afterText);
146 host.commitUpdate(recorder);
147 };
148}
149function findCallExpressionNode(node, text) {
150 if (ts.isCallExpression(node) &&
151 ts.isIdentifier(node.expression) &&
152 node.expression.text === text) {
153 return node;
154 }
155 let foundNode = null;
156 ts.forEachChild(node, (childNode) => {
157 foundNode = findCallExpressionNode(childNode, text);
158 if (foundNode) {
159 return true;
160 }
161 });
162 return foundNode;
163}
164function addServerTransition(options, mainFile, clientProjectRoot) {
165 return (host) => {
166 const mainPath = core_1.normalize('/' + mainFile);
167 const bootstrapModuleRelativePath = ng_ast_utils_1.findBootstrapModulePath(host, mainPath);
168 const bootstrapModulePath = core_1.normalize(`/${clientProjectRoot}/src/${bootstrapModuleRelativePath}.ts`);
169 const browserModuleImport = findBrowserModuleImport(host, bootstrapModulePath);
170 const appId = options.appId;
171 const transitionCall = `.withServerTransition({ appId: '${appId}' })`;
172 const position = browserModuleImport.pos + browserModuleImport.getFullText().length;
173 const transitionCallChange = new change_1.InsertChange(bootstrapModulePath, position, transitionCall);
174 const transitionCallRecorder = host.beginUpdate(bootstrapModulePath);
175 transitionCallRecorder.insertLeft(transitionCallChange.pos, transitionCallChange.toAdd);
176 host.commitUpdate(transitionCallRecorder);
177 };
178}
179function addDependencies() {
180 return (host) => {
181 const coreDep = dependencies_1.getPackageJsonDependency(host, '@angular/core');
182 if (coreDep === null) {
183 throw new schematics_1.SchematicsException('Could not find version.');
184 }
185 const platformServerDep = {
186 ...coreDep,
187 name: '@angular/platform-server',
188 };
189 dependencies_1.addPackageJsonDependency(host, platformServerDep);
190 return host;
191 };
192}
193function default_1(options) {
194 return async (host, context) => {
195 const workspace = await workspace_1.getWorkspace(host);
196 const clientProject = workspace.projects.get(options.project);
197 if (!clientProject || clientProject.extensions.projectType !== 'application') {
198 throw new schematics_1.SchematicsException(`Universal requires a project type of "application".`);
199 }
200 const clientBuildTarget = clientProject.targets.get('build');
201 if (!clientBuildTarget) {
202 throw project_targets_1.targetBuildNotFoundError();
203 }
204 const clientBuildOptions = (clientBuildTarget.options ||
205 {});
206 const clientTsConfig = core_1.normalize(clientBuildOptions.tsConfig);
207 const tsConfigExtends = core_1.basename(clientTsConfig);
208 // this is needed because prior to version 8, tsconfig might have been in 'src'
209 // and we don't want to break the 'ng add @nguniversal/express-engine schematics'
210 const rootInSrc = clientProject.root === '' && clientTsConfig.includes('src/');
211 const tsConfigDirectory = core_1.join(core_1.normalize(clientProject.root), rootInSrc ? 'src' : '');
212 if (!options.skipInstall) {
213 context.addTask(new tasks_1.NodePackageInstallTask());
214 }
215 const templateSource = schematics_1.apply(schematics_1.url('./files/src'), [
216 schematics_1.applyTemplates({
217 ...core_1.strings,
218 ...options,
219 stripTsExtension: (s) => s.replace(/\.ts$/, ''),
220 hasLocalizePackage: !!dependencies_1.getPackageJsonDependency(host, '@angular/localize'),
221 }),
222 schematics_1.move(core_1.join(core_1.normalize(clientProject.root), 'src')),
223 ]);
224 const rootSource = schematics_1.apply(schematics_1.url('./files/root'), [
225 schematics_1.applyTemplates({
226 ...core_1.strings,
227 ...options,
228 stripTsExtension: (s) => s.replace(/\.ts$/, ''),
229 tsConfigExtends,
230 relativePathToWorkspaceRoot: paths_1.relativePathToWorkspaceRoot(tsConfigDirectory),
231 rootInSrc,
232 }),
233 schematics_1.move(tsConfigDirectory),
234 ]);
235 return schematics_1.chain([
236 schematics_1.mergeWith(templateSource),
237 schematics_1.mergeWith(rootSource),
238 addDependencies(),
239 updateConfigFile(options, tsConfigDirectory),
240 wrapBootstrapCall(clientBuildOptions.main),
241 addServerTransition(options, clientBuildOptions.main, clientProject.root),
242 ]);
243 };
244}
245exports.default = default_1;
Note: See TracBrowser for help on using the repository browser.