Fork me on GitHub

webpack生产环境

webpack 生产环境简单配置


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const path = require("path")
const HtmlWebpackPlugin = require("html-webpack-plugin")
const CleanWebpackPlugin = require("clean-webpack-plugin")
const VueLoaderPlugin = require("vue-loader/lib/plugin")
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
module.exports = {
// 模式 生产模式
mode: "production",
devtool: "none",
entry: "./src/main.js",
output: {
filename: "js/[hash:5].bundle.js",
path: path.resolve(__dirname, "./dist")
},
module: {
rules: [
{
// css 文件的规则
test: /(\.css|\.scss)$/,
// use: ["style-loader", "css-loader", "sass-loader"]
use: [
"style-loader",
{
loader: MiniCssExtractPlugin.loader,
options: {
// you can specify a publicPath here
// by default it use publicPath in webpackOptions.output
}
},
"css-loader",
{
loader: "postcss-loader",
options: {
// autoprefixer 自动添加浏览器厂商前缀
// 从webpack高版本之后 autoprefixer 就必须带配置才能生效
// cssnano 压缩css
plugins: [
require("autoprefixer")({
browsers: [
"> 1%",
"last 2 versions",
"not ie <= 8",
"ios >= 8",
"android >= 4.0"
]
}),
require("cssnano")
]
}
},
"sass-loader"
]
},
{
// vue 文件的规则
test: /\.vue$/,
use: ["vue-loader"]
},
// 图片当作模块引入
{
test: /\.(png|jpg|gif)$/i,
use: [
{
loader: "url-loader",
options: {
// 图片小于 8kb 自动转化为 base64 编码
//
limit: 8192,
name: "images/[hash:8].[name].[ext]",
publicPath: "/"
}
}
]
}
]
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: "./public/index.html"
}),
new VueLoaderPlugin(),
new MiniCssExtractPlugin({
filename: "css/[hash:5].[name].css",
chunkFilename: "[id].css",
publicPath: "/css"
})
],
resolve: {
// 模块解析的时候可以忽略的扩展名
extensions: [".js", ".json", ".vue"]
}
}

请我喝杯可乐吧