Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update docs for the extends option #6794

Merged
merged 9 commits into from
May 8, 2023
10 changes: 10 additions & 0 deletions src/content/api/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ contributors:
- smelukov
- anikethsaha
- jamesgeorge007
- burhanuday
related:
- title: Analyzing Build Statistics
url: https://survivejs.com/webpack/optimizing-build/analyzing-build-statistics/
Expand Down Expand Up @@ -270,6 +271,7 @@ By default webpack ships with the following flags:
| `--disable-interpret` | boolean | Disable interpret for loading the config file. |
| `--fail-on-warnings` | boolean | Stop webpack-cli process with non-zero exit code on warnings from webpack |
| [`--analyze`](#analyzing-bundle) | boolean | It invokes `webpack-bundle-analyzer` plugin to get bundle information |
| [`--extends, -e`](#extends) | string[] | Extend an existing configuration |

### Negated Flags

Expand Down Expand Up @@ -540,6 +542,14 @@ You can merge two or more different webpack configurations with the help of `--m
npx webpack --config ./first.js --config ./second.js --merge
```

### extends

You can extend existing webpack configurations with the help of `--extends`:
snitin315 marked this conversation as resolved.
Show resolved Hide resolved

```bash
npx webpack --extends ./base.webpack.config.js
```

### json

**Print result of webpack as JSON**
Expand Down
119 changes: 119 additions & 0 deletions src/content/configuration/extending-configurations.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
---
title: Extends
sort: 12
contributors:
- burhanuday
---

## extends

`string | string[]`

The `extends` property allows you to extend an existing configuration to use as base. It internally uses the `webpack-merge` package to merge the configurations and helps you to avoid duplicating configuration between multiple configurations.

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.
burhanuday marked this conversation as resolved.
Show resolved Hide resolved

**base.webpack.config.js**

```javascript
module.exports = {
module: {
rules: [
{
test: /\.js$/,
use: 'babel-loader',
exclude: /node_modules/,
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
],
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production'),
}),
],
};
```

**webpack.config.js**

```javascript
module.exports = {
extends: './base.webpack.config.js',
burhanuday marked this conversation as resolved.
Show resolved Hide resolved
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
};
```

## Extending multiple configurations

You can extend multiple configurations at once by passing an array of configuration paths to the `extends` property.

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 overriden by passing the same property in the configuration on the right.
burhanuday marked this conversation as resolved.
Show resolved Hide resolved

**js.webpack.config.js**

```javascript
module.exports = {
module: {
rules: [
{
test: /\.js$/,
use: 'babel-loader',
exclude: /node_modules/,
},
],
},
};
```

**css.webpack.config.js**

```javascript
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
],
},
};
```

**webpack.config.js**

```javascript
module.exports = {
extends: ['./js.webpack.config.js', './css.webpack.config.js'],
burhanuday marked this conversation as resolved.
Show resolved Hide resolved
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
};
```

## Loading configuration from external packages

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.
burhanuday marked this conversation as resolved.
Show resolved Hide resolved

**webpack.config.js**

```javascript
module.exports = {
extends: require.resolve('webpack-config-foo'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need better documentation here on how to create and publish webpack-config-foo. But we can add it later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need better documentation here on how to create and publish webpack-config-foo. But we can add it later.

More work needs to be done here. Ideally, consumers of a config should be able to pass options like we do for plugins.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll create a new issue for this on the webpack-cli project

entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
};
```