字串 | 字串陣列
extends
屬性允許你擴充現有的組態作為基礎。它內部使用 webpack-merge
套件來合併組態,並幫助你避免在多個組態之間重複組態。
base.webpack.config.js
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
module.exports = {
extends: path.resolve(__dirname, './base.webpack.config.js'),
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
};
你可以一次擴充多個組態,方法是將組態路徑陣列傳遞給 extends
屬性。
來自 extends
屬性的組態從右到左合併,表示右邊的組態將合併到左邊的組態中。組態可以透過在右邊的組態中傳遞相同的屬性來覆寫。
js.webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.js$/,
use: 'babel-loader',
exclude: /node_modules/,
},
],
},
};
css.webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
],
},
};
webpack.config.js
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',
},
};
你可以透過傳遞與延伸設定檔中相同的屬性來覆寫延伸設定檔中的設定檔。
base.webpack.config.js
module.exports = {
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
};
webpack.config.js
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',
},
};
你也可以透過傳遞套件名稱給 extends
屬性,從第三方套件載入設定檔。套件必須在 package.json 中匯出 webpack 設定檔。
webpack.config.js
module.exports = {
extends: require.resolve('webpack-config-foo'),
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
};