-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.prod.js
169 lines (163 loc) · 4.69 KB
/
webpack.prod.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const optimizeCssAssetsWebpakPlugin = require("optimize-css-assets-webpack-plugin");
const FriendlyErrorsWebpackPlugin = require("friendly-errors-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin"); // clean-webpack-plugin最新更改必须是解构f
const VueLoaderPlugin = require("vue-loader/lib/plugin");
const SpeedMeasurePlugin = require("speed-measure-webpack-plugin");
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
const TerserPlugin = require("terser-webpack-plugin");
const FileList = require("./plugins/fileList");
const path = require("path");
// 在耗时的loader上使用,感觉自己的项目,效果不佳
const threadLoaderOption = {
workers: 3,
poolTimeout: 2000,
};
const speed = new SpeedMeasurePlugin();
module.exports = speed.wrap({
mode: "production",
entry: "./src/index.js",
output: {
path: path.resolve(__dirname, "dist"),
// 解决报错
// Cannot use [chunkhash] or [contenthash] for chunk in '[name].[chunkhash].js' (use [hash] instead)
filename: "bundle-[contenthash].js",
},
devtool: "source-map",
resolve: {
alias: {
vue: "vue/dist/vue",
"@": path.resolve("src"),
Components: path.resolve("src/components"),
},
extensions: [".js", ".vue"],
},
module: {
rules: [
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
},
{
test: /\.less$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
"less-loader",
{
loader: "postcss-loader",
options: {
plugins: () => [require("autoprefixer")()], // 必须指定bowserslist才会生效,我是在package.json里面指定的
},
},
],
},
{
test: /\.js$/,
use: [
// 有问题感觉反而更慢了
// {
// loader: "thread-loader",
// options: threadLoaderOption
// },
"babel-loader",
],
exclude: /node_modules/,
},
{
test: /\.vue$/,
use: [
{
loader: "thread-loader",
options: threadLoaderOption,
},
"vue-loader",
],
},
{
test: /\.(woff|ttf|eot)$/,
use: "file-loader",
},
{
test: /\.(jpg|jpeg|gif|png)/,
use: {
loader: "url-loader",
options: {
limit: 8000,
},
},
},
// 将juqery, proj4定义在window上,这样第三方插件就可以直接用
// proj4.js是从网上下载的UMD文件,es6模块化还不知道怎么做
// 这个意思是有地方引入这个文件,就把这个文件的对象挂载到window.proj4上
{
test: require.resolve("./src/util/proj4.js"),
use: "expose-loader?proj4",
},
],
},
plugins: [
new FriendlyErrorsWebpackPlugin(), // webpakck打包日志优化
new MiniCssExtractPlugin({
filename: "vue.css",
}),
new optimizeCssAssetsWebpakPlugin({
assetNameRegExp: /\.css$/g,
cssProcessor: require("cssnano"),
}),
new HtmlWebpackPlugin({
template: "index.html",
minify: {
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true,
removeComments: true,
},
}),
new CleanWebpackPlugin(),
new VueLoaderPlugin(),
// dll 分包插件
new webpack.DllReferencePlugin({
manifest: require("./json-dll/library.json"),
}),
// new BundleAnalyzerPlugin()
// new webpack.HotModuleReplacementPlugin() // 热跟新
// new webpack.ProvidePlugin({
// "window.proj4": path.resolve('./src/util/proj4.js')
// })
new FileList({
filename: "fileList.md",
}),
],
// 提取公共函数和分离模块,为了让第三方的模块走缓存
// 这里有一个问题如果走cdn打包会更快一点
optimization: {
// 本地电脑开启多线程压缩,反而速度减慢
// minimizer: [
// new TerserPlugin({
// parallel: true,
// cache: true
// })
// ],
splitChunks: {
minSize: 0,
cacheGroups: {
// vander: {
// test: /[\\/]node_modules[\\/]/,
// name: "vue-vander",
// chunks: "all",
// priority: -10
// },
common: {
name: "common",
chunks: "all",
minChunks: 2,
priority: -20,
},
},
},
},
stats: "errors-only", // 打包时候的日志优化,配合下面的插件使用
});