-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
215 lines (209 loc) · 5.97 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
const path = require('path');
const history = require('connect-history-api-fallback');
const convert = require('koa-connect');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ScriptExtHtmlPlugin = require('script-ext-html-webpack-plugin');
const HtmlWebpackExternalsPlugin = require('html-webpack-externals-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const WorkboxPlugin = require('workbox-webpack-plugin');
const WebpackBar = require('webpackbar');
const webpack = require('webpack');
const isProd = process.env.NODE_ENV === 'production';
module.exports = {
target: 'web',
entry: {
app: './src/app-shell.js'
},
mode: isProd ? 'production' : 'development',
output: {
path: path.resolve(__dirname, './dist'),
chunkFilename: !isProd
? `module/[name].js`
: `module/[name].[chunkhash:8].js`,
filename: !isProd ? `module/[name].js` : `module/[name].[chunkhash:8].js`,
publicPath: '/'
},
resolve: {
extensions: ['.js'],
modules: ['./src', 'node_modules']
},
module: {
rules: [
{
test: /\.js$/,
type: 'javascript/esm',
exclude: /node_modules/
},
{
test: /\.(jpe?g|png|gif|svg)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 8192, // return DataURL if image size <= 8KB
name: 'assets/[name].[ext]',
fallback: 'file-loader' // use file loader for size > 8KB
}
}
]
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: ['file-loader']
}
]
},
plugins: [
new HtmlWebpackPlugin({
minify: {
collapseWhitespace: isProd,
removeComments: isProd,
removeRedundantAttributes: isProd,
removeScriptTypeAttributes: isProd,
removeStyleLinkTypeAttributes: isProd,
sortAttributes: isProd,
sortClassName: isProd,
useShortDoctype: isProd,
minifyCSS: isProd,
minifyJS: isProd,
caseSensitive: isProd
},
hash: isProd,
inject: true,
template: './public/index.html'
}),
new ScriptExtHtmlPlugin({
defaultAttribute: 'defer',
module: ['app']
}),
new HtmlWebpackExternalsPlugin({
externals: [
{
module: '@webcomponents/webcomponentsjs',
entry: 'webcomponents-loader.js',
supplements: ['bundles']
}
]
}),
new WebpackBar(),
...(isProd
? [
new WorkboxPlugin.GenerateSW({
swDest: 'sw.js',
clientsClaim: true,
skipWaiting: true,
// Exclude images from the precache
exclude: [/\.(?:png|jpg|jpeg|svg|ico|webp)$/],
// Define runtime caching rules.
runtimeCaching: [
{
urlPattern: /^(https?.*)/,
handler: 'networkFirst',
options: {
cacheName: 'cache-https',
expiration: {
maxEntries: 50
},
networkTimeoutSeconds: 3
}
},
{
urlPattern: new RegExp('https://fonts.googleapis.com/(.*)'),
// Apply a cache-first strategy.
handler: 'cacheFirst',
options: {
cacheName: 'googleapis',
expiration: {
maxEntries: 50
}
}
},
{
// Match any request ends with .png, .jpg, .jpeg or .svg.
urlPattern: /\.(?:png|jpg|jpeg|svg)$/,
// Apply a cache-first strategy.
handler: 'cacheFirst',
options: {
cacheName: 'images-cache',
// Only cache 10 images.
expiration: {
maxEntries: 60,
maxAgeSeconds: 30 * 24 * 60 * 60 // 30 Days
},
cacheableResponse: {
statuses: [0, 200]
}
}
},
// {
// urlPattern: /\.(?:js|css)$/,
// handler: 'staleWhileRevalidate',
// options: {
// cacheName: 'static-resources'
// }
// },
{
urlPattern: /.*(?:googleapis)\.com.*$/,
handler: 'staleWhileRevalidate',
options: {
cacheName: 'googleapis-cache'
}
},
{
urlPattern: /.*(?:gstatic)\.com.*$/,
handler: 'staleWhileRevalidate',
options: {
cacheName: 'gstatic-cache'
}
}
]
}),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
})
// new BundleAnalyzerPlugin({ openAnalyzer: false })
]
: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('development')
})
])
],
optimization: {
nodeEnv: 'production',
concatenateModules: true,
minimizer: [
new UglifyJsPlugin({
uglifyOptions: { ecma: 6 },
cache: true,
// parallel: true,
sourceMap: true // set to true if you want JS source maps
})
],
splitChunks: {
chunks: 'all',
cacheGroups: {
commons: {
test: /node_modules/,
chunks: 'initial',
name: 'vendor',
priority: 10,
enforce: true
}
}
}
}
};
if (!isProd) {
module.exports.serve = {
content: [__dirname],
logLevel: 'error',
hot: true,
add: (app, middleware, options) => {
const historyOptions = {
// ... see: https://github.com/bripkens/connect-history-api-fallback#options
};
app.use(convert(history(historyOptions)));
}
};
}