-
Notifications
You must be signed in to change notification settings - Fork 3
/
webpack.config.production.js
122 lines (107 loc) · 4.05 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
const path = require('path');
const fs = require('fs');
const webpack = require('webpack');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const StyleExtHtmlWebpackPlugin = require('style-ext-html-webpack-plugin');
const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin');
const WorkboxPlugin = require('workbox-webpack-plugin');
const nodeExternals = require('webpack-node-externals');
const { rules, extensions, browserEntryProd, serverEntry, output, DIST_DIR } = require('./webpack.shared');
// const PUBLIC_URL = 'http://localhost:3000';
const PUBLIC_URL = 'https://reactspike.azurewebsites.net';
const definedVariables = new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production'),
'process.env.PUBLIC_URL': JSON.stringify(PUBLIC_URL),
});
const browserConfig = {
entry: browserEntryProd,
output,
resolve: { extensions },
module: { rules },
plugins: [
definedVariables,
// These plugins will create the HTML / CSS / FavIcon / ServiceWorker etc static assets
new CopyWebpackPlugin([{
from: 'src/images',
to: 'images'
}, {
from: 'src/manifest.json',
to: 'manifest.json'
}]),
new HtmlWebpackPlugin({
filename: 'template.html',
favicon: 'src/favicon.ico',
inject: true,
template: 'src/template.html',
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
},
}),
new ExtractTextPlugin({ filename: 'style.css', allChunks: true }),
new StyleExtHtmlWebpackPlugin({
minify: true
}),
new ScriptExtHtmlWebpackPlugin({
defaultAttribute: 'async'
}),
new UglifyJSPlugin(),
new WorkboxPlugin({
globDirectory: DIST_DIR,
globPatterns: ['**/*.{js,css,png,svg,jpg,gif,json}'],
globIgnores: ['server.js'],
swDest: path.resolve(DIST_DIR, 'sw.js'),
clientsClaim: true,
skipWaiting: true,
// navigateFallback: 'template.html',
runtimeCaching: [{
// match empty strings and words under the website eg https://reactspike.azurewebsites.net/world
// - intended to cache server side rendered code
urlPattern: new RegExp(PUBLIC_URL + '\/(\\w*)\$'),
handler: 'networkFirst',
options: { cacheName: 'html-cache' }
}, {
// match json paths under the website eg https://reactspike.azurewebsites.net/home.json
urlPattern: new RegExp(PUBLIC_URL + '\/(.*\.json)\$'),
handler: 'networkFirst',
options: { cacheName: 'json-cache' }
}]
}),
// Since the type checker covers both client and server code we only need a single instance
// of the plugin; not one for both client and server
new ForkTsCheckerWebpackPlugin({
async: false,
memoryLimit: 4096,
checkSyntacticErrors: true
}),
new webpack.NoEmitOnErrorsPlugin(),
],
};
const serverConfig = {
entry: serverEntry,
output,
resolve: { extensions },
module: { rules },
plugins: [
definedVariables
],
target: 'node',
node: {
__dirname: false,
__filename: false,
},
externals: [nodeExternals()],
};
module.exports = [browserConfig, serverConfig];