Skip to content

Commit cbadf09

Browse files
alan-agius4kyliau
authored andcommitted
docs: update universal rendering story
Closes: #9202
1 parent 081edf3 commit cbadf09

File tree

1 file changed

+47
-48
lines changed

1 file changed

+47
-48
lines changed

docs/documentation/stories/universal-rendering.md

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ This story will show you how to set up Universal bundling for an existing `@angu
2020

2121
Install `@angular/platform-server` into your project. Make sure you use the same version as the other `@angular` packages in your project.
2222

23-
> You'll also need ts-loader (for your webpack build we'll show later) and @nguniversal/module-map-ngfactory-loader, as it's used to handle lazy-loading in the context of a server-render. (by loading the chunks right away)
23+
> You'll also need @nguniversal/module-map-ngfactory-loader, as it's used to handle lazy-loading in the context of a server-render. (by loading the chunks right away)
2424
2525
```bash
2626
$ npm install --save @angular/platform-server @nguniversal/module-map-ngfactory-loader express
27-
$ npm install --save-dev ts-loader webpack-cli
2827
```
2928

3029
## Step 1: Prepare your App for Universal rendering
@@ -197,6 +196,30 @@ app.engine('html', ngExpressEngine({
197196
}));
198197
```
199198

199+
In case you want to use an AppShell with SSR and Lazy loaded modules you need to configure `NgModuleFactoryLoader` as a provider.
200+
201+
For more context see: https://github.com/angular/angular-cli/issues/9202
202+
203+
```typescript
204+
import { Compiler, NgModuleFactoryLoader } from '@angular/core';
205+
import { provideModuleMap, ModuleMapNgFactoryLoader, MODULE_MAP } from '@nguniversal/module-map-ngfactory-loader';
206+
207+
app.engine('html', ngExpressEngine({
208+
bootstrap: AppServerModuleNgFactory,
209+
providers: [
210+
provideModuleMap(LAZY_MODULE_MAP),
211+
{
212+
provide: NgModuleFactoryLoader,
213+
useClass: ModuleMapNgFactoryLoader,
214+
deps: [
215+
Compiler,
216+
MODULE_MAP
217+
],
218+
},
219+
]
220+
}));
221+
```
222+
200223
Below we can see a TypeScript implementation of a -very- simple Express server to fire everything up.
201224

202225
> Note: This is a very bare bones Express application, and is just for demonstrations sake. In a real production environment, you'd want to make sure you have other authentication and security things setup here as well. This is just meant just to show the specific things needed that are relevant to Universal itself. The rest is up to you!
@@ -263,60 +286,36 @@ app.listen(PORT, () => {
263286
});
264287
```
265288

266-
## Step 5: Setup a webpack config to handle this Node server.ts file and serve your application!
289+
## Step 5: Setup a TypeScript config to handle this Node server.ts file and serve your application!
267290

268291
Now that we have our Node Express server setup, we need to pack it and serve it!
269292

270-
Create a file named `webpack.server.config.js` at the ROOT of your application.
293+
Create a file named `server.tsconfig.json` at the ROOT of your application.
271294

272295
> This file basically takes that server.ts file, and takes it and compiles it and every dependency it has into `dist/server.js`.
273296
274-
### ./webpack.server.config.js (root project level)
297+
### ./server.tsconfig.json (root project level)
275298

276299
```typescript
277-
const path = require('path');
278-
const webpack = require('webpack');
279-
280-
module.exports = {
281-
mode: 'none',
282-
entry: {
283-
server: './server.ts',
284-
},
285-
target: 'node',
286-
resolve: { extensions: ['.ts', '.js'] },
287-
optimization: {
288-
minimize: false
289-
},
290-
output: {
291-
// Puts the output at the root of the dist folder
292-
path: path.join(__dirname, 'dist'),
293-
filename: '[name].js'
294-
},
295-
module: {
296-
rules: [
297-
{ test: /\.ts$/, loader: 'ts-loader' },
298-
{
299-
// Mark files inside `@angular/core` as using SystemJS style dynamic imports.
300-
// Removing this will cause deprecation warnings to appear.
301-
test: /(\\|\/)@angular(\\|\/)core(\\|\/).+\.js$/,
302-
parser: { system: true },
303-
},
300+
{
301+
"compileOnSave": false,
302+
"compilerOptions": {
303+
"outDir": "./dist",
304+
"sourceMap": true,
305+
"declaration": false,
306+
"moduleResolution": "node",
307+
"emitDecoratorMetadata": true,
308+
"experimentalDecorators": true,
309+
"target": "es5",
310+
"typeRoots": [
311+
"node_modules/@types"
312+
],
313+
"lib": [
314+
"es2017",
315+
"dom"
304316
]
305317
},
306-
plugins: [
307-
new webpack.ContextReplacementPlugin(
308-
// fixes WARNING Critical dependency: the request of a dependency is an expression
309-
/(.+)?angular(\\|\/)core(.+)?/,
310-
path.join(__dirname, 'src'), // location of your src
311-
{} // a map of your routes
312-
),
313-
new webpack.ContextReplacementPlugin(
314-
// fixes WARNING Critical dependency: the request of a dependency is an expression
315-
/(.+)?express(\\|\/)(.+)?/,
316-
path.join(__dirname, 'src'),
317-
{}
318-
)
319-
]
318+
"include": ["server.ts"]
320319
}
321320
```
322321

@@ -343,12 +342,12 @@ Now lets create a few handy scripts to help us do all of this in the future.
343342
```json
344343
"scripts": {
345344
// These will be your common scripts
346-
"build:ssr": "npm run build:client-and-server-bundles && npm run webpack:server",
345+
"build:ssr": "npm run build:client-and-server-bundles && npm run compile:server",
347346
"serve:ssr": "node dist/server.js",
348347

349348
// Helpers for the above scripts
350349
"build:client-and-server-bundles": "ng build --prod && ng run your-project-name:server",
351-
"webpack:server": "webpack --config webpack.server.config.js --progress --colors"
350+
"compile:server": "tsc -p server.tsconfig.json",
352351
}
353352
```
354353

0 commit comments

Comments
 (0)