📅  最后修改于: 2023-12-03 14:48:25.917000             🧑  作者: Mango
Webpack-merge是一个帮助程序员合并webpack配置的npm包,可以简化webpack配置管理,提高工作效率。
安装webpack-merge
npm install webpack-merge --save-dev
在webpack配置文件中引入webpack-merge
const { merge } = require('webpack-merge');
使用merge方法合并不同的webpack配置
const commonConfig = require('./webpack.common.js');
const devConfig = require('./webpack.dev.js');
const prodConfig = require('./webpack.prod.js');
module.exports = (env) => {
const isDev = env.mode === 'development';
const baseConfig = isDev ? devConfig : prodConfig;
return merge(commonConfig, baseConfig);
};
const result = merge.smart({}, {});
const result = merge({
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: { importLoaders: 1 }
},
'postcss-loader'
]
}
]
},
}, {
module: {
rules: [
{
test: /\.css/,
use: [
'style-loader',
'css-loader'
]
}
]
}
}, {
module: {
rules: {
test: 'match',
use: 'prepend'
}
}
})
// result is:
// {
// module: {
// rules: [
// {
// test: /\.css$/,
// use: [
// 'style-loader',
// {
// loader: 'css-loader',
// options: { importLoaders: 1 }
// },
// 'postcss-loader'
// ]
// },
// {
// test: /\.css/,
// use: [
// 'style-loader',
// {
// loader: 'css-loader',
// options: { importLoaders: 1 }
// }
// ]
// }
// ],
// }
// }
const result = merge({
entry: ['index.js'],
resolve: {
alias: {
Foo: 'component/Foo',
},
},
}, {
entry: ['app.js'],
resolve: {
alias: {
Bar: 'component/Bar',
},
},
}, {
customizeArray(a, b, key) {
if (key === 'entry') {
// return entries merged together
return Array.from(new Set([...a, ...b]));
}
// fall back to default merging
return undefined;
},
});