在React项目中,通常使用Webpack作为打包工具。webpack.config.js
和 webpack.base.config.js
两个文件在项目中的作用是不同的。
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
// 规定模块加载器
]
},
plugins: [
new HtmlWebpackPlugin({
template: './public/index.html',
filename: 'index.html'
})
// 其他插件配置
],
// 其他通用配置
};
const path = require('path');
module.exports = {
module: {
rules: [
// 共享的模块加载器规则
]
},
resolve: {
// 共享的解析规则
},
// 其他通用配置
};
在实际项目中,通过将通用的配置提取到 webpack.base.config.js 中,可以使项目的Webpack配置更加清晰、可维护,同时在不同环境的配置文件中只需要关注特定环境的差异性。