1,webpack 命令
1 | # webpack 打包文件 |
2,配置文件
单入口配置
1
2
3
4
5
6
7
8
9const path = require('path')
module.exports = {
entry: "./src/index.js",
output:{
path: path.resolve(__dirname, 'dist'),
filename:'main.js',
}
}多入口配置
1
2
3
4
5
6
7
8
9
10
11
12const path = require('path')
module.exports = {
entry: {
app1:"./src/index.js",
app2:"./src/index2.js",
},
output:{
path: path.resolve(__dirname, 'dist'),
filename:'[name].main.js',
}
}
3,配置插件,以uglifyjs
安装插件
1
npm install uglifyjs-webpack-plugin --save-dev
配置, 参考文档https://webpack.js.org/plugins/uglifyjs-webpack-plugin/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16const path = require('path')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
module.exports = {
entry: {
app1:"./src/index.js",
app2:"./src/index2.js",
},
output:{
path: path.resolve(__dirname, 'dist'),
filename:'[name].main.js',
},
optimization: {
minimizer: [new UglifyJsPlugin()],
},
}
4,配置loader加载器
安装依赖
1
npm install --save-dev css-loader
配置文件, 参考文档https://webpack.js.org/loaders/css-loader/#getting-started
1
2
3
4
5
6
7
8
9
10module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: ['css-loader'],
},
],
},
};