Skip to content

Commit a27ee9d

Browse files
authored
docs(guides): convert hot module replacement webpack.config.js to esm (#7799)
1 parent 971f71b commit a27ee9d

1 file changed

Lines changed: 31 additions & 14 deletions

File tree

src/content/guides/hot-module-replacement.mdx

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ contributors:
2121
- wizardofhogwarts
2222
- aholzner
2323
- snitin315
24+
- Brennvo
2425

2526
related:
2627
- title: Concepts - Hot Module Replacement
@@ -46,10 +47,14 @@ T> If you took the route of using `webpack-dev-middleware` instead of `webpack-d
4647
**webpack.config.js**
4748

4849
```diff
49-
const path = require('path');
50-
const HtmlWebpackPlugin = require('html-webpack-plugin');
50+
import path from 'node:path';
51+
import { fileURLToPath } from 'node:url';
52+
import HtmlWebpackPlugin from 'html-webpack-plugin';
5153

52-
module.exports = {
54+
const __filename = fileURLToPath(import.meta.url);
55+
const __dirname = path.dirname(__filename);
56+
57+
export default {
5358
entry: {
5459
app: './src/index.js',
5560
- print: './src/print.js',
@@ -77,11 +82,15 @@ you can also provide manual entry points for HMR:
7782
**webpack.config.js**
7883

7984
```diff
80-
const path = require('path');
81-
const HtmlWebpackPlugin = require('html-webpack-plugin');
82-
+ const webpack = require("webpack");
85+
import path from 'node:path';
86+
import { fileURLToPath } from 'node:url';
87+
import HtmlWebpackPlugin from 'html-webpack-plugin';
88+
+ import webpack from 'webpack';
89+
90+
const __filename = fileURLToPath(import.meta.url);
91+
const __dirname = path.dirname(__filename);
8392

84-
module.exports = {
93+
export default {
8594
entry: {
8695
app: './src/index.js',
8796
- print: './src/print.js',
@@ -182,10 +191,14 @@ To enable HMR, you also need to modify your webpack configuration object to incl
182191
**dev-server.js**
183192

184193
```javascript
185-
const path = require("node:path");
186-
const HtmlWebpackPlugin = require("html-webpack-plugin");
187-
const webpack = require("webpack");
188-
const WebpackDevServer = require("webpack-dev-server");
194+
import path from "node:path";
195+
import { fileURLToPath } from "node:url";
196+
import HtmlWebpackPlugin from "html-webpack-plugin";
197+
import webpack from "webpack";
198+
import WebpackDevServer from "webpack-dev-server";
199+
200+
const __filename = fileURLToPath(import.meta.url);
201+
const __dirname = path.dirname(__filename);
189202

190203
const config = {
191204
mode: "development",
@@ -288,10 +301,14 @@ Now let's update the configuration file to make use of the loader.
288301
**webpack.config.js**
289302
290303
```diff
291-
const path = require('path');
292-
const HtmlWebpackPlugin = require('html-webpack-plugin');
304+
import path from 'node:path';
305+
import { fileURLToPath } from 'node:url';
306+
import HtmlWebpackPlugin from 'html-webpack-plugin';
307+
308+
const __filename = fileURLToPath(import.meta.url);
309+
const __dirname = path.dirname(__filename);
293310

294-
module.exports = {
311+
export default {
295312
entry: {
296313
app: './src/index.js',
297314
},

0 commit comments

Comments
 (0)