Skip to content

Commit ac13e58

Browse files
Merge branch 'brandonchinn178-hot-reload'
2 parents 7e67450 + d3f8e84 commit ac13e58

File tree

3 files changed

+381
-18
lines changed

3 files changed

+381
-18
lines changed

content/recipes/hot-reload.md

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@ If you are using the [Nest CLI](https://docs.nestjs.com/cli/overview), the confi
1313
First install the required packages:
1414

1515
```bash
16-
$ npm i --save-dev webpack-node-externals start-server-webpack-plugin
16+
$ npm i --save-dev webpack-node-externals run-script-webpack-plugin webpack
1717
```
1818

19+
> info **Hint** If you use **Yarn Berry** (not classic Yarn), install the `webpack-pnp-externals` package instead of the `webpack-node-externals`.
20+
1921
#### Configuration
2022

2123
Once the installation is complete, create a `webpack-hmr.config.js` file in the root directory of your application.
2224

2325
```typescript
24-
const webpack = require('webpack');
2526
const nodeExternals = require('webpack-node-externals');
26-
const StartServerPlugin = require('start-server-webpack-plugin');
27+
const { RunScriptWebpackPlugin } = require('run-script-webpack-plugin');
2728

28-
module.exports = function(options) {
29+
module.exports = function (options, webpack) {
2930
return {
3031
...options,
3132
entry: ['webpack/hot/poll?100', options.entry],
@@ -38,14 +39,18 @@ module.exports = function(options) {
3839
plugins: [
3940
...options.plugins,
4041
new webpack.HotModuleReplacementPlugin(),
41-
new webpack.WatchIgnorePlugin([/\.js$/, /\.d\.ts$/]),
42-
new StartServerPlugin({ name: options.output.filename }),
42+
new webpack.WatchIgnorePlugin({
43+
paths: [/\.js$/, /\.d\.ts$/],
44+
}),
45+
new RunScriptWebpackPlugin({ name: options.output.filename }),
4346
],
4447
};
4548
};
4649
```
4750

48-
This function takes the original object containing the default webpack configuration and returns a modified one with an applied `HotModuleReplacementPlugin` plugin.
51+
> info **Hint** With **Yarn Berry** (not classic Yarn), instead of using the `nodeExternals` in the `externals` configuration property, use the `WebpackPnpExternals` from `webpack-pnp-externals` package: `WebpackPnpExternals({{ '{' }} exclude: ['webpack/hot/poll?100'] {{ '}' }})`.
52+
53+
This function takes the original object containing the default webpack configuration as a first argument, and the reference to the underlying `webpack` package used by the Nest CLI as the second one. Also, it returns a modified webpack configuration with the `HotModuleReplacementPlugin`, `WatchIgnorePlugin`, and `RunScriptWebpackPlugin` plugins.
4954

5055
#### Hot-Module Replacement
5156

@@ -87,18 +92,22 @@ If you are not using the [Nest CLI](https://docs.nestjs.com/cli/overview), the c
8792
First install the required packages:
8893

8994
```bash
90-
$ npm i --save-dev webpack webpack-cli webpack-node-externals ts-loader start-server-webpack-plugin
95+
$ npm i --save-dev webpack webpack-cli webpack-node-externals ts-loader run-script-webpack-plugin
9196
```
9297

98+
> info **Hint** If you use **Yarn Berry** (not classic Yarn), install the `webpack-pnp-externals` package instead of the `webpack-node-externals`.
99+
93100
#### Configuration
94101

95102
Once the installation is complete, create a `webpack.config.js` file in the root directory of your application.
96103

104+
##### With NPM or Yarn classic
105+
97106
```typescript
98107
const webpack = require('webpack');
99108
const path = require('path');
100109
const nodeExternals = require('webpack-node-externals');
101-
const StartServerPlugin = require('start-server-webpack-plugin');
110+
const { RunScriptWebpackPlugin } = require('run-script-webpack-plugin');
102111

103112
module.exports = {
104113
entry: ['webpack/hot/poll?100', './src/main.ts'],
@@ -124,7 +133,7 @@ module.exports = {
124133
},
125134
plugins: [
126135
new webpack.HotModuleReplacementPlugin(),
127-
new StartServerPlugin({ name: 'server.js' }),
136+
new RunScriptWebpackPlugin({ name: 'server.js' }),
128137
],
129138
output: {
130139
path: path.join(__dirname, 'dist'),
@@ -133,6 +142,8 @@ module.exports = {
133142
};
134143
```
135144

145+
> info **Hint** With **Yarn Berry** (not classic Yarn), instead of using the `nodeExternals` in the `externals` configuration property, use the `WebpackPnpExternals` from `webpack-pnp-externals` package: `WebpackPnpExternals({{ '{' }} exclude: ['webpack/hot/poll?100'] {{ '}' }})`.
146+
136147
This configuration tells webpack a few essential things about your application: location of the entry file, which directory should be used to hold **compiled** files, and what kind of loader we want to use to compile source files. Generally, you should be able to use this file as-is, even if you don't fully understand all of the options.
137148

138149
#### Hot-Module Replacement

0 commit comments

Comments
 (0)