-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.babel.js
98 lines (84 loc) · 2.04 KB
/
webpack.config.babel.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
import path from 'path';
import nodeExternals from 'webpack-node-externals';
import { GenerateSW } from 'workbox-webpack-plugin';
import regexparam from 'regexparam';
import pageRoutes from './src/pages/routes';
const commonConfig = {
// stats: 'verbose',
};
const pageRoutesRuntimeCaching = pageRoutes
.filter(({ cache }) => cache)
.map(({ path: routePath }) => {
const { pattern: pagePathRegExp } = regexparam(routePath);
return {
urlPattern: eval(`({url}) => {
return ${pagePathRegExp}.exec(url.pathname);
}`),
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'page-route-cache',
},
};
});
const generateSWConfig = {
clientsClaim: true,
skipWaiting: true,
runtimeCaching: [
...pageRoutesRuntimeCaching,
{
urlPattern: /https:\/\/hn\.algolia\.com\/api/,
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'algolia-api-cache',
expiration: {
maxEntries: 30,
},
},
},
{
urlPattern: /\.(?:png|gif|jpg|jpeg|svg|ico|json)$/,
handler: 'CacheFirst',
options: {
cacheName: 'assets-cache',
expiration: {
maxEntries: 60,
maxAgeSeconds: 1 * 24 * 60 * 60, // 1 Day
},
},
},
],
};
const clientConfig = {
...commonConfig,
entry: './src/client/index.js',
plugins: [],
module: {
rules: [{ test: /\.js/, exclude: /node_modules/, use: 'babel-loader' }],
},
output: {
path: path.resolve(__dirname, 'build'),
filename: 'client.js',
},
};
const serverConfig = {
...commonConfig,
target: 'node',
externals: [nodeExternals()],
entry: './src/server/index.js',
module: {
rules: [{ test: /\.js/, use: 'babel-loader' }],
},
output: {
path: path.resolve(__dirname, 'build'),
filename: 'server.js',
},
node: {
__dirname: false,
},
};
export default (env, { mode }) => {
if (mode === 'production') {
clientConfig.plugins.push(new GenerateSW(generateSWConfig));
}
return [clientConfig, serverConfig];
};