-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.dev.js
197 lines (190 loc) · 5.59 KB
/
webpack.dev.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin"); // 开启css提取
const OptimizeCssAssetsWebpackPlugin = require("optimize-css-assets-webpack-plugin"); // 提取的css代码压缩
const FriendlyErrorsWebpackPlugin = require("friendly-errors-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin"); // 清除dist目录 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 FileList = require("./plugins/fileList");
const path = require("path");
const speed = new SpeedMeasurePlugin();
module.exports = speed.wrap({
mode: "development",
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-[hash].js`,
},
devServer: {
stats: "errors-only", // 打包时候的日志优化,配合下面的插件使用
port: 9001,
hotOnly: true, // 因为是vue项目,有了vue-loader之后,只需要设置hotOnly就能热
proxy: {
"/api2": {
target: "https://data.jianshukeji.com",
pathRewrite: {
"^/api2": "",
},
changeOrigin: true,
},
"/api": {
target: "http://localhost:8000",
},
"/expressApi": {
target: "http://localhost:8001",
changeOrigin: true,
},
"/koa": {
target: "http://localhost:8002",
changeOrigin: true,
},
},
},
devtool: "source-map",
// context: process.cwd(),
resolve: {
alias: {
vue: "vue/dist/vue",
"@": path.resolve("src"),
Components: path.resolve("src/components"),
},
modules: [path.resolve(__dirname, "node_modules")],
extensions: [".vue", ".js"],
},
module: {
rules: [
{
test: /\.css$/,
use: ["vue-style-loader", "css-loader"],
},
{
test: /\.less$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "remove-ui-hash-loader",
options: {
moduleName: "el-",
diretiveRule: /__.{8}_json/,
},
},
{
loader: "css-loader",
options: {
modules: {
localIdentName: "[local]__[hash:base64:8]_json",
},
},
},
"less-loader",
{
loader: "postcss-loader",
options: {
plugins: () => [require("autoprefixer")()], // 必须指定bowserslist才会生效,我是在package.json里面指定的
},
},
],
},
{
test: /\.js$/,
use: [
// {
// loader: "thread-loader",
// options: {
// workers: 3
// }
// },
"babel-loader",
],
include: path.resolve(__dirname, "./src"),
//exclude: /node_modules/,
},
{
test: /\.vue$/,
use: "vue-loader",
//include: path.resolve(__dirname, "./src"),
exclude: /node_modules/,
},
{
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 MiniCssExtractPlugin({
filename: "vue.css",
}),
new OptimizeCssAssetsWebpackPlugin({
assetNameRegExp: /\.css$/g,
// cssProcessor: require("cssnano"),
}),
new HtmlWebpackPlugin({
template: "index.html",
minify: {
removeComments: false,
minifyCSS: false,
minifyJS: false,
collapseWhitespace: false,
},
}),
new CleanWebpackPlugin(),
new VueLoaderPlugin(),
// new webpack.DllReferencePlugin({
// context: path.join(__dirname, '/json-dll'),
// manifest: require("./json-dll/library.json")
// }),
// scope hoisting减少webpack打包后的包裹
new webpack.optimize.ModuleConcatenationPlugin(),
new FriendlyErrorsWebpackPlugin(), // webpack打包日志优化
// new BundleAnalyzerPlugin() // 打包体积插件
// new webpack.HotModuleReplacementPlugin() // 热跟新
// new webpack.ProvidePlugin({
// "window.proj4": path.resolve('./src/util/proj4.js')
// })
new FileList({
filename: "fileList.md",
}),
],
// 提取公共函数和分离模块,为了让第三方的模块走缓存
// 这里有一个问题如果走cdn打包会更快一点
// optimization: {
// splitChunks: {
// minSize: 0,
// cacheGroups: {
// vander: {
// test: /[\\/]node_modules[\\/]/,
// name: "vue-vander",
// chunks: "all",
// priority: -10
// },
// common: {
// name: "common",
// chunks: "all",
// minChunks: 2,
// priority: -20
// }
// }
// }
// }
});