|
| 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