-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
181 lines (170 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
import webpack from "webpack";
import path from "path";
import HtmlWebpackPlugin from "html-webpack-plugin";
import UglifyJSPlugin from "uglifyjs-webpack-plugin";
import MiniCssExtractPlugin from "mini-css-extract-plugin";
const DEVELOPMENT = process.env.NODE_ENV === "development";
const PRODUCTION = process.env.NODE_ENV === "production";
const mode = DEVELOPMENT ? "development" : "production";
let entry = PRODUCTION
? {
index: path.resolve(__dirname, "src/index")
}
: [
"eventsource-polyfill", // necessary for hot reloading with IE
"webpack-hot-middleware/client?reload=true", //note that it reloads the page if hot module reloading fails.
path.resolve(__dirname, "src/index")
];
let devtool = PRODUCTION ? "source-map" : "cheap-module-eval-source-map";
let output = DEVELOPMENT
? {
path: __dirname + "/dist", // Note: Physical files are only output by the production build task `npm run build`.
publicPath: "/",
filename: "scripts.js"
}
: {
path: path.resolve(__dirname, "dist"),
publicPath: "/",
filename: "[name].[chunkhash].js"
};
let plugins = DEVELOPMENT
? [
new webpack.LoaderOptionsPlugin({
minimize: false,
debug: true,
noInfo: true // set to false to see a list of every file being bundled.
}),
new webpack.HotModuleReplacementPlugin() //for hot reloading
]
: [
new webpack.HashedModuleIdsPlugin(),
// for compatibility with old loaders, loaders can be switched to minimize mode via plugin
new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false,
noInfo: false
}),
// Extract text from bundle into a file
new MiniCssExtractPlugin({
filename: "[name].[chunkhash].css",
chunkFilename: "[id].css"
}),
// Simplifies creation of HTML files to serve webpack bundles
new HtmlWebpackPlugin({
template: "build/index-template.html",
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
},
inject: true
})
];
plugins.push(
new webpack.DefinePlugin({
DEVELOPMENT: JSON.stringify(DEVELOPMENT),
PRODUCTION: JSON.stringify(PRODUCTION),
"process.env": {
NODE_ENV: JSON.stringify(PRODUCTION ? "production" : "development")
}
})
);
let cssLoaders = PRODUCTION
? [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"]
: ["style-loader", "css-loader", "sass-loader"];
export default {
resolve: {
extensions: ["*", ".js", ".jsx", ".json"],
modules: [path.resolve(__dirname, "./src"), "node_modules"]
},
mode: mode,
context: __dirname,
devtool: devtool,
entry: entry,
target: "web",
output: output,
plugins: plugins,
optimization: {
splitChunks: {
// replaces CommonsChunkPlugin()
name: "vendor",
minChunks: Infinity
},
noEmitOnErrors: true, // NoEmitOnErrorsPlugin
// concatenateModules: true, //ModuleConcatenationPlugin
minimizer: [
// Minify JS and allow tree shaking
new UglifyJSPlugin({
uglifyOptions: {
output: {
comments: false
},
compress: true
}
})
]
},
module: {
rules: [
{
test: /\.s?css$/,
use: cssLoaders,
exclude: "/node_modules/"
},
{
test: /\.jsx?$/, // loads both js and jsx files
exclude: /node_modules/,
loader: "babel-loader"
},
{
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
use: "file-loader"
},
{
test: /\.(woff|woff2)$/,
use: "url-loader?prefix=font/&limit=5000&name=[name]-[hash].[ext]"
},
{
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
use: "url-loader?limit=10000&mimetype=application/octet-stream"
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
use: [
"file-loader?hash=sha512&digest=hex&name=[name]-[hash].[ext]",
{
loader: "image-webpack-loader",
options: {
mozjpeg: {
progressive: true,
quality: 65
},
// optipng.enabled: false will disable optipng
optipng: {
enabled: false
},
pngquant: {
quality: [0.65, 0.9],
speed: 4
},
gifsicle: {
interlaced: false
},
// the webp option will enable WEBP
webp: {
quality: 75
}
}
}
]
}
]
}
};