-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.prod.js
137 lines (130 loc) · 5.11 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
const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const commonConfig = require('./webpack.common');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = merge(commonConfig, {
module: {
rules: [
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
minimize: true,
sourceMap: true
}
}
]
}),
},
{
test: /\.(scss|sass)$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: "css-loader",
options: {
// If you are having trouble with urls not resolving add this setting.
// See https://github.com/webpack-contrib/css-loader#url
url: false,
minimize: true,
sourceMap: true
}
},
{
loader: "sass-loader",
options: {
sourceMap: true
}
}
],
})
},
{
test: /\.(png|svg|jpg|gif)$/,
use: [
/**
* Loads image files as `base64` encoded URL.
*
* https://github.com/webpack-contrib/url-loader
*/
{
loader: 'url-loader',
options: {
fallback: 'file-loader', //when file is greater than the limit
limit: 1024 * 10, // Byte limit to inline files as Data URL
}
},
/**
* Minify PNG, JPEG, GIF, SVG and WEBP images with imagemin
* NOTE: compression is a little time-consuming.
*
* https://github.com/tcoopman/image-webpack-loader
*/
{
loader: 'image-webpack-loader',
options: {
mozjpeg: {
progressive: true,
quality: 65 // [0, 100]
},
pngquant: {
floyd: 0.5, // level of dithering, [0, 1]
quality: '65-90', // similar to JPEG, [0, 100]
speed: 4 // speed/quality trade-off, [1, 10]
},
optipng: {
enabled: false,
},
gifsicle: {
interlaced: true, // for progressive rendering
optimizationLevel: 2, // [1,2,3], bigger takes longer but better results
},
}
},
]
},
]
},
devtool: 'source-map',
plugins: [
/* 为所有依赖指定环境为 production */
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
}),
/* webpack 自带的 UglifyJs 插件 */
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false,
drop_console: true,
}
}),
/**
* moves all the required *.css modules in entry chunks into a separate CSS file.
* So your styles are no longer inlined into the JS bundle, but in a separate CSS file
*
* ExtractTextPlugin generates a file per entry,
* so you must use [name], [id] or [contenthash] when using multiple entries.
*
* https://github.com/webpack-contrib/extract-text-webpack-plugin
*/
new ExtractTextPlugin({
filename: '[name].[contenthash].css'
}),
/**
* https://webpack.js.org/plugins/hashed-module-ids-plugin/
* 根据文件的相对路径生成一个 hash 来作为 module.id,建议用于生产环境
*/
new webpack.HashedModuleIdsPlugin(),
/**
* https://github.com/chrisbateman/webpack-visualizer
* 打包分析,会在output目录输出一个HTML文件。不需要时就注释掉。
*/
// new require('webpack-visualizer-plugin')({filename: './statistics.html'}),
]
});