Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit b031a26

Browse files
authored
fix(express-engine): add server configuration to schematics (#1056)
* Contains fixes for Express engine schematics
1 parent 1909be1 commit b031a26

3 files changed

Lines changed: 41 additions & 36 deletions

File tree

modules/express-engine/schematics/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ A collection of Schematics for Angular Universal Express-Engine.
77
Adds Angular Universal Express Engine and its dependencies and pre-configures the application.
88

99
- Runs the default Angular Universal schematic to add Universal capabilities to an application
10-
- Adds Express-Engine, NgModule-Factory-Loader, ts-loader, and webpack to `package.json`
10+
- Adds Express-Engine and NgModule-Factory-Loader to `package.json`
1111
- Adds a sample Express server file
1212

1313
Command: `ng add @nguniversal/express-engine`

modules/express-engine/schematics/install/files/root/__serverFileName@stripTsExtension__.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const PORT = process.env.PORT || <%= serverPort %>;
1919
const DIST_FOLDER = join(process.cwd(), 'dist');
2020

2121
// * NOTE :: leave this as require() since this file is built Dynamically from webpack
22-
const {AppServerModuleNgFactory, LAZY_MODULE_MAP} = require('./server/main');
22+
const {AppServerModuleNgFactory, LAZY_MODULE_MAP} = require('./<%= clientProject %>-server/main');
2323

2424
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
2525
app.engine('html', ngExpressEngine({
@@ -30,12 +30,12 @@ app.engine('html', ngExpressEngine({
3030
}));
3131

3232
app.set('view engine', 'html');
33-
app.set('views', join(DIST_FOLDER, 'browser'));
33+
app.set('views', join(DIST_FOLDER, '<%= clientProject %>'));
3434

3535
// Example Express Rest API endpoints
3636
// app.get('/api/**', (req, res) => { });
3737
// Server static files from /browser
38-
app.get('*.*', express.static(join(DIST_FOLDER, 'browser'), {
38+
app.get('*.*', express.static(join(DIST_FOLDER, '<%= clientProject %>'), {
3939
maxAge: '1y'
4040
}));
4141

modules/express-engine/schematics/install/index.ts

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import {
9-
basename,
10-
experimental,
11-
join,
12-
normalize,
13-
parseJson,
14-
strings,
15-
} from '@angular-devkit/core';
8+
import {experimental, JsonObject, strings} from '@angular-devkit/core';
169
import {
1710
Rule,
1811
SchematicContext,
@@ -21,15 +14,14 @@ import {
2114
apply,
2215
chain,
2316
mergeWith,
24-
move,
2517
template,
2618
url,
2719
noop,
2820
filter,
2921
externalSchematic,
3022
} from '@angular-devkit/schematics';
3123
import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks';
32-
import {getWorkspace} from '@schematics/angular/utility/config';
24+
import {getWorkspace, getWorkspacePath} from '@schematics/angular/utility/config';
3325
import {Schema as UniversalOptions} from './schema';
3426

3527

@@ -45,19 +37,6 @@ function getClientProject(
4537
return clientProject;
4638
}
4739

48-
function getClientArchitect(
49-
host: Tree,
50-
options: UniversalOptions,
51-
): experimental.workspace.WorkspaceTool {
52-
const clientArchitect = getClientProject(host, options).architect;
53-
54-
if (!clientArchitect) {
55-
throw new Error('Client project architect not found.');
56-
}
57-
58-
return clientArchitect;
59-
}
60-
6140
function addDependenciesAndScripts(options: UniversalOptions): Rule {
6241
return (host: Tree) => {
6342

@@ -86,16 +65,44 @@ function addDependenciesAndScripts(options: UniversalOptions): Rule {
8665
};
8766
}
8867

68+
function updateConfigFile(options: UniversalOptions): Rule {
69+
return (host: Tree) => {
70+
const workspace = getWorkspace(host);
71+
if (!workspace.projects[options.clientProject]) {
72+
throw new SchematicsException(`Client app ${options.clientProject} not found.`);
73+
}
74+
75+
const clientProject = workspace.projects[options.clientProject];
76+
if (!clientProject.architect) {
77+
throw new Error('Client project architect not found.');
78+
}
79+
80+
const serverConfig: JsonObject = {
81+
production: {
82+
fileReplacements: [
83+
{
84+
replace: 'src/environments/environment.ts',
85+
with: 'src/environments/environment.prod.ts'
86+
}
87+
]
88+
}
89+
};
90+
clientProject.architect.server.configurations = serverConfig;
91+
92+
const workspacePath = getWorkspacePath(host);
93+
94+
host.overwrite(workspacePath, JSON.stringify(workspace, null, 2));
95+
96+
return host;
97+
};
98+
}
99+
89100
export default function (options: UniversalOptions): Rule {
90101
return (host: Tree, context: SchematicContext) => {
91102
const clientProject = getClientProject(host, options);
92103
if (clientProject.projectType !== 'application') {
93104
throw new SchematicsException(`Universal requires a project type of "application".`);
94105
}
95-
const clientArchitect = getClientArchitect(host, options);
96-
const tsConfigExtends = basename(clientArchitect.build.options.tsConfig);
97-
const rootInSrc = clientProject.root === '';
98-
const tsConfigDirectory = join(normalize(clientProject.root), rootInSrc ? 'src' : '');
99106

100107
if (!options.skipInstall) {
101108
context.addTask(new NodePackageInstallTask());
@@ -106,15 +113,13 @@ export default function (options: UniversalOptions): Rule {
106113
template({
107114
...strings,
108115
...options as object,
109-
stripTsExtension: (s: string) => { return s.replace(/\.ts$/, ''); },
110-
tsConfigExtends,
111-
rootInSrc,
112-
}),
113-
move(tsConfigDirectory),
116+
stripTsExtension: (s: string) => { return s.replace(/\.ts$/, ''); }
117+
})
114118
]);
115119

116120
return chain([
117121
externalSchematic('@schematics/angular', 'universal', options),
122+
updateConfigFile(options),
118123
mergeWith(rootSource),
119124
addDependenciesAndScripts(options),
120125
]);

0 commit comments

Comments
 (0)