-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.prod.js
200 lines (183 loc) · 5.33 KB
/
webpack.config.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
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
198
199
200
/* eslint-disable indent */
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const ParallelUglifyPlugin = require('webpack-parallel-uglify-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin');
const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin');
const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin');
const PreloadWebpackPlugin = require('preload-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const sourceDir = path.join(__dirname, 'app');
const buildDir = path.join(__dirname, 'build');
const entryPath = path.join(sourceDir, 'index.js');
const { NODE_ENV = 'production' } = process.env;
module.exports = require('./webpack.config.base')({
mode: 'production',
devtool: 'source-map',
entry: {
main: entryPath,
},
output: {
filename: '[name]-[hash:4].js',
chunkFilename: '[name]-[chunkhash:4].js',
publicPath: '/',
},
optimization: {
namedModules: true,
noEmitOnErrors: true,
minimizer: [
// in some cases we may want to try to run prod build without the uglifyer
// for debugging purposes only
NODE_ENV === 'production'
? new ParallelUglifyPlugin({
sourceMap: process.env.SOURCEMAPS === 'true',
workerCount: 4,
cacheDir: path.join(__dirname, '../.uglify-cache'),
uglifyES: {
compress: {
warnings: false,
conditionals: true,
unused: true,
comparisons: true,
sequences: true,
dead_code: true,
evaluate: true,
if_return: true,
join_vars: true,
},
output: {
comments: false,
},
},
})
: null,
new OptimizeCSSAssetsPlugin({}),
// copy statics
new CopyWebpackPlugin([{ from: '**/*', context: path.join(sourceDir, 'static') }]),
// remove null plugins
].filter(Boolean),
},
plugins: [
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: 'static/[name].[hash:4].css',
chunkFilename: 'static/[id].[hash:4].css',
}),
new CopyWebpackPlugin([
{
from: path.join(__dirname, 'public'),
to: path.join(buildDir, 'static'),
ignore: '_redirects',
},
{
from: path.join(__dirname, 'public', '_redirects'),
to: buildDir,
},
]),
new SWPrecacheWebpackPlugin({
cacheId: 'budgeting-app',
filename: 'sw.js',
maximumFileSizeToCacheInBytes: 800000,
staticFileGlobsIgnorePatterns: [/_redirects$/],
mergeStaticsConfig: true,
minify: true,
runtimeCaching: [
{
handler: 'cacheFirst',
urlPattern: /(.*?)/,
},
],
}),
new HtmlWebpackPlugin({
template: 'index.prod.ejs',
inject: true,
production: true,
inlineSource: '.*main.*(css)$',
root: '/',
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
},
}),
// make sure script tags are async to avoid blocking html render
new ScriptExtHtmlWebpackPlugin({
defaultAttribute: 'async',
}),
// inline entrypoint css
new HtmlWebpackInlineSourcePlugin(),
// preload JS and CSS
// ScriptExtHtmlWebpackPlugin doesn't preload CSS well ATM
new PreloadWebpackPlugin({
include: 'initial',
fileWhitelist: [/(css|js)/],
}),
],
module: {
rules: [
{
test: /\.(ico|jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2)(\?.*)?$/,
include: sourceDir,
use: {
loader: 'file-loader',
query: {
name: 'static/[name]-[hash:5].[ext]',
},
},
},
{
test: /\.scss$/,
include: [sourceDir],
use: [
MiniCssExtractPlugin.loader,
'cache-loader',
{
loader: 'css-loader',
options: {
module: true,
localIdentName: '[hash:base64:5]',
importLoaders: 2,
context: sourceDir,
},
},
{
loader: 'postcss-loader',
options: {
sourceMap: false,
},
},
{
loader: 'sass-loader',
options: {
outputStyle: 'collapsed',
sourceMap: false,
includePaths: [sourceDir, path.resolve(sourceDir, '../')],
},
},
],
},
{
test: /\.(js)$/,
include: [sourceDir],
use: [
{
loader: 'babel-loader',
options: {
cacheDirectory: path.join(__dirname, '.babel-cache', 'production'),
},
},
],
},
],
},
});