-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
docs: update docs for the extends option #6794
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cf5c3c0
docs: update docs for the extends option
burhanuday 6cca80a
docs: remove section on external packages
burhanuday 646131c
docs: add fixed docs for external packages
burhanuday 33750b3
docs: add warning for node api
burhanuday ddcf06f
docs: add version after which extends property is supported
burhanuday 0061706
Apply suggestions from code review
burhanuday 66a9d8b
docs: added docs on extending a configuration
burhanuday 39b1458
docs: fix CLI version
burhanuday 41cc0ee
Update src/content/api/cli.mdx
snitin315 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
--- | ||
title: Extends | ||
sort: 12 | ||
contributors: | ||
- burhanuday | ||
--- | ||
|
||
## extends | ||
|
||
`string | string[]` | ||
|
||
<Badge text="webpack v5.82.0+" /> <Badge text="webpack-cli v5.1.0+" /> | ||
|
||
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. | ||
|
||
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. | ||
|
||
**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: path.resolve(__dirname, './base.webpack.config.js'), | ||
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 overridden by passing the same property in the configuration on the right. | ||
|
||
**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: [ | ||
path.resolve(__dirname, './js.webpack.config.js'), | ||
path.resolve(__dirname, './css.webpack.config.js'), | ||
], | ||
entry: './src/index.js', | ||
output: { | ||
path: path.resolve(__dirname, 'dist'), | ||
filename: 'bundle.js', | ||
}, | ||
}; | ||
``` | ||
|
||
## Overridding Configurations | ||
|
||
You can override configurations from the extended configuration by passing the same property in the configuration that extends it. | ||
|
||
**base.webpack.config.js** | ||
|
||
```javascript | ||
module.exports = { | ||
output: { | ||
path: path.resolve(__dirname, 'dist'), | ||
filename: 'bundle.js', | ||
}, | ||
}; | ||
``` | ||
|
||
**webpack.config.js** | ||
|
||
```javascript | ||
module.exports = { | ||
extends: path.resolve(__dirname, './base.webpack.config.js'), | ||
entry: './src/index.js', | ||
// overriding the output path and filename | ||
output: { | ||
path: path.resolve(__dirname, 'build'), | ||
filename: '[name].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. | ||
|
||
**webpack.config.js** | ||
|
||
```javascript | ||
module.exports = { | ||
extends: require.resolve('webpack-config-foo'), | ||
entry: './src/index.js', | ||
output: { | ||
path: path.resolve(__dirname, 'dist'), | ||
filename: 'bundle.js', | ||
}, | ||
}; | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More work needs to be done here. Ideally, consumers of a config should be able to pass options like we do for plugins.
There was a problem hiding this comment.
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