Skip to content

Commit 6de7d3e

Browse files
docs(hot-reload): Add separate instructions for Yarn berry
1 parent 68370c1 commit 6de7d3e

File tree

1 file changed

+104
-2
lines changed

1 file changed

+104
-2
lines changed

content/recipes/hot-reload.md

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,32 @@ If you are using the [Nest CLI](https://docs.nestjs.com/cli/overview), the confi
1010

1111
#### Installation
1212

13-
First install the required packages:
13+
First install the required packages.
14+
15+
##### With NPM
1416

1517
```bash
1618
$ npm i --save-dev webpack-node-externals run-script-webpack-plugin webpack
1719
```
1820

21+
##### With Yarn classic
22+
23+
```bash
24+
$ yarn add -D webpack-node-externals run-script-webpack-plugin webpack
25+
```
26+
27+
##### With Yarn berry
28+
29+
```bash
30+
$ yarn add -D webpack-pnp-externals run-script-webpack-plugin webpack
31+
```
32+
1933
#### Configuration
2034

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

37+
##### With NPM or Yarn classic
38+
2339
```typescript
2440
const webpack = require('webpack');
2541
const nodeExternals = require('webpack-node-externals');
@@ -47,6 +63,35 @@ module.exports = function(options) {
4763
};
4864
```
4965

66+
##### With Yarn berry
67+
68+
```typescript
69+
const webpack = require('webpack');
70+
const { WebpackPnpExternals } = require('webpack-pnp-externals');
71+
const { RunScriptWebpackPlugin } = require('run-script-webpack-plugin');
72+
73+
module.exports = function(options) {
74+
return {
75+
...options,
76+
entry: ['webpack/hot/poll?100', options.entry],
77+
watch: true,
78+
externals: [
79+
WebpackPnpExternals({
80+
exclude: ['webpack/hot/poll?100'],
81+
}),
82+
],
83+
plugins: [
84+
...options.plugins,
85+
new webpack.HotModuleReplacementPlugin(),
86+
new webpack.WatchIgnorePlugin({
87+
paths: [/\.js$/, /\.d\.ts$/]
88+
}),
89+
new RunScriptWebpackPlugin({ name: options.output.filename }),
90+
],
91+
};
92+
};
93+
```
94+
5095
This function takes the original object containing the default webpack configuration and returns a modified one with an applied `HotModuleReplacementPlugin` plugin.
5196

5297
#### Hot-Module Replacement
@@ -86,16 +131,32 @@ If you are not using the [Nest CLI](https://docs.nestjs.com/cli/overview), the c
86131

87132
#### Installation
88133

89-
First install the required packages:
134+
First install the required packages.
135+
136+
##### With NPM
90137

91138
```bash
92139
$ npm i --save-dev webpack webpack-cli webpack-node-externals ts-loader run-script-webpack-plugin
93140
```
94141

142+
##### With Yarn classic
143+
144+
```bash
145+
$ yarn add -D webpack webpack-cli webpack-node-externals ts-loader run-script-webpack-plugin
146+
```
147+
148+
##### With Yarn berry
149+
150+
```bash
151+
$ yarn add -D webpack webpack-cli webpack-pnp-externals ts-loader run-script-webpack-plugin
152+
```
153+
95154
#### Configuration
96155

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

158+
##### With NPM or Yarn classic
159+
99160
```typescript
100161
const webpack = require('webpack');
101162
const path = require('path');
@@ -135,6 +196,47 @@ module.exports = {
135196
};
136197
```
137198

199+
##### With Yarn berry
200+
201+
```typescript
202+
const webpack = require('webpack');
203+
const path = require('path');
204+
const { WebpackPnpExternals } = require('webpack-pnp-externals');
205+
const { RunScriptWebpackPlugin } = require('run-script-webpack-plugin');
206+
207+
module.exports = {
208+
entry: ['webpack/hot/poll?100', './src/main.ts'],
209+
watch: true,
210+
target: 'node',
211+
externals: [
212+
WebpackPnpExternals({
213+
exclude: ['webpack/hot/poll?100'],
214+
}),
215+
],
216+
module: {
217+
rules: [
218+
{
219+
test: /.tsx?$/,
220+
use: 'ts-loader',
221+
exclude: /node_modules/,
222+
},
223+
],
224+
},
225+
mode: 'development',
226+
resolve: {
227+
extensions: ['.tsx', '.ts', '.js'],
228+
},
229+
plugins: [
230+
new webpack.HotModuleReplacementPlugin(),
231+
new RunScriptWebpackPlugin({ name: 'server.js' }),
232+
],
233+
output: {
234+
path: path.join(__dirname, 'dist'),
235+
filename: 'server.js',
236+
},
237+
};
238+
```
239+
138240
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.
139241

140242
#### Hot-Module Replacement

0 commit comments

Comments
 (0)