-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
190 lines (187 loc) · 4.83 KB
/
webpack.config.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
// webpack.config.js
const webpack = require('webpack');
const path = require('path');
const isDev = process.env.NODE_ENV === 'development';
const {
CleanWebpackPlugin
} = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin');
const TerserWebpackPlugin = require('terser-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const ImageminPlugin = require('imagemin-webpack-plugin').default // 使用cnpm 安装,npm有些依赖下不来会报错
const {
VueLoaderPlugin
} = require('vue-loader/dist/index');
const resolveSrc = (src) => path.resolve(__dirname, src)
module.exports = {
mode: 'development',
devtool: 'eval-cheap-module-source-map',
entry: './src/main.js',
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/'
},
resolve: {
extensions: ['.js', '.vue', '.json'],
alias: {
// 'vue$': 'vue/dist/vue.esm.js',
'@': resolveSrc('src'),
'pages': resolveSrc('src/pages'),
'utils': resolveSrc('src/utils'),
'assets': resolveSrc('src/assets'),
'components': resolveSrc('src/components'),
}
},
module: {
rules: [{
test: /\.js$/,
include: resolveSrc('src'),
use: [
'cache-loader',
'thread-loader',
{
loader: 'babel-loader',
options: {
// presets: ['@babel/preset-env']
}
}
],
// include: [path.join(__dirname, './src'), path.join(__dirname, './node_modules/webpack-dev-server/client')],
// exclude: '/node_modules/'
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
// {
// loader: MiniCssExtractPlugin.loader,
// options: {
// publicPath: './'
// }
// },
// 'style-loader',
'css-loader'
]
},
{
test: /\.less$/,
use: [
MiniCssExtractPlugin.loader,
// 'style-loader',
'css-loader',
'less-loader'
]
},
{
test: /\.scss$/,
use: [
'style-loader',
'css-loader',
"sass-loader", // Compiles Sass to CSS
]
},
{
test: /\.(jpg|png|jpeg|gif|bmp)$/,
use: [{
loader: 'url-loader',
options: {
limit: 1024,
fallback: {
loader: 'file-loader',
options: {
name: '[name].[ext]'
}
}
}
},
/* {
loader: 'image-webpack-loader',
options: {
mozjpeg: {
progressive: true,
},
optipng: {
enabled: false,
},
pngquant: {
quality: [0.65, 0.90],
speed: 4
},
gifsicle: {
interlaced: false,
},
webp: {
quality: 75
}
}
} */
]
},
{
test: /\.(mp4|ogg|mp3|wav)$/,
use: {
loader: 'url-loader',
options: {
limit: 1024,
fallback: {
loader: 'file-loader',
options: {
name: '[name].[ext]'
}
}
}
}
},
{
test: /\.vue$/,
use: [
'vue-loader'
]
}
],
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html',
title: 'Vue3 -> Web App',
minify: {
collapseWhitespace: true, // 去掉空格
removeComments: true // 去掉注释
}
}),
new CleanWebpackPlugin(),
new OptimizeCssAssetsWebpackPlugin(),
new MiniCssExtractPlugin({
filename: 'css/[name].css'
}),
new ImageminPlugin({
test: /\.(jpe?g|png|gif|svg)$/i
}),
new VueLoaderPlugin(),
// 使用vue3 时推荐开启 具体可查看 https://link.vuejs.org/feature-flags.
new webpack.DefinePlugin({
'__VUE_OPTIONS_API__': true, //(启用/禁用选项 API 支持,默认值true:)
'__VUE_PROD_DEVTOOLS__': false //(在生产中启用/禁用 devtools 支持,默认值false:)
}),
/* new BundleAnalyzerPlugin({
analyzerPort: 9091,
generateStatsFile: false
}), */
],
devServer: {
port: 8080,
hot: true,
open: false, // 是否自动打开
// contentBase: './dist'
},
optimization: {
minimize: true,
minimizer: [
new TerserWebpackPlugin()
]
},
}