Skip to content

Commit 6aff217

Browse files
alan-agius4Keen Yee Liau
authored andcommitted
docs: update universal rendering story
Closes: angular#9202
1 parent abf99b5 commit 6aff217

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
@@ -22,11 +22,10 @@ This story will show you how to set up Universal bundling for an existing `@angu
2222

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

25-
> 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)
25+
> 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)
2626
2727
```bash
2828
$ npm install --save @angular/platform-server @nguniversal/module-map-ngfactory-loader express
29-
$ npm install --save-dev ts-loader webpack-cli
3029
```
3130

3231
## Step 1: Prepare your App for Universal rendering
@@ -199,6 +198,30 @@ app.engine('html', ngExpressEngine({
199198
}));
200199
```
201200

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

204227
> 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!
@@ -265,60 +288,36 @@ app.listen(PORT, () => {
265288
});
266289
```
267290

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

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

272-
Create a file named `webpack.server.config.js` at the ROOT of your application.
295+
Create a file named `server.tsconfig.json` at the ROOT of your application.
273296

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

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

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

351350
// Helpers for the above scripts
352351
"build:client-and-server-bundles": "ng build --prod && ng run your-project-name:server",
353-
"webpack:server": "webpack --config webpack.server.config.js --progress --colors"
352+
"compile:server": "tsc -p server.tsconfig.json",
354353
}
355354
```
356355

0 commit comments

Comments
 (0)