@@ -45,7 +45,7 @@ This is a [Hapiness](https://github.com/hapinessjs/hapiness) Engine for running
4545
4646This story will show you how to set up Universal bundling for an existing ` @angular/cli ` .
4747
48- We support actually ` @angular ` ` @8.0 .0 ` and next so you must upgrade all packages inside your project.
48+ We support actually ` @angular ` ` @8.1 .0 ` and next so you must upgrade all packages inside your project.
4949
5050We use ` yarn ` as package manager.
5151
@@ -193,7 +193,17 @@ Create a main file for your Universal bundle. This file only needs to export you
193193### src/main.server.ts:
194194
195195``` typescript
196+ import { enableProdMode } from ' @angular/core' ;
197+
198+ import { environment } from ' ./environments/environment' ;
199+
200+ if (environment .production ) {
201+ enableProdMode ();
202+ }
203+
196204export { AppServerModule } from ' ./app/app.server.module' ;
205+
206+ export { NgUniversalModule } from ' @hapiness/ng-universal' ;
197207```
198208
199209Copy ` tsconfig.app.json ` to ` tsconfig.server.json ` and change it to build with a ` "module" ` target of ` "commonjs" ` .
@@ -249,20 +259,22 @@ In **build target**, adapt `options.outputPath` to `dist/browser`.
249259 ...
250260 }
251261 "server": {
252- "builder": "@angular-devkit/build-angular:server",
253- "options": {
254- "outputPath": "dist/server",
255- "main": "src/main.server.ts",
256- "tsConfig": "tsconfig.server.json",
257- "fileReplacements": [
258- {
259- "replace": "src/environments/environment.ts",
260- "with": "src/environments/environment.prod.ts"
261- }
262- ],
263- "optimization": true,
264- "sourceMap": false
265- }
262+ "builder": "@angular-devkit/build-angular:server",
263+ "options": {
264+ "outputPath": "dist/server",
265+ "main": "src/main.server.ts",
266+ "tsConfig": "tsconfig.server.json"
267+ },
268+ "configurations": {
269+ "production": {
270+ "fileReplacements": [
271+ {
272+ "replace": "src/environments/environment.ts",
273+ "with": "src/environments/environment.prod.ts"
274+ }
275+ ]
276+ }
277+ }
266278 }
267279 }
268280 ...
@@ -313,23 +325,17 @@ At the ROOT level of your project (where package.json / etc are), created a file
313325### server.ts (root project level):
314326
315327``` typescript
316- // These are important and needed before anything else
317- import ' reflect-metadata' ;
328+ // This is important and needed before anything else
318329import ' zone.js/dist/zone-node' ;
319330
320- import { enableProdMode } from ' @angular/core' ;
321331import { Hapiness , Module } from ' @hapiness/core' ;
322332import { HttpServer , HttpServerConfig } from ' @hapiness/core/httpserver' ;
323- import { NgUniversalModule } from ' @hapiness/ng-universal' ;
324333import { join } from ' path' ;
325334
326335const BROWSER_FOLDER = join (process .cwd (), ' dist' , ' browser' );
327336
328- // Faster server renders w/ Prod mode (dev mode never needed)
329- enableProdMode ();
330-
331337// * NOTE :: leave this as require() since this file is built Dynamically from webpack
332- const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require (' ./dist/server/main' );
338+ const { AppServerModuleNgFactory, LAZY_MODULE_MAP, NgUniversalModule } = require (' ./dist/server/main' );
333339
334340// Create our Hapiness application
335341@Module ({
@@ -441,15 +447,13 @@ module.exports = {
441447 optimization: {
442448 minimize: false
443449 },
444- externals: [
445- / (node_modules)/
446- ],
447450 output: {
448451 path: path .join (__dirname , ' dist' ),
449452 filename: ' [name].js' ,
450453 libraryTarget: " commonjs"
451454 },
452455 module: {
456+ noParse: / polyfills-. * \. js/ ,
453457 rules: [
454458 { test: / \. ts$ / , loader: ' ts-loader' },
455459 {
@@ -469,12 +473,7 @@ module.exports = {
469473 {} // a map of your routes
470474 ),
471475 new webpack.ContextReplacementPlugin (
472- / (. + )? hapiness(\\ | \/ )core(. + )? / ,
473- path .join (__dirname , ' src' ),
474- {}
475- ),
476- new webpack.ContextReplacementPlugin (
477- / (. + )? hapiness(\\ | \/ )ng-universal(. + )? / ,
476+ / (. + )? hapiness(\\ | \/ )(. + )? / ,
478477 path .join (__dirname , ' src' ),
479478 {}
480479 )
@@ -485,6 +484,32 @@ module.exports = {
485484};
486485```
487486
487+ You can add this config if you want to use ` @hapiness/config ` to have server config in ` ./config/default.yml ` instead of static data:
488+
489+ ``` javascript
490+ externals: [
491+ {
492+ // This is the only module you have to install with npm in your final packaging
493+ // npm i config
494+ config: {
495+ commonjs: ' config' ,
496+ root: ' config'
497+ }
498+ }
499+ ]
500+ ```
501+
502+ And replace the ` bootstrap ` in * ./server.ts*
503+
504+ ``` typescript
505+ import { Config } from ' @hapiness/config' ;
506+
507+ // Boostrap Hapiness application
508+ Hapiness .bootstrap (HapinessApplication , [
509+ HttpServer .setConfig <HttpServerConfig >(Config .get (' server' ))
510+ ]);
511+ ```
512+
488513Now, you can build your server file:
489514
490515``` bash
@@ -518,7 +543,7 @@ Now lets create a few handy scripts to help us do all of this in the future.
518543 "serve:dynamic": "node dist/server.js",
519544
520545 // Helpers for the above scripts
521- "build:client-and-server-bundles": "ng build --prod && ng run your-project-name:server",
546+ "build:client-and-server-bundles": "ng build --prod && ng run your-project-name:server:production ",
522547 "webpack:server": "webpack --config webpack.server.config.js --progress --colors"
523548}
524549```
@@ -552,6 +577,9 @@ To set up your development environment:
552577[ Back to top] ( #table-of-contents )
553578
554579## Change History
580+ * v8.1.0 (2019-07-04)
581+ * ` Angular v8.1.0+ `
582+ * Documentation to allow dynamic import syntax directly to load lazy loaded chunks
555583* v8.0.0 (2019-05-31)
556584 * ` Angular v8.0.0+ `
557585 * Migrate server to ` Hapiness ` v2 based on [ Fastify] ( https://www.fastify.io/ )
0 commit comments