-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.js
173 lines (158 loc) · 4.34 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
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const toml = require('toml');
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { VueLoaderPlugin } = require('vue-loader');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const AutoprefixerPlugin = require('autoprefixer');
const pages = require('./assets/templates/pages.json');
function requireToml(pathToToml) {
return toml.parse(fs.readFileSync(pathToToml, { encoding: 'utf-8' }));
}
const cargoLockfile = requireToml('./wasm/Cargo.lock');
function getDependencyInfo(dependencyName) {
const packageInfo = cargoLockfile.package.find(({ name }) => name === dependencyName);
return {
version: packageInfo.version,
};
}
function getGitInfo() {
const gitOutput = execSync('git status --porcelain=v2 --branch', {
encoding: 'utf8',
});
const gitOutputLines = gitOutput.split('\n');
const commitLine = gitOutputLines.find((line) => line.startsWith('# branch.oid'));
const commitHash = commitLine.match(/\b(?<hash>[0-9a-f]{40})$/).groups.hash;
const isDirty = gitOutputLines.some((line) => line.startsWith('1 ') || line.startsWith('2 '));
return { commitHash, isDirty };
}
function getRustInfo() {
const rustcOutput = execSync('rustc --version', {
encoding: 'utf8',
});
return rustcOutput.replace(/^rustc\s+/, '').trim();
}
const buildInfo = {
deps: ['ed25519-dalek', 'curve25519-dalek', 'wasm-bindgen'].reduce(
(acc, name) => {
acc[name] = getDependencyInfo(name);
return acc;
},
{},
),
git: getGitInfo(),
rust: getRustInfo(),
};
const entries = {
index: './src/home',
about: './src/about',
basics: './src/basics',
malleability: './src/malleability',
wildcards: './src/wildcards',
clamping: './src/clamping',
};
const htmlPlugins = Object.keys(entries).map((entry) => new HtmlWebpackPlugin({
filename: entry === 'index' ? 'index.html' : `${entry}/index.html`,
chunks: [entry, 'commons'],
template: `assets/templates/${entry}.pug`,
templateParameters: {
$pages: pages,
$buildInfo: buildInfo,
},
}));
module.exports = {
entry: entries,
output: {
path: path.resolve(__dirname, 'dist'),
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,
topLevelAwait: true,
},
optimization: {
splitChunks: {
chunks: 'async',
cacheGroups: {
vendors: false, // disable splitting the main chunk into 3rd-party and built-in parts
commons: {
name: 'commons',
chunks: 'initial',
minChunks: Object.keys(entries).length,
},
},
},
},
module: {
rules: [
{
test: /\.js/,
exclude: /node_modules/,
use: 'babel-loader',
},
{
test: /\.vue$/,
use: 'vue-loader',
},
{
test: /\.css$/,
use: [
'vue-style-loader',
'css-loader',
],
},
{
test: /\.scss$/i,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [AutoprefixerPlugin],
},
},
},
'sass-loader',
],
},
{
test: /\.pug$/i,
loader: 'pug-loader',
},
{
test: /\.(woff|woff2)$/i,
type: 'asset',
},
],
},
plugins: [
new webpack.DefinePlugin({
__VUE_OPTIONS_API__: true,
__VUE_PROD_DEVTOOLS__: false,
}),
new CopyWebpackPlugin({
patterns: [{
from: './assets/favicon',
to: '_assets/css/[name][ext]',
}],
}),
new MiniCssExtractPlugin({
filename: '_assets/css/[name].css',
}),
new VueLoaderPlugin(),
// This hard-codes the relative path to the `TextDecoder` module from the `wasm/pkg` directory,
// which is the only place using the `TextDecoder` global.
new webpack.ProvidePlugin({
TextDecoder: ['../../src/TextDecoder.js', 'default'],
}),
...htmlPlugins,
],
};