webpack合并js文件
时间: 2023-10-12 16:12:57 浏览: 176
在使用webpack进行项目打包时,可以使用webpack-merge插件来合并多个js文件,具体步骤如下:
1. 安装webpack-merge插件
```
npm install webpack-merge --save-dev
```
2. 创建不同的webpack配置文件,例如:
webpack.common.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')
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html'
})
]
};
```
webpack.prod.js:
```
const merge = require('webpack-merge');
const common = require('./webpack.common.js');
module.exports = merge(common, {
mode: 'production'
});
```
webpack.dev.js:
```
const merge = require('webpack-merge');
const common = require('./webpack.common.js');
module.exports = merge(common, {
mode: 'development',
devtool: 'inline-source-map',
devServer: {
contentBase: './dist'
}
});
```
3. 在package.json文件中添加打包命令
```
"scripts": {
"dev": "webpack-dev-server --open --config webpack.dev.js",
"build": "webpack --config webpack.prod.js"
}
```
4. 运行打包命令
```
npm run build
```
以上操作将会合并webpack.common.js和webpack.prod.js中的配置,并将最终打包结果输出到dist文件夹中的bundle.js文件中。
阅读全文