forked from mhaagens/react-mobx-react-router4-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 32
/
webpack.config.production.js
189 lines (168 loc) · 4.74 KB
/
webpack.config.production.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
const path = require('path');
const webpack = require('webpack');
const cssnano = require('cssnano');
const merge = require('webpack-merge');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const BundlePlugin = require('webpack-bundle-analyzer');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin');
const PrerenderSPAPlugin = require('prerender-spa-plugin');
const webpackConfig = require('./webpack.config');
const Renderer = PrerenderSPAPlugin.PuppeteerRenderer;
const publicPath = '/';
const mergeConfig = merge(webpackConfig, {
mode: 'production',
output: {
path: path.join(__dirname, 'output'),
publicPath: publicPath,
filename: 'js/[name].[hash:12].js',
chunkFilename: 'js/[id].[chunkhash:12].js'
},
devtool: '#source-map',
module: {},
optimization: {
runtimeChunk: {
name: 'manifest'
},
minimizer: [
new UglifyJsPlugin({
cache: true,
parallel: true,
uglifyOptions: {
warnings: false,
mangle: {
toplevel: true
}
}
}),
new OptimizeCSSAssetsPlugin({
assetNameRegExp: /\.css$/g,
cssProcessor: cssnano,
cssProcessorOptions: {
autoprefixer: false,
preset: [
'default',
{
discardComments: {
removeAll: true
}
}
]
}
})
],
splitChunks: {
chunks: 'async',
minSize: 30000,
minChunks: 1,
maxAsyncRequests: 5,
maxInitialRequests: 3,
name: false,
cacheGroups: {
vendor: {
name: 'vendor',
chunks: 'initial',
priority: -10,
reuseExistingChunk: false,
test: /node_modules\/(.*)\.js/
},
styles: {
name: 'styles',
test: /\.(less|css)$/,
chunks: 'all',
minChunks: 1,
reuseExistingChunk: true,
enforce: true
}
}
}
},
performance: {
hints: false
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production'),
__DEV__: process.env.NODE_ENV === 'production'
}
}),
new webpack.NamedModulesPlugin(),
new MiniCssExtractPlugin({
filename: 'css/app.[name].css',
chunkFilename: 'css/app.[contenthash:12].css'
}),
new HtmlWebpackPlugin({
hash: false,
template: './src/index.html',
filename: 'index.html'
}),
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./public/lib/min/manifest.json')
}),
new AddAssetHtmlPlugin([
{
filepath: path.resolve(__dirname, './public/lib/min/lib.aef84325a.js'),
outputPath: 'lib/min',
publicPath: `${publicPath}lib/min`,
includeSourcemap: false
}
]),
new webpack.ContextReplacementPlugin(/moment[/\\]locale$/, /nb/),
new BundlePlugin.BundleAnalyzerPlugin(),
new PrerenderSPAPlugin({
// Index.html is in the root directory.
staticDir: path.join(__dirname, 'output'),
outputDir: path.join(__dirname, 'output'),
indexPath: path.join(__dirname, 'output', 'index.html'),
routes: ['/'],
// Optional minification.
minify: {
collapseBooleanAttributes: true,
collapseWhitespace: true,
decodeEntities: true,
keepClosingSlash: true,
sortAttributes: true
},
server: {
// Normally a free port is autodetected, but feel free to set this if needed.
port: 8001,
proxy: {
'/dist/': {
target: 'http://127.0.0.1:8001',
pathRewrite: { '^/dist': '/' }
}
}
},
postProcess(context) {
// Remove /index.html from the output path if the dir name ends with a .html file extension.
// For example: /dist/dir/special.html/index.html -> /dist/dir/special.html
// context.html = context.html.replace(/\/dist/gi, '');
return context;
},
renderer: new Renderer({
renderAfterTime: 2000
})
})
]
});
mergeConfig.module.rules
.filter((rule) => {
const bool =
rule.use &&
Array.isArray(rule.use) &&
rule.use.find((name) => {
if (Object.prototype.toString.call(name) === '[object Object]') {
name = name.loader;
}
return /css-loader/.test(name.split('?')[0]);
});
return bool;
})
.forEach((rule) => {
rule.use[0] = MiniCssExtractPlugin.loader;
});
module.exports = mergeConfig;