Skip to content

Commit 3086ae9

Browse files
alan-agius4alexeagle
authored andcommitted
docs: update universal story
1 parent 3b4352c commit 3086ae9

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

docs/documentation/stories/universal-rendering.md

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ Install `@angular/platform-server` into your project. Make sure you use the same
2323
> 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)
2424
2525
```bash
26-
$ npm install --save @angular/platform-server @nguniversal/module-map-ngfactory-loader ts-loader
26+
$ npm install --save @angular/platform-server @nguniversal/module-map-ngfactory-loader express
27+
$ npm install --save-dev ts-loader webpack-cli
2728
```
2829

2930
## Step 1: Prepare your App for Universal rendering
@@ -33,7 +34,7 @@ The first thing you need to do is make your `AppModule` compatible with Universa
3334

3435
### src/app/app.module.ts:
3536

36-
```javascript
37+
```typescript
3738
@NgModule({
3839
bootstrap: [AppComponent],
3940
imports: [
@@ -143,7 +144,7 @@ target:
143144

144145
## Building the bundle
145146

146-
With these steps complete, you should be able to build a server bundle for your application, using the `--app` flag to tell the CLI to build the server bundle, referencing its index of `1` in the `"apps"` array in `.angular-cli.json`:
147+
With these steps complete, you should be able to build a server bundle for your application, using your project name and command name from `angular.json` to tell the CLI to build the server bundle.
147148

148149
```bash
149150
# This builds your project using the server target, and places the output
@@ -216,6 +217,9 @@ import * as express from 'express';
216217
import { join } from 'path';
217218
import { readFileSync } from 'fs';
218219

220+
// Import module map for lazy loading
221+
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';
222+
219223
// Faster server renders w/ Prod mode (dev mode never needed)
220224
enableProdMode();
221225

@@ -228,11 +232,6 @@ const DIST_FOLDER = join(process.cwd(), 'dist');
228232
// Our index.html we'll use as our template
229233
const template = readFileSync(join(DIST_FOLDER, 'browser', 'index.html')).toString();
230234

231-
// * NOTE :: leave this as require() since this file is built Dynamically from webpack
232-
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main.bundle');
233-
234-
const { provideModuleMap } = require('@nguniversal/module-map-ngfactory-loader');
235-
236235
app.engine('html', (_, options, callback) => {
237236
renderModuleFactory(AppServerModuleNgFactory, {
238237
// Our index.html
@@ -279,29 +278,40 @@ const path = require('path');
279278
const webpack = require('webpack');
280279

281280
module.exports = {
282-
entry: { server: './server.ts' },
283-
resolve: { extensions: ['.js', '.ts'] },
281+
mode: 'none',
282+
entry: {
283+
server: './server.ts',
284+
},
284285
target: 'node',
285-
// this makes sure we include node_modules and other 3rd party libraries
286-
externals: [/(node_modules|main\..*\.js)/],
286+
resolve: { extensions: ['.ts', '.js'] },
287+
optimization: {
288+
minimize: false
289+
},
287290
output: {
291+
// Puts the output at the root of the dist folder
288292
path: path.join(__dirname, 'dist'),
289293
filename: '[name].js'
290294
},
291295
module: {
292296
rules: [
293-
{ test: /\.ts$/, loader: 'ts-loader' }
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+
},
294304
]
295305
},
296306
plugins: [
297-
// Temporary Fix for issue: https://github.com/angular/angular/issues/11580
298-
// for "WARNING Critical dependency: the request of a dependency is an expression"
299307
new webpack.ContextReplacementPlugin(
308+
// fixes WARNING Critical dependency: the request of a dependency is an expression
300309
/(.+)?angular(\\|\/)core(.+)?/,
301310
path.join(__dirname, 'src'), // location of your src
302311
{} // a map of your routes
303312
),
304313
new webpack.ContextReplacementPlugin(
314+
// fixes WARNING Critical dependency: the request of a dependency is an expression
305315
/(.+)?express(\\|\/)(.+)?/,
306316
path.join(__dirname, 'src'),
307317
{}
@@ -332,13 +342,12 @@ Now lets create a few handy scripts to help us do all of this in the future.
332342

333343
```json
334344
"scripts": {
335-
336345
// These will be your common scripts
337346
"build:ssr": "npm run build:client-and-server-bundles && npm run webpack:server",
338347
"serve:ssr": "node dist/server.js",
339348

340349
// Helpers for the above scripts
341-
"build:client-and-server-bundles": "ng build --prod && ng build --prod --app 1 --output-hashing=false",
350+
"build:client-and-server-bundles": "ng build --prod && ng run your-project-name:server",
342351
"webpack:server": "webpack --config webpack.server.config.js --progress --colors"
343352
}
344353
```

0 commit comments

Comments
 (0)