Skip to content

Commit a8e449d

Browse files
committed
docs(concepts): convert CommonJS examples to ESM in output and plugins
1 parent ef111b7 commit a8e449d

2 files changed

Lines changed: 20 additions & 12 deletions

File tree

src/content/concepts/output.mdx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The minimum requirement for the `output` property in your webpack configuration
2020
**webpack.config.js**
2121

2222
```javascript
23-
module.exports = {
23+
export default {
2424
output: {
2525
filename: "bundle.js",
2626
},
@@ -34,9 +34,13 @@ This configuration would output a single `bundle.js` file into the `dist` direct
3434
If your configuration creates more than a single "chunk" (as with multiple entry points or when using plugins like CommonsChunkPlugin), you should use [substitutions](/configuration/output/#outputfilename) to ensure that each file has a unique name.
3535

3636
```javascript
37-
const path = require("node:path");
37+
import path from "node:path";
38+
import { fileURLToPath } from "node:url";
3839

39-
module.exports = {
40+
const __filename = fileURLToPath(import.meta.url);
41+
const __dirname = path.dirname(__filename);
42+
43+
export default {
4044
entry: {
4145
app: "./src/app.js",
4246
search: "./src/search.js",
@@ -57,7 +61,7 @@ Here's a more complicated example of using a CDN and hashes for assets:
5761
**config.js**
5862
5963
```javascript
60-
module.exports = {
64+
export default {
6165
// ...
6266
output: {
6367
path: "/home/proj/cdn/assets/[fullhash]",

src/content/concepts/plugins.mdx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ contributors:
1515

1616
They also serve the purpose of doing **anything else** that a [loader](/concepts/loaders) cannot do. Webpack provides [many such plugins](/plugins/) out of the box.
1717

18-
T> When consuming [`webpack-sources`](https://github.com/webpack/webpack-sources) package in plugins, use `require('webpack').sources` instead of `require('webpack-sources')` to avoid version conflicts for persistent caching.
18+
T> When consuming [`webpack-sources`](https://github.com/webpack/webpack-sources) package in plugins, use `import { sources } from "webpack"` instead of importing from `webpack-sources` to avoid version conflicts for persistent caching.
1919

2020
## Anatomy
2121

@@ -34,7 +34,7 @@ class ConsoleLogOnBuildWebpackPlugin {
3434
}
3535
}
3636

37-
module.exports = ConsoleLogOnBuildWebpackPlugin;
37+
export default ConsoleLogOnBuildWebpackPlugin;
3838
```
3939

4040
It is recommended that the first parameter of the tap method of the compiler hook should be a camelized version of the plugin name. It is advisable to use a constant for this so it can be reused in all hooks.
@@ -50,11 +50,15 @@ Depending on how you are using webpack, there are multiple ways to use plugins.
5050
**webpack.config.js**
5151

5252
```javascript
53-
const path = require("node:path");
54-
const HtmlWebpackPlugin = require("html-webpack-plugin");
55-
const webpack = require("webpack"); // to access built-in plugins
53+
import path from "node:path";
54+
import { fileURLToPath } from "node:url";
55+
import HtmlWebpackPlugin from "html-webpack-plugin";
56+
import webpack from "webpack"; // to access built-in plugins
5657

57-
module.exports = {
58+
const __filename = fileURLToPath(import.meta.url);
59+
const __dirname = path.dirname(__filename);
60+
61+
export default {
5862
entry: "./path/to/my/entry/file.js",
5963
output: {
6064
filename: "my-first-webpack.bundle.js",
@@ -84,8 +88,8 @@ When using the Node API, you can also pass plugins via the `plugins` property in
8488
**some-node-script.js**
8589
8690
```javascript
87-
const webpack = require("webpack"); // to access webpack runtime
88-
const configuration = require("./webpack.config.js");
91+
import webpack from "webpack"; // to access webpack runtime
92+
import configuration from "./webpack.config.js";
8993

9094
const compiler = webpack(configuration);
9195

0 commit comments

Comments
 (0)