-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
131 lines (125 loc) · 3.57 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
const { mkdir } = require('fs');
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WasmPackPlugin = require('@wasm-tool/wasm-pack-plugin');
const AutoprefixerPlugin = require('autoprefixer');
const PrerenderSPAPlugin = require('prerender-spa-plugin');
const { InjectManifest } = require('workbox-webpack-plugin');
const Renderer = PrerenderSPAPlugin.PuppeteerRenderer;
const distPath = path.resolve(__dirname, 'dist');
// Monkey-patches `mkdirp` function to `compiler.outputFileSystem`, which is used
// by `PrerenderSPAPlugin`.
class MkdirpProviderPlugin {
// eslint-disable-next-line class-methods-use-this
apply(compiler) {
// eslint-disable-next-line no-param-reassign
compiler.outputFileSystem.mkdirp = (dirPath, options, callback) => {
mkdir(dirPath, { ...(options || {}), recursive: true }, callback);
};
}
}
const config = {
entry: { index: './webpack/index.js' },
output: {
path: distPath,
publicPath: process.env.WEBPACK_PUBLIC_PATH || '/',
filename: '_assets/js/[name].js',
chunkFilename: '_assets/js/[name].[chunkhash:8].js',
webassemblyModuleFilename: '_assets/js/[hash].module.wasm',
},
experiments: {
asyncWebAssembly: true,
},
module: {
rules: [
{
test: /\.js/,
exclude: /node_modules/,
use: 'babel-loader',
},
{
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, 'css-loader'],
},
{
test: /\.scss$/i,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [AutoprefixerPlugin],
},
},
},
'sass-loader',
],
},
{
test: /\.(woff|woff2)$/i,
type: 'asset',
},
],
},
optimization: {
splitChunks: {
chunks: 'all',
cacheGroups: {
vendors: false, // disable splitting the main chunk into 3rd-party and built-in parts
},
},
},
plugins: [
new CopyWebpackPlugin({
patterns: [{
from: './webpack/favicon',
to: '_assets/css/[name][ext]',
}],
}),
new MiniCssExtractPlugin({
filename: '_assets/css/[name].css',
}),
new WasmPackPlugin({
crateDirectory: '.',
extraArgs: '--no-typescript',
}),
new HtmlWebpackPlugin({
filename: 'index.html',
chunks: ['index'],
template: 'webpack/index.html',
}),
],
};
module.exports = (env, argv) => {
const serviceWorkerExcludes = (argv.mode === 'development')
? [/.*/] // exclude precaching for dev builds (leads to infinite reloading loops)
: [];
config.plugins.push(new InjectManifest({
swSrc: './webpack/service-worker.js',
swDest: 'service-worker.js',
exclude: serviceWorkerExcludes,
}));
if (argv.mode === 'production') {
config.plugins.push(
new MkdirpProviderPlugin(),
new PrerenderSPAPlugin({
staticDir: path.join(__dirname, 'dist'),
routes: ['/', '/about', '/implementation'],
renderer: new Renderer({
injectProperty: '__PRERENDER__',
inject: {}, // we don't need any properties, just for the global var to be set
renderAfterDocumentEvent: 'wasm-rendered',
}),
}),
);
} else if (argv.mode === 'development') {
config.devServer = {
historyApiFallback: true,
};
}
return config;
};