-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
137 lines (126 loc) · 3.52 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
import * as crypto from 'crypto';
import * as path from 'path';
import { fileURLToPath } from 'url';
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import { RelativeCiAgentWebpackPlugin } from '@relative-ci/agent';
import { InjectManifest } from 'workbox-webpack-plugin';
import webpack from 'webpack';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const mode = process.env.NODE_ENV || 'development';
const prod = mode === 'production';
const plugins = [
new webpack.DefinePlugin({
__BUILD_ID__: `'${getUniqueBuildId()}'`,
__RELEASE_STAGE__: `'${mode === 'production' ? 'prod' : 'test'}'`,
}),
new CleanWebpackPlugin(),
new CopyWebpackPlugin({
patterns: [
{ from: 'img/*', to: '[name][ext]' },
{ from: 'src/manifest.webmanifest', to: 'manifest.webmanifest' },
{ from: '_headers' },
],
}),
new HtmlWebpackPlugin({
template: './src/index.html',
excludeChunks: ['db-worker'],
}),
new MiniCssExtractPlugin({ filename: 'hikibiki.[contenthash].css' }),
];
if (mode !== 'development') {
plugins.push(
new InjectManifest({
swSrc: './src/sw.ts',
exclude: ['_headers'],
})
);
}
// Don't run the Relative CI task unless we mean to -- it corrupts the stats
// file so other tools can't use it.
if (process.env.RELATIVE_CI_KEY) {
plugins.push(new RelativeCiAgentWebpackPlugin());
}
export default {
entry: {
hikibiki: ['./src/main.tsx'],
},
resolve: {
alias: {
react: 'preact/compat',
'react-dom/test-utils': 'preact/test-utils',
'react-dom': 'preact/compat',
},
extensions: ['.ts', '.tsx', '.js'],
},
output: {
// Although this path is the default, it is needed by clean-webpack-plugin
path: path.resolve(__dirname, 'dist'),
filename: '[name].[contenthash].js',
chunkFilename: '[name].[contenthash].js',
},
module: {
rules: [
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: 'ts-loader',
},
{
test: /\.css?$/,
exclude: /node_modules/,
use: [
{ loader: MiniCssExtractPlugin.loader },
{
loader: 'css-loader',
options: {
url: false,
importLoaders: 1,
},
},
{
loader: 'postcss-loader',
},
],
},
],
},
plugins,
mode,
optimization: {
splitChunks: {
minChunks: 2,
},
},
devtool: prod ? false : 'source-map',
devServer: {
static: {
directory: path.resolve(__dirname, 'dist'),
},
},
};
function getUniqueBuildId() {
const EPOCH_START = Date.UTC(2020, 0, 1);
const RANDOM_COMPONENT_LENGTH = 8;
const timeStamp = Date.now() - EPOCH_START;
// Random component
const buffer = new Uint8Array(RANDOM_COMPONENT_LENGTH);
crypto.randomFillSync(buffer);
const max = Math.pow(2, 8);
let randomComponent = 0;
for (let i = 0; i < buffer.length; i++) {
randomComponent *= 36;
randomComponent += Math.round((buffer[i] / max) * 35);
}
return (
// Take the timestamp, convert to base 36, and zero-pad it so it
// collates correctly for at least 50 years...
`0${timeStamp.toString(36)}`.slice(-8) +
// ...then add the random component, also suitably zero-padded
`${'0'.repeat(RANDOM_COMPONENT_LENGTH)}${randomComponent.toString(
36
)}`.slice(-RANDOM_COMPONENT_LENGTH)
);
}