-
Notifications
You must be signed in to change notification settings - Fork 32
/
webpack.config.js
233 lines (215 loc) · 8.46 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
import HtmlWebpackPlugin from "html-webpack-plugin";
import HtmlInlineScriptPlugin from 'html-inline-script-webpack-plugin';
import FaviconsWebpackPlugin from "favicons-webpack-plugin";
import { CleanWebpackPlugin } from "clean-webpack-plugin";
import WorkboxPlugin from "workbox-webpack-plugin";
import ReactRefreshTypeScript from 'react-refresh-typescript';
import ReactRefreshWebpackPlugin from '@pmmmwh/react-refresh-webpack-plugin';
import webpack from "webpack";
import { execSync } from "child_process";
import moment from "moment";
import path from "path";
import fetch from "node-fetch";
import { fileURLToPath } from 'url';
import * as sass from "sass";
function parseChangelog() {
return execSync("git log --grep \"^Changelog: \" -10")
.toString()
.split(/^commit /m)
.slice(1)
.map((commit) => {
const changelogIndex = commit.indexOf(" Changelog: ");
if (changelogIndex === -1) {
throw `Changelog not found in commit:\n${commit}`;
}
const dateString = commit.match(/^Date: (.*)$/m)?.[1];
if (!dateString) {
throw `Date not found in commit:\n${commit}`;
}
const date = moment(new Date(dateString)).utc().format("YYYY-MM-DD");
const id = commit.substring(0, commit.indexOf("\n"));
const message = commit
.substring(changelogIndex + 15)
.replaceAll("\n ", "\n")
.trim();
return {
id,
message,
date,
};
})
.filter((changelog) => changelog.message);
}
export default async (env, argv) => {
const getContributorsForRepo = async (repoName) => {
const contributorsData = await fetch(`https://api.github.com/repos/LiveSplit/${repoName}/contributors`, {
headers: {
"Authorization": env.GITHUB_TOKEN ? `Bearer ${env.GITHUB_TOKEN}` : undefined,
},
});
return contributorsData.json();
}
const lsoContributorsList = await getContributorsForRepo("LiveSplitOne");
const coreContributorsList = await getContributorsForRepo("livesplit-core");
const coreContributorsMap = {};
for (const coreContributor of coreContributorsList) {
if (coreContributor.type === "User" && !coreContributor.login.includes("dependabot")) {
coreContributorsMap[coreContributor.login] = coreContributor;
}
}
for (let lsoContributor of lsoContributorsList) {
const existingContributor = coreContributorsMap[lsoContributor.login];
if (existingContributor) {
existingContributor.contributions += lsoContributor.contributions;
} else if (lsoContributor.type === "User" && !lsoContributor.login.includes("dependabot")) {
coreContributorsMap[lsoContributor.login] = lsoContributor;
}
}
const contributorsList = Object.values(coreContributorsMap)
// Sort by contributions, but fallback to alphabetical order for the
// same amount of contributions
.sort((a, b) => a.login > b.login ? 1 : b.login > a.login ? -1 : 0)
.sort((a, b) => b.contributions - a.contributions)
.map((user) => {
return { id: user.id, name: user.login };
});
const commitHash = execSync("git rev-parse --short HEAD").toString();
const date = moment.utc().format("YYYY-MM-DD kk:mm:ss z");
const changelog = parseChangelog();
const basePath = path.dirname(fileURLToPath(import.meta.url));
const isProduction = argv.mode === "production";
const isTauri = env.TAURI === "true";
const distPath = path.join(...[
basePath,
...(isTauri ? ["src-tauri", "target", "dist"] : ["dist"]),
]);
return {
entry: {
"bundle": ["./src/index.tsx"],
},
output: {
filename: "[name].js",
path: distPath,
publicPath: '',
},
devtool: isProduction ? undefined : "source-map",
devServer: {
port: 8080,
hot: true
},
resolve: {
extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js", ".json", ".wasm"],
},
plugins: [
...(isProduction ? [new CleanWebpackPlugin({
protectWebpackAssets: false,
cleanAfterEveryBuildPatterns: ['*.LICENSE.txt'],
})] : []),
...(isTauri ? [] : [new FaviconsWebpackPlugin({
logo: path.resolve("src/assets/icon.svg"),
inject: true,
logoMaskable: path.resolve("src/assets/maskable.svg"),
favicons: {
appName: "LiveSplit One",
appDescription: "A version of LiveSplit that works on a lot of platforms.",
developerName: "CryZe",
developerURL: "https://livesplit.org",
background: "#171717",
theme_color: "#232323",
appleStatusBarStyle: "black-translucent",
icons: {
appleIcon: {
offset: 10,
},
appleStartup: {
offset: 15,
},
windows: false,
coast: false,
yandex: false,
},
start_url: "/",
},
})]),
new HtmlWebpackPlugin({
template: "./src/index.html",
}),
new webpack.DefinePlugin({
BUILD_DATE: JSON.stringify(date),
COMMIT_HASH: JSON.stringify(commitHash),
CONTRIBUTORS_LIST: JSON.stringify(contributorsList),
CHANGELOG: JSON.stringify(changelog),
}),
...(isProduction ? [
new HtmlInlineScriptPlugin({
scriptMatchPattern: ['^bundle.js$'],
}),
...(isTauri ? [] : [new WorkboxPlugin.GenerateSW({
clientsClaim: true,
skipWaiting: true,
maximumFileSizeToCacheInBytes: 100 * 1024 * 1024,
exclude: [
/^assets/,
/\.LICENSE\.txt$/,
],
runtimeCaching: [{
urlPattern: (context) => {
return self.origin === context.url.origin &&
context.url.pathname.startsWith("/assets/");
},
handler: "CacheFirst",
}],
})])
] : []),
...(!isProduction ? [new ReactRefreshWebpackPlugin()] : []),
],
module: {
rules: [
{
test: /\.tsx?$/,
use: [
{
loader: "ts-loader",
options: {
getCustomTransformers: () => ({
before: [!isProduction && ReactRefreshTypeScript()].filter(Boolean),
}),
transpileOnly: !isProduction,
},
},
],
exclude: "/node_modules",
},
{
test: /\.(s?)css$/,
use: [
"style-loader",
{
loader: "css-loader",
options: {
importLoaders: 1,
modules: "icss",
},
},
{
loader: "sass-loader",
options: {
// Prefer `dart-sass`
implementation: sass,
},
},
],
},
{
test: /\.(png|jpg|gif|woff|ico|svg)$/,
type: 'asset/resource'
},
],
},
experiments: {
syncWebAssembly: true,
topLevelAwait: true,
},
mode: isProduction ? "production" : "development",
};
};