-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.base.config.js
140 lines (139 loc) · 3.92 KB
/
webpack.base.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
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CleanWebpackplugin = require('clean-webpack-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
module.exports = {
entry: './src/index.js',
resolve: {
// 模块扩展名, 引入文件今可能写文件名
extensions: ['.js', '.vue', '.json'],
// 配置别名来映射路径
alias: {
'@': path.resolve(__dirname, 'src/'),
// 对于固定的库, 减少递归解析
'vue': path.resolve(__dirname, 'node_modules/vue/dist/vue.min.js')
},
// 指明第三方模块的绝对路径, 减少路径查找
modules: [path.resolve(__dirname, 'node_modules')]
},
// 配置外部依赖不会打包到boudle
externals: {
jquery: 'jQuery'
},
module: {
// 忽略对没采用模块化的模块进行递归解析
noParse: [/vue\.min\.js/],
rules: [
// 编译js
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
include: path.resolve(__dirname, 'src'), // 缩小命中范围, 减少构建时间
use: [
{
loader: 'babel-loader?cacheDirectory' // 通过cacheDirectory选项开启支持缓存
},
{
loader: 'eslint-loader',
options: {
fix: true
}
}
]
},
// 处理字体
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url-loader',
options: {
// 文件大小小于limit参数,url-loader将会把文件转为DataUR
limit: 10000,
name: '[name]-[hash:5].[ext]',
output: 'fonts/'
// publicPath: '', 多用于CDN
}
},
// 处理文件
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
use: [
// 转base64
{
loader: 'url-loader',
options: {
// 具体配置见插件官网
limit: 10000,
name: '[name]-[hash:5].[ext]',
outputPath: 'img/'// outputPath所设置的路径,是相对于 webpack 的输出目录。
// publicPath 选项则被许多webpack的插件用于在生产模式下更新内嵌到css、html文件内的 url , 如CDN地址
}
},
{
loader: 'image-webpack-loader',
options: {
mozjpeg: {
progressive: true,
quality: 65
},
// optipng.enabled: false will disable optipng
optipng: {
enabled: false
},
pngquant: {
quality: '65-90',
speed: 4
},
gifsicle: {
interlaced: false
},
// the webp option will enable WEBP
webp: {
quality: 75
}
}
}
]
},
{
enforce: 'pre',
test: /\.(js|vue)$/,
loader: 'eslint-loader',
exclude: /node_modules/
},
{
test: /\.vue$/,
use: [
{
loader: 'vue-loader',
options: {
loaders: {
sass: ['style-loader', 'css-loader', 'sass-loader', 'postcss-loader']
}
}
}
]
}
]
},
plugins: [
new CleanWebpackplugin(['dist']),
// 打包模板
new HtmlWebpackPlugin({
inject: true,
hash: true,
cache: true,
chunksSortMode: 'none',
title: 'Webapck4-demo', // 可以由外面传入
filename: 'index.html', // 默认index.html
template: path.resolve(__dirname, 'index.html'),
minify: {
collapseWhitespace: true,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true
}
}),
new VueLoaderPlugin()
]
}