-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.cjs
232 lines (225 loc) · 5.96 KB
/
webpack.config.cjs
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
const { includes } = require('ramda')
const path = require('path');
const { IgnorePlugin, DefinePlugin } = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const WorkboxPlugin = require('workbox-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin')
const CompressionPlugin = require('compression-webpack-plugin');
const zlib = require('zlib');
const mode = process.env.NODE_ENV || 'development';
const prod = mode === 'production';
const githash = require('child_process')
.execSync('git rev-parse HEAD')
.toString().trim()
const WorkboxServiceWorker = new WorkboxPlugin.GenerateSW({
//include: [path.resolve(__dirname, 'public')],
include: [],
additionalManifestEntries: [
// consider precaching this when stable
//'/',
{
url: '/argon2id.worker.js',
// TODO update this with a real subhash or rev number, should stay pretty static though
// Will be using commit hash that necessitated rev update
revision: '0e3080f68de94ade475ab73ef9e0ce8687625110',
},
],
runtimeCaching: [
{
urlPattern: /\.png$/,
handler: 'CacheFirst',
},
{
urlPattern: '/',
handler: 'StaleWhileRevalidate',
},
{
urlPattern: '/bundle.js',
handler: 'StaleWhileRevalidate',
},
{
urlPattern: /\.css$/,
handler: 'StaleWhileRevalidate',
},
{
urlPattern: /\.js$/,
handler: 'StaleWhileRevalidate',
},
],
maximumFileSizeToCacheInBytes: 5000000,
// these options encourage the ServiceWorkers to get in there fast
// and not allow any straggling "old" SWs to hang around
clientsClaim: true,
skipWaiting: true,
})
module.exports = {
node: {
fs: 'empty'
},
entry: {
bundle: ['./src/main.js']
},
resolve: {
alias: {
svelte: path.resolve('node_modules', 'svelte'),
'@': path.resolve(__dirname, 'src'),
'@root': path.resolve(__dirname, ''),
'@public': path.resolve(__dirname, 'src/public'),
'@component': path.resolve(__dirname, 'src/component'),
'@modal': path.resolve(__dirname, 'src/modal'),
'@worker': path.resolve(__dirname, 'src/worker'),
'@util': path.resolve(__dirname, 'src/util'),
'@store': path.resolve(__dirname, 'src/store')
},
extensions: ['.mjs', '.js', '.svelte'],
mainFields: ['svelte', 'browser', 'module', 'main']
},
output: {
path: __dirname + '/dist',
filename: '[name].js',
chunkFilename: '[name].[id].js'
},
module: {
noParse: /\.wasm$/,
rules: [
{
test: /\.m?js$/,
include: /src/,
use: ['babel-loader'],
},
{
test: /\.svelte$/,
include: /src/,
use: ['babel-loader', {
loader: 'svelte-loader',
options: {
emitCss: true,
hotReload: true,
onwarn: (warning, handleWarning) => {
const ignored = [
'autofocus'
]
const warnMsg = warning.code.replace('a11y-', '')
if (includes(warnMsg, ignored))
return
handleWarning(warning)
}
}
}]
},
{
test: /\.css$/i,
use: [
/**
* MiniCssExtractPlugin doesn't support HMR.
* For developing, use 'style-loader' instead.
* */
prod ? MiniCssExtractPlugin.loader :
'style-loader',
{
loader: 'css-loader',
options: {
esModule: false
}
}
]
},
{
test: /\.wasm$/,
// Tells WebPack that this module should be included as
// base64-encoded binary file and not as code
loaders: ['base64-loader'],
// Disables WebPack's opinion where WebAssembly should be,
// makes it think that it's not WebAssembly
//
// Error: WebAssembly module is included in initial chunk.
type: 'javascript/auto'
},
{
include: /public\//,
test: /\.webmanifest$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]'
}
}
]
},
{
test: /\.worker\.js$/,
loader: "worker-loader",
options: {
filename: "[name].js",
},
}
]
},
mode,
plugins: [
new MiniCssExtractPlugin({
filename: '[name].css'
}),
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: 'src/public/index.html',
favicon: 'src/public/favicon.png',
files: {
manifest: 'src/public/manifest.webmanifest',
css: 'src/public/global.css',
},
scriptLoading: 'defer'
}),
new IgnorePlugin(/^\.\/wordlists\/(?!english)/, /bip39\/src$/),
...(prod ? [WorkboxServiceWorker] : []),
new DefinePlugin({
'process.env.GIT_HASH': JSON.stringify(githash),
}),
new CompressionPlugin({
algorithm: 'gzip',
test: /\.js$|\.css$|\.html$/,
threshold: 10240,
minRatio: 0.75
}),
new CompressionPlugin({
algorithm: 'brotliCompress',
test: /\.js$|\.css$|\.html$/,
threshold: 10240,
minRatio: 0.75,
filename: '[path][base].br',
compressionOptions: {
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: 11,
},
},
}),
],
devtool: prod ? false : 'eval-source-map',
optimization: !prod ? {} : {
splitChunks: {
chunks: 'all',
maxSize: 400000,
minSize: 300000,
},
runtimeChunk: true,
usedExports: true,
mangleWasmImports: true,
sideEffects: true,
minimize: true,
minimizer: [
new TerserPlugin({
cache: true,
parallel: true,
terserOptions: {
ecma: 2020
, output: {
comments: false
}
}
})
]
},
};