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

Commit a4bf3d9

Browse files
authored
feat(hapi-engine): add schematics (#1057)
* Add schematics for the Hapi engine
1 parent 0e38dd1 commit a4bf3d9

16 files changed

Lines changed: 692 additions & 1 deletion

.bazelignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

modules/express-engine/schematics/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ ts_library(
1919
exclude = [
2020
"**/*.spec.ts",
2121
"**/files/**/*",
22-
"test-setup/**/*",
2322
],
2423
),
2524
module_name = "@nguniversal/express-engine/schematics",

modules/hapi-engine/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ng_package(
2222
":package.json",
2323
],
2424
entry_point = "modules/hapi-engine/index.js",
25+
packages = ["//modules/hapi-engine/schematics:npm_package"],
2526
readme_md = ":README.md",
2627
tags = ["release"],
2728
deps = [

modules/hapi-engine/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"@angular/platform-server": "NG_VERSION",
1515
"hapi": "HAPI_VERSION"
1616
},
17+
"schematics": "./schematics/collection.json",
1718
"ng-update": {
1819
"packageGroup": "NG_UPDATE_PACKAGE_GROUP"
1920
},
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
load("//tools:defaults.bzl", "jasmine_node_test", "ng_test_library", "npm_package", "ts_library")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
filegroup(
6+
name = "schematics_assets",
7+
srcs = glob([
8+
"**/files/**/*",
9+
"**/*.json",
10+
]) + [
11+
"README.md",
12+
],
13+
)
14+
15+
ts_library(
16+
name = "schematics",
17+
srcs = glob(
18+
["**/*.ts"],
19+
exclude = [
20+
"**/*.spec.ts",
21+
"**/files/**/*",
22+
],
23+
),
24+
module_name = "@nguniversal/hapi-engine/schematics",
25+
tsconfig = ":tsconfig.json",
26+
deps = [
27+
"@ngudeps//@angular-devkit/schematics",
28+
"@ngudeps//@schematics/angular",
29+
"@ngudeps//@types/jasmine",
30+
],
31+
)
32+
33+
# This package is intended to be combined into the main @nguniversal/hapi-engine package as a dep.
34+
npm_package(
35+
name = "npm_package",
36+
srcs = [":schematics_assets"],
37+
deps = [":schematics"],
38+
)
39+
40+
### Testing rules
41+
42+
jasmine_node_test(
43+
name = "unit_tests",
44+
srcs = [":schematics_test_sources"],
45+
data = [":schematics_assets"],
46+
)
47+
48+
ng_test_library(
49+
name = "schematics_test_sources",
50+
srcs = glob(
51+
["**/*.spec.ts"],
52+
exclude = ["**/files/**/*"],
53+
),
54+
tsconfig = ":tsconfig.json",
55+
deps = [
56+
":schematics",
57+
"@ngudeps//@angular-devkit/schematics",
58+
"@ngudeps//@schematics/angular",
59+
],
60+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Angular Universal Hapi-Engine Schematics
2+
A collection of Schematics for Angular Universal Hapi-Engine.
3+
4+
## Collection
5+
6+
### Install
7+
Adds Angular Universal Hapi Engine and its dependencies and pre-configures the application.
8+
9+
- Runs the default Angular Universal schematic to add Universal capabilities to an application
10+
- Adds Hapi-Engine and NgModule-Factory-Loader to `package.json`
11+
- Adds a sample Hapi server file
12+
13+
Command: `ng add @nguniversal/hapi-engine`
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json",
3+
"schematics": {
4+
"ng-add": {
5+
"description": "Adds Angular Universal Hapi Engine to the application without affecting any templates",
6+
"factory": "./install",
7+
"schema": "./install/schema.json",
8+
"aliases": ["hapi-engine-shell"]
9+
},
10+
}
11+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import 'zone.js/dist/zone-node';
2+
import {enableProdMode} from '@angular/core';
3+
// Hapi Engine
4+
import {ngHapiEngine} from '@nguniversal/hapi-engine';
5+
// Import module map for lazy loading
6+
import {provideModuleMap} from '@nguniversal/module-map-ngfactory-loader';
7+
8+
import {Request, Server} from 'hapi';
9+
import * as Inert from 'inert';
10+
import {join} from 'path';
11+
12+
// Faster server renders w/ Prod mode (dev mode never needed)
13+
enableProdMode();
14+
15+
// Hapi server
16+
const PORT = process.env.PORT || <%= serverPort %>;
17+
const server = new Server({ port: PORT, host: 'localhost' });
18+
19+
const DIST_FOLDER = join(process.cwd(), 'dist');
20+
21+
// * NOTE :: leave this as require() since this file is built Dynamically from webpack
22+
const {AppServerModuleNgFactory, LAZY_MODULE_MAP} = require('./<%= clientProject %>-server/main');
23+
24+
server.route({
25+
method: 'GET',
26+
path: '*',
27+
handler: (req: Request) =>
28+
ngHapiEngine({
29+
bootstrap: AppServerModuleNgFactory,
30+
req,
31+
providers: [
32+
provideModuleMap(LAZY_MODULE_MAP)
33+
]
34+
})
35+
});
36+
37+
(async() => {
38+
await server.register(Inert);
39+
40+
// Client bundles will be statically served from the built/ directory.
41+
server.route({
42+
method: 'GET',
43+
path: '{file*}',
44+
handler: {
45+
directory: {
46+
path: join(DIST_FOLDER, '<%= clientProject %>')
47+
}
48+
}
49+
});
50+
51+
await server.start();
52+
console.log(`Node Hapi server listening on http://localhost:${PORT}`);
53+
})();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compileOnSave": false,
3+
"compilerOptions": {
4+
"outDir": "./dist",
5+
"sourceMap": true,
6+
"declaration": false,
7+
"moduleResolution": "node",
8+
"emitDecoratorMetadata": true,
9+
"experimentalDecorators": true,
10+
"target": "es5",
11+
"typeRoots": [
12+
"node_modules/@types"
13+
],
14+
"lib": [
15+
"es2017",
16+
"dom"
17+
]
18+
},
19+
"include": ["<%= stripTsExtension(serverFileName) %>.ts"]
20+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Work around for https://github.com/angular/angular-cli/issues/7200
2+
3+
const path = require('path');
4+
const webpack = require('webpack');
5+
6+
module.exports = {
7+
mode: 'none',
8+
entry: {
9+
// This is our Hapi server for Dynamic universal
10+
server: './<%= stripTsExtension(serverFileName) %>.ts'
11+
},
12+
target: 'node',
13+
resolve: { extensions: ['.ts', '.js'] },
14+
optimization: {
15+
minimize: false
16+
},
17+
output: {
18+
// Puts the output at the root of the dist folder
19+
path: path.join(__dirname, 'dist'),
20+
filename: '[name].js'
21+
},
22+
module: {
23+
rules: [
24+
{ test: /\.ts$/, loader: 'ts-loader' },
25+
{
26+
// Mark files inside `@angular/core` as using SystemJS style dynamic imports.
27+
// Removing this will cause deprecation warnings to appear.
28+
test: /(\\|\/)@angular(\\|\/)core(\\|\/).+\.js$/,
29+
parser: { system: true },
30+
},
31+
]
32+
},
33+
plugins: [
34+
new webpack.ContextReplacementPlugin(
35+
// fixes WARNING Critical dependency: the request of a dependency is an expression
36+
/(.+)?angular(\\|\/)core(.+)?/,
37+
path.join(__dirname, 'src'), // location of your src
38+
{} // a map of your routes
39+
),
40+
new webpack.ContextReplacementPlugin(
41+
// fixes WARNING Critical dependency: the request of a dependency is an expression
42+
/(.+)?hapi(\\|\/)(.+)?/,
43+
path.join(__dirname, 'src'),
44+
{}
45+
)
46+
]
47+
};

0 commit comments

Comments
 (0)