Skip to content

Commit e5a74ae

Browse files
docs(configuration): update docs for the extends option (#6794)
Co-authored-by: Nitin Kumar <[email protected]>
1 parent 0be627d commit e5a74ae

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed

src/content/api/cli.mdx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ contributors:
1818
- smelukov
1919
- anikethsaha
2020
- jamesgeorge007
21+
- burhanuday
2122
related:
2223
- title: Analyzing Build Statistics
2324
url: https://survivejs.com/webpack/optimizing-build/analyzing-build-statistics/
@@ -270,6 +271,7 @@ By default webpack ships with the following flags:
270271
| `--disable-interpret` | boolean | Disable interpret for loading the config file. |
271272
| `--fail-on-warnings` | boolean | Stop webpack-cli process with non-zero exit code on warnings from webpack |
272273
| [`--analyze`](#analyzing-bundle) | boolean | It invokes `webpack-bundle-analyzer` plugin to get bundle information |
274+
| [`--extends, -e`](#extends) | string[] | Extend an existing configuration |
273275

274276
### Negated Flags
275277

@@ -540,6 +542,16 @@ You can merge two or more different webpack configurations with the help of `--m
540542
npx webpack --config ./first.js --config ./second.js --merge
541543
```
542544

545+
### extends
546+
547+
<Badge text="webpack-cli v5.1.0+" />
548+
549+
You can extend existing webpack configurations with the help of `--extends`:
550+
551+
```bash
552+
npx webpack --extends ./base.webpack.config.js
553+
```
554+
543555
### json
544556

545557
**Print result of webpack as JSON**
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
---
2+
title: Extends
3+
sort: 12
4+
contributors:
5+
- burhanuday
6+
---
7+
8+
## extends
9+
10+
`string | string[]`
11+
12+
<Badge text="webpack v5.82.0+" /> <Badge text="webpack-cli v5.1.0+" />
13+
14+
The `extends` property allows you to extend an existing configuration to use as the base. It internally uses the `webpack-merge` package to merge the configurations and helps you to avoid duplicating configurations between multiple configurations.
15+
16+
W> **This option is not supported via the Node API**: Extends will have no effect when using the Node API. webpack-cli is required to use this feature.
17+
18+
**base.webpack.config.js**
19+
20+
```javascript
21+
module.exports = {
22+
module: {
23+
rules: [
24+
{
25+
test: /\.js$/,
26+
use: 'babel-loader',
27+
exclude: /node_modules/,
28+
},
29+
{
30+
test: /\.css$/,
31+
use: ['style-loader', 'css-loader'],
32+
},
33+
],
34+
},
35+
plugins: [
36+
new webpack.DefinePlugin({
37+
'process.env.NODE_ENV': JSON.stringify('production'),
38+
}),
39+
],
40+
};
41+
```
42+
43+
**webpack.config.js**
44+
45+
```javascript
46+
module.exports = {
47+
extends: path.resolve(__dirname, './base.webpack.config.js'),
48+
entry: './src/index.js',
49+
output: {
50+
path: path.resolve(__dirname, 'dist'),
51+
filename: 'bundle.js',
52+
},
53+
};
54+
```
55+
56+
## Extending multiple configurations
57+
58+
You can extend multiple configurations at once by passing an array of configuration paths to the `extends` property.
59+
60+
Configurations from the `extends` property are merged from right to left, meaning that the configuration on the right will be merged into the configuration on the left. Configuration can be overridden by passing the same property in the configuration on the right.
61+
62+
**js.webpack.config.js**
63+
64+
```javascript
65+
module.exports = {
66+
module: {
67+
rules: [
68+
{
69+
test: /\.js$/,
70+
use: 'babel-loader',
71+
exclude: /node_modules/,
72+
},
73+
],
74+
},
75+
};
76+
```
77+
78+
**css.webpack.config.js**
79+
80+
```javascript
81+
module.exports = {
82+
module: {
83+
rules: [
84+
{
85+
test: /\.css$/,
86+
use: ['style-loader', 'css-loader'],
87+
},
88+
],
89+
},
90+
};
91+
```
92+
93+
**webpack.config.js**
94+
95+
```javascript
96+
module.exports = {
97+
extends: [
98+
path.resolve(__dirname, './js.webpack.config.js'),
99+
path.resolve(__dirname, './css.webpack.config.js'),
100+
],
101+
entry: './src/index.js',
102+
output: {
103+
path: path.resolve(__dirname, 'dist'),
104+
filename: 'bundle.js',
105+
},
106+
};
107+
```
108+
109+
## Overridding Configurations
110+
111+
You can override configurations from the extended configuration by passing the same property in the configuration that extends it.
112+
113+
**base.webpack.config.js**
114+
115+
```javascript
116+
module.exports = {
117+
output: {
118+
path: path.resolve(__dirname, 'dist'),
119+
filename: 'bundle.js',
120+
},
121+
};
122+
```
123+
124+
**webpack.config.js**
125+
126+
```javascript
127+
module.exports = {
128+
extends: path.resolve(__dirname, './base.webpack.config.js'),
129+
entry: './src/index.js',
130+
// overriding the output path and filename
131+
output: {
132+
path: path.resolve(__dirname, 'build'),
133+
filename: '[name].bundle.js',
134+
},
135+
};
136+
```
137+
138+
## Loading configuration from external packages
139+
140+
You can also load configuration from third-party packages by passing the package name to the `extends` property. The package must export the webpack configuration in package.json.
141+
142+
**webpack.config.js**
143+
144+
```javascript
145+
module.exports = {
146+
extends: require.resolve('webpack-config-foo'),
147+
entry: './src/index.js',
148+
output: {
149+
path: path.resolve(__dirname, 'dist'),
150+
filename: 'bundle.js',
151+
},
152+
};
153+
```

0 commit comments

Comments
 (0)