-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathwebpack.config.js
309 lines (295 loc) · 8.12 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// SPDX-FileCopyrightText: 2017-2022 City of Espoo
//
// SPDX-License-Identifier: LGPL-2.1-or-later
/* eslint-disable no-console */
const fs = require('fs')
const path = require('path')
const sentryWebpackPlugin =
require('@sentry/webpack-plugin').sentryWebpackPlugin
const HtmlWebpackPlugin = require('html-webpack-plugin')
const TsConfigPaths = require('tsconfig-paths-webpack-plugin')
const webpack = require('webpack')
const WebpackPwaManifest = require('webpack-pwa-manifest')
function resolveCustomizations() {
const customizations = process.env.EVAKA_CUSTOMIZATIONS
if (customizations) {
const customizationsPath = path.resolve(
__dirname,
'src/lib-customizations',
customizations
)
console.info(`Using customizations from ${customizationsPath}`)
return customizations
} else {
return 'espoo'
}
}
function resolveIcons() {
switch (process.env.ICONS) {
case 'pro':
console.info('Using pro icons (forced)')
return 'pro'
case 'free':
console.info('Using free icons (forced)')
return 'free'
case undefined:
break
default:
throw new Error(`Invalid environment variable ICONS=${process.env.ICONS}`)
}
try {
require('@fortawesome/pro-light-svg-icons')
require('@fortawesome/pro-regular-svg-icons')
require('@fortawesome/pro-solid-svg-icons')
console.info('Using pro icons (auto-detected)')
return 'pro'
} catch (e) {
console.info('Using free icons (fallback)')
return 'free'
}
}
const customizationsModule = resolveCustomizations()
const icons = resolveIcons()
function baseConfig({ isDevelopment }, { name, publicPath, entry }) {
const plugins = [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, `src/${name}/index.html`),
templateParameters: {
appBody: fs.readFileSync(
path.resolve(__dirname, 'src/body.html'),
'utf-8'
)
}
}),
new webpack.NormalModuleReplacementPlugin(
/@evaka\/customizations\/(.*)/,
(resource) => {
resource.request = resource.request.replace(
/@evaka\/customizations/,
`lib-customizations/${customizationsModule}`
)
}
),
new webpack.DefinePlugin({
// This matches APP_COMMIT in apigw
'process.env.APP_COMMIT': `'${process.env.APP_COMMIT || 'UNDEFINED'}'`
})
]
// Only create a Sentry release when Sentry is enabled (i.e. production builds).
// SentryWebpackPlugin automatically publishes source maps and creates a release.
if (process.env.SENTRY_PUBLISH_ENABLED === 'true') {
plugins.push(
sentryWebpackPlugin({
org: process.env.SENTRY_ORG || undefined,
release: {
name: process.env.APP_COMMIT,
setCommits: {
repo: 'espoon-voltti/evaka',
commit: process.env.APP_COMMIT,
auto: false
}
},
project: `evaka-${name}`,
sourcemaps: {
assets: path.resolve(__dirname, `dist/bundle/${name}/**`)
}
})
)
}
return {
name,
context: path.resolve(__dirname, `src/${name}`),
mode: isDevelopment ? 'development' : 'production',
devtool: isDevelopment ? 'cheap-module-source-map' : 'source-map',
entry: entry ?? path.resolve(__dirname, `src/${name}/index.tsx`),
output: {
filename: isDevelopment ? '[name].js' : '[name].[contenthash].js',
path: path.resolve(__dirname, `dist/bundle/${name}`),
publicPath,
assetModuleFilename: isDevelopment
? '[name][ext][query][fragment]'
: '[name].[contenthash][ext][query][fragment]'
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
symlinks: false,
alias: {
Icons:
icons === 'pro'
? path.resolve(__dirname, 'src/lib-icons/pro-icons')
: path.resolve(__dirname, 'src/lib-icons/free-icons')
},
plugins: [
new TsConfigPaths({
configFile: path.resolve(__dirname, `src/${name}/tsconfig.json`)
})
]
},
plugins,
module: {
rules: [
// JS/TS/JSON
{
test: /\.(ts|tsx|json)$/,
exclude: /[\\/]node_modules[\\/]/,
use: [
{
loader: 'babel-loader',
options: {
presets: [
[
'@babel/preset-env',
{
corejs: '3.37',
useBuiltIns: 'entry'
}
]
],
plugins: [
[
'babel-plugin-styled-components',
{
displayName: false,
fileName: false,
pure: true
}
]
]
}
},
{
loader: 'ts-loader',
options: {
projectReferences: true
}
}
]
},
// All CSS
{
test: /\.css$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: { importLoaders: 1 }
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
config: path.resolve(__dirname, 'package.json')
}
}
}
]
},
// Static files
{
test: /\.(woff|woff2|otf|ttf|eot|svg|png|gif|jpg|ico)$/,
type: 'asset/resource'
}
]
},
optimization: {
splitChunks: {
// Service workers must *not* use any kind of "chunk splitting" magic,
// because a service worker is supposed to be contained in a single file
chunks: (chunk) => chunk.name !== 'service-worker',
cacheGroups: {
defaultVendors: false,
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
// See above
chunks: (chunk) => chunk.name !== 'service-worker'
}
}
}
},
stats: {
children: false,
colors: true,
entrypoints: true,
modules: false
},
performance: {
hints: false
}
}
}
function citizen(flags) {
return baseConfig(flags, {
name: 'citizen-frontend',
publicPath: '/'
})
}
function employee(flags) {
return baseConfig(flags, {
name: 'employee-frontend',
publicPath: '/employee/'
})
}
function employeeMobile(flags) {
const config = baseConfig(flags, {
name: 'employee-mobile-frontend',
publicPath: '/employee/mobile/',
entry: {
main: path.resolve(__dirname, 'src/employee-mobile-frontend/index.tsx'),
'service-worker': {
import: path.resolve(
__dirname,
'src/employee-mobile-frontend/service-worker.js'
),
filename: '[name].js'
}
}
})
config.plugins.push(
new WebpackPwaManifest({
fingerprints: !flags.isDevelopment,
ios: true,
name: 'eVaka',
display: 'standalone',
start_url: '/employee/mobile',
background_color: '#ffffff',
theme_color: '#3273c9',
icons: [
{
ios: true,
src: path.resolve(
__dirname,
'src/employee-mobile-frontend/assets/evaka-180px.png'
),
size: 180,
type: 'image/png',
purpose: 'maskable any'
},
{
src: path.resolve(
__dirname,
'src/employee-mobile-frontend/assets/evaka-192px.png'
),
size: 192,
type: 'image/png',
purpose: 'maskable any'
},
{
src: path.resolve(
__dirname,
'src/employee-mobile-frontend/assets/evaka-512px.png'
),
size: 512,
type: 'image/png',
purpose: 'maskable any'
}
]
})
)
return config
}
module.exports = (_env, argv) => {
const isDevelopment = !!(argv && argv['mode'] !== 'production')
const flags = { isDevelopment }
return [citizen(flags), employee(flags), employeeMobile(flags)]
}