From f4c66523e764a8cc256584d09ca73570946072ca Mon Sep 17 00:00:00 2001 From: Adam Plumer Date: Tue, 21 Aug 2018 23:15:23 -0500 Subject: [PATCH] fix(express-engine): add server configuration to schematics --- modules/express-engine/schematics/README.md | 2 +- .../__serverFileName@stripTsExtension__.ts | 6 +- .../schematics/install/index.ts | 69 ++++++++++--------- 3 files changed, 41 insertions(+), 36 deletions(-) diff --git a/modules/express-engine/schematics/README.md b/modules/express-engine/schematics/README.md index 02307ae41..3a10f66d2 100644 --- a/modules/express-engine/schematics/README.md +++ b/modules/express-engine/schematics/README.md @@ -7,7 +7,7 @@ A collection of Schematics for Angular Universal Express-Engine. Adds Angular Universal Express Engine and its dependencies and pre-configures the application. - Runs the default Angular Universal schematic to add Universal capabilities to an application -- Adds Express-Engine, NgModule-Factory-Loader, ts-loader, and webpack to `package.json` +- Adds Express-Engine and NgModule-Factory-Loader to `package.json` - Adds a sample Express server file Command: `ng add @nguniversal/express-engine` \ No newline at end of file diff --git a/modules/express-engine/schematics/install/files/root/__serverFileName@stripTsExtension__.ts b/modules/express-engine/schematics/install/files/root/__serverFileName@stripTsExtension__.ts index ad83aa1d8..27a88612e 100644 --- a/modules/express-engine/schematics/install/files/root/__serverFileName@stripTsExtension__.ts +++ b/modules/express-engine/schematics/install/files/root/__serverFileName@stripTsExtension__.ts @@ -19,7 +19,7 @@ const PORT = process.env.PORT || <%= serverPort %>; const DIST_FOLDER = join(process.cwd(), 'dist'); // * NOTE :: leave this as require() since this file is built Dynamically from webpack -const {AppServerModuleNgFactory, LAZY_MODULE_MAP} = require('./server/main'); +const {AppServerModuleNgFactory, LAZY_MODULE_MAP} = require('./<%= clientProject %>-server/main'); // Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine) app.engine('html', ngExpressEngine({ @@ -30,12 +30,12 @@ app.engine('html', ngExpressEngine({ })); app.set('view engine', 'html'); -app.set('views', join(DIST_FOLDER, 'browser')); +app.set('views', join(DIST_FOLDER, '<%= clientProject %>')); // Example Express Rest API endpoints // app.get('/api/**', (req, res) => { }); // Server static files from /browser -app.get('*.*', express.static(join(DIST_FOLDER, 'browser'), { +app.get('*.*', express.static(join(DIST_FOLDER, '<%= clientProject %>'), { maxAge: '1y' })); diff --git a/modules/express-engine/schematics/install/index.ts b/modules/express-engine/schematics/install/index.ts index 78219e905..7b2062a41 100644 --- a/modules/express-engine/schematics/install/index.ts +++ b/modules/express-engine/schematics/install/index.ts @@ -5,14 +5,7 @@ * 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 */ -import { - basename, - experimental, - join, - normalize, - parseJson, - strings, -} from '@angular-devkit/core'; +import {experimental, JsonObject, strings} from '@angular-devkit/core'; import { Rule, SchematicContext, @@ -21,7 +14,6 @@ import { apply, chain, mergeWith, - move, template, url, noop, @@ -29,7 +21,7 @@ import { externalSchematic, } from '@angular-devkit/schematics'; import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks'; -import {getWorkspace} from '@schematics/angular/utility/config'; +import {getWorkspace, getWorkspacePath} from '@schematics/angular/utility/config'; import {Schema as UniversalOptions} from './schema'; @@ -45,19 +37,6 @@ function getClientProject( return clientProject; } -function getClientArchitect( - host: Tree, - options: UniversalOptions, -): experimental.workspace.WorkspaceTool { - const clientArchitect = getClientProject(host, options).architect; - - if (!clientArchitect) { - throw new Error('Client project architect not found.'); - } - - return clientArchitect; -} - function addDependenciesAndScripts(options: UniversalOptions): Rule { return (host: Tree) => { @@ -86,16 +65,44 @@ function addDependenciesAndScripts(options: UniversalOptions): Rule { }; } +function updateConfigFile(options: UniversalOptions): Rule { + return (host: Tree) => { + const workspace = getWorkspace(host); + if (!workspace.projects[options.clientProject]) { + throw new SchematicsException(`Client app ${options.clientProject} not found.`); + } + + const clientProject = workspace.projects[options.clientProject]; + if (!clientProject.architect) { + throw new Error('Client project architect not found.'); + } + + const serverConfig: JsonObject = { + production: { + fileReplacements: [ + { + replace: 'src/environments/environment.ts', + with: 'src/environments/environment.prod.ts' + } + ] + } + }; + clientProject.architect.server.configurations = serverConfig; + + const workspacePath = getWorkspacePath(host); + + host.overwrite(workspacePath, JSON.stringify(workspace, null, 2)); + + return host; + }; +} + export default function (options: UniversalOptions): Rule { return (host: Tree, context: SchematicContext) => { const clientProject = getClientProject(host, options); if (clientProject.projectType !== 'application') { throw new SchematicsException(`Universal requires a project type of "application".`); } - const clientArchitect = getClientArchitect(host, options); - const tsConfigExtends = basename(clientArchitect.build.options.tsConfig); - const rootInSrc = clientProject.root === ''; - const tsConfigDirectory = join(normalize(clientProject.root), rootInSrc ? 'src' : ''); if (!options.skipInstall) { context.addTask(new NodePackageInstallTask()); @@ -106,15 +113,13 @@ export default function (options: UniversalOptions): Rule { template({ ...strings, ...options as object, - stripTsExtension: (s: string) => { return s.replace(/\.ts$/, ''); }, - tsConfigExtends, - rootInSrc, - }), - move(tsConfigDirectory), + stripTsExtension: (s: string) => { return s.replace(/\.ts$/, ''); } + }) ]); return chain([ externalSchematic('@schematics/angular', 'universal', options), + updateConfigFile(options), mergeWith(rootSource), addDependenciesAndScripts(options), ]);