Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/express-engine/schematics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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'
}));

Expand Down
69 changes: 37 additions & 32 deletions modules/express-engine/schematics/install/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -21,15 +14,14 @@ import {
apply,
chain,
mergeWith,
move,
template,
url,
noop,
filter,
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';


Expand All @@ -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) => {

Expand Down Expand Up @@ -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());
Expand All @@ -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),
]);
Expand Down