This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.config.js
177 lines (154 loc) · 5.62 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
// Copyright 2021 Peter Beverloo & AnimeCon. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.
const childProcess = require('child_process');
const path = require('path');
const webpack = require('webpack');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { GenerateSW } = require('workbox-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MomentLocalesPlugin = require('moment-locales-webpack-plugin');
const MomentTimezoneDataPlugin = require('moment-timezone-data-webpack-plugin');
function createCopyDestinationFn(prefix) {
return ({ context, absoluteFilename }) =>
prefix + path.basename(absoluteFilename);
}
function getGitCommitHash() {
return childProcess.execSync(`git describe --always`, { encoding: 'utf8' }).trim();
}
module.exports = {
entry: './src/index.tsx',
devtool: 'source-map',
mode: 'development',
module: {
rules: [
{
test: /\.js$/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-typescript'],
}
},
exclude: /node_modules/,
},
{
test: /\.tsx?$/,
use: [
{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-typescript'],
plugins: ['@babel/plugin-proposal-class-properties'],
}
},
{
loader: 'ts-loader',
}
],
exclude: /node_modules/,
},
]
},
resolve: {
extensions: [ '.ts', '.tsx', '.js' ],
alias: {
'react': 'preact/compat',
'react-dom': 'preact/compat',
}
},
optimization: {
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/i,
name: 'vendors',
chunks: 'all'
}
}
}
},
output: {
filename: '[name].[chunkhash:8].js',
path: path.resolve(__dirname, 'dist'),
},
devServer: {
historyApiFallback: {
rewrites: [
// Rewrites the paths part of the SPA back to index.html
{
from: /^\/registration\/?.*$/,
to: '/index.html',
},
// Rewrites image requests to the stewards configuration. The actual webserver will
// rewrite paths based on the hostname, which is included in nginx.conf.
{
from: /^\/images\/.*$/,
to: context =>
'/static/images/stewards/' + path.basename(context.parsedUrl.pathname)
}
],
},
port: 4000,
open: true,
hot: true
},
plugins: [
new webpack.EnvironmentPlugin({
'REACT_APP_API_HOST': '',
'REACT_APP_GIT_VERSION': getGitCommitHash(),
}),
new MomentLocalesPlugin({ localesToKeep: [ 'en-gb' ] }),
new MomentTimezoneDataPlugin({
matchZones: /^Europe\//,
startYear: 2022,
}),
new CleanWebpackPlugin(),
new CopyWebpackPlugin({
patterns: [
{ from: 'static/**/*', to: '' },
// Duplicate common image resources to both environments.
{ from: 'static/images/*.*', to: createCopyDestinationFn('static/images/gophers/') },
{ from: 'static/images/*.*', to: createCopyDestinationFn('static/images/hosts/') },
{ from: 'static/images/*.*', to: createCopyDestinationFn('static/images/stewards/') },
],
}),
...(
// Only generate the service worker in production builds. This is because Workbox has
// some odd way of interacting with emits that's incompatible with the plugin.
// https://github.com/GoogleChrome/workbox/issues/1790#issuecomment-544982014
process.env.NODE_PROD === '1'
? [ new GenerateSW({
clientsClaim: true,
skipWaiting: true,
navigateFallback: '/index.html',
navigateFallbackAllowlist: [
/\/schedule\//,
/\?app$/,
],
runtimeCaching: [
{
urlPattern: /\/avatars\//,
handler: 'CacheFirst',
options: {
cacheName: 'vp-avatars',
},
},
],
}) ]
: []),
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'src/index.html',
base: '/', // Enables relative URLs to work painlessly
hash: false, // Cache busting is enabled through chunk hashes
}),
new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false,
reportFilename: 'index-sizes.html',
}),
],
}