13 |
14 | ## package.json npm file
15 |
16 | The webpack-dev-server from Kees Kluskens is added to the devDependencies in the npm package.json file. The webpack-dev-server package implements and supports the HMR feature.
17 | ```javascript
18 | "devDependencies": {
19 | ...
20 | "webpack": "^3.2.0",
21 | "webpack-dev-server": "^2.5.1",
22 | },
23 | ```
24 |
25 | In the scripts section of the package.json, the start command is configured to start the dotnet server and also the webpack-dev-server with the --hot and the --inline parameters.
26 |
27 | See the webpack-dev-server documentation for more information about the possible parameters.
28 |
29 | The dotnet server is only required because this demo application uses a Web API service implemented in ASP.NET Core.
30 |
31 | ```javascript
32 | "start": "concurrently \"webpack-dev-server --hot --inline --port 8080\" \"dotnet run\" "
33 | ```
34 |
35 | ## webpack dev configuration
36 |
37 | The devServer is added to the module.exports in the webpack.dev.js. This configures the webpack-dev-server as required. The webpack-dev-server configuration can be set here as well as the command line options, so you as a developer can decide which is better for you.
38 | ```javascript
39 | devServer: {
40 | historyApiFallback: true,
41 | contentBase: path.join(__dirname, '/wwwroot/'),
42 | watchOptions: {
43 | aggregateTimeout: 300,
44 | poll: 1000
45 | }
46 | },
47 | ```
48 |
49 | The output in the module.exports also needs to be configured correctly for the webpack-dev-server to work correctly. If the './' path is used in the path option of the output section, the webpack-dev-server will not start.
50 | ```javascript
51 | output: {
52 | path: __dirname + '/wwwroot/',
53 | filename: 'dist/[name].bundle.js',
54 | chunkFilename: 'dist/[id].chunk.js',
55 | publicPath: '/'
56 | },
57 | ```
58 |
59 | ## Running the application
60 |
61 | Build the application using the webpack dev build. This can be done in the command line. Before building, you need to install all the npm packages using npm install.
62 | ```javascript
63 | $ npm run build-dev
64 | ```
65 |
66 | The npm script build-dev is defined in the package.json file and uses the webpack-dev script which does a development build.
67 | ```javascript
68 | "build-dev": "npm run webpack-dev",
69 | "webpack-dev": "set NODE_ENV=development && webpack",
70 | ```
71 |
72 | Now the server can be started using the start script.
73 | ```javascript
74 | $ npm start
75 | ```
76 |
77 |
78 |
79 | The application is now running on localhost with port 8080 as defined.
80 |
81 | http://localhost:8080/home
82 |
83 | If for example, the color is changed in the app.scss, the bundles will be reloaded in the browser without refreshing.
84 |
85 |
86 | ## Links
87 |
88 | https://webpack.js.org/concepts/hot-module-replacement/
89 |
90 | https://webpack.js.org/configuration/dev-server/#devserver
91 |
92 | https://github.com/webpack/webpack-dev-server
93 |
94 | https://www.sitepoint.com/beginners-guide-to-webpack-2-and-module-bundling/
95 |
96 | https://medium.com/@rajaraodv/webpack-hot-module-replacement-hmr-e756a726a07
97 |
--------------------------------------------------------------------------------
/docs/LAZY_LOADING.md:
--------------------------------------------------------------------------------
1 |
2 | # Angular Lazy Loading with Webpack 3
3 |
4 | This documentation shows how Angular lazy loading can be supported using Webpack 3 for both JIT and AOT builds. The Webpack loader angular-router-loader from Brandon Roberts is used to implement this.
5 |
6 | A big thanks to Roberto Simonetti for his help in this.
7 |
8 |
9 | Code VS2017 angular 4.x
10 |
11 |
12 |