-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.mjs
321 lines (274 loc) · 8.36 KB
/
build.mjs
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* eslint-disable no-console */
import * as esbuild from "esbuild";
import copyStaticFiles from "esbuild-copy-static-files";
import path from "path";
import fs from "fs";
const config = {
injector: "packages/injector/src/index.ts",
"node-preload": "packages/node-preload/src/index.ts",
"web-preload": "packages/web-preload/src/index.ts"
};
const prod = process.env.NODE_ENV === "production";
const watch = process.argv.includes("--watch");
const browser = process.argv.includes("--browser");
const mv2 = process.argv.includes("--mv2");
const clean = process.argv.includes("--clean");
const buildBranch = process.env.MOONLIGHT_BRANCH ?? "dev";
const buildVersion = process.env.MOONLIGHT_VERSION ?? "dev";
const external = [
"electron",
"fs",
"path",
"module",
"discord", // mappings
// Silence an esbuild warning
"./node-preload.js"
];
let lastMessages = new Set();
/** @type {import("esbuild").Plugin} */
const deduplicatedLogging = {
name: "deduplicated-logging",
setup(build) {
build.onStart(() => {
lastMessages.clear();
});
build.onEnd(async (result) => {
const formatted = await Promise.all([
esbuild.formatMessages(result.warnings, {
kind: "warning",
color: true
}),
esbuild.formatMessages(result.errors, { kind: "error", color: true })
]).then((a) => a.flat());
// console.log(formatted);
for (const message of formatted) {
if (lastMessages.has(message)) continue;
lastMessages.add(message);
console.log(message.trim());
}
});
}
};
const timeFormatter = new Intl.DateTimeFormat(undefined, {
hour: "numeric",
minute: "numeric",
second: "numeric",
hour12: false
});
/** @type {import("esbuild").Plugin} */
const taggedBuildLog = (tag) => ({
name: "build-log",
setup(build) {
build.onEnd((result) => {
console.log(`[${timeFormatter.format(new Date())}] [${tag}] build finished`);
});
}
});
async function build(name, entry) {
let outfile = path.join("./dist", name + ".js");
const browserDir = mv2 ? "browser-mv2" : "browser";
if (name === "browser") outfile = path.join("./dist", browserDir, "index.js");
const dropLabels = [];
const labels = {
injector: ["injector"],
nodePreload: ["node-preload"],
webPreload: ["web-preload"],
browser: ["browser"],
webTarget: ["web-preload", "browser"],
nodeTarget: ["node-preload", "injector"]
};
for (const [label, targets] of Object.entries(labels)) {
if (!targets.includes(name)) {
dropLabels.push(label);
}
}
const define = {
MOONLIGHT_ENV: `"${name}"`,
MOONLIGHT_PROD: prod.toString(),
MOONLIGHT_BRANCH: `"${buildBranch}"`,
MOONLIGHT_VERSION: `"${buildVersion}"`
};
for (const iterName of ["injector", "node-preload", "web-preload", "browser"]) {
const snake = iterName.replace(/-/g, "_").toUpperCase();
define[`MOONLIGHT_${snake}`] = (name === iterName).toString();
}
const nodeDependencies = ["glob"];
const ignoredExternal = name === "web-preload" ? nodeDependencies : [];
const plugins = [deduplicatedLogging, taggedBuildLog(name)];
if (name === "browser") {
plugins.push(
copyStaticFiles({
src: mv2 ? "./packages/browser/manifestv2.json" : "./packages/browser/manifest.json",
dest: `./dist/${browserDir}/manifest.json`
})
);
if (!mv2) {
plugins.push(
copyStaticFiles({
src: "./packages/browser/modifyResponseHeaders.json",
dest: `./dist/${browserDir}/modifyResponseHeaders.json`
})
);
plugins.push(
copyStaticFiles({
src: "./packages/browser/blockLoading.json",
dest: `./dist/${browserDir}/blockLoading.json`
})
);
}
plugins.push(
copyStaticFiles({
src: mv2 ? "./packages/browser/src/background-mv2.js" : "./packages/browser/src/background.js",
dest: `./dist/${browserDir}/background.js`
})
);
}
/** @type {import("esbuild").BuildOptions} */
const esbuildConfig = {
entryPoints: [entry],
outfile,
format: "iife",
globalName: "module.exports",
platform: ["web-preload", "browser"].includes(name) ? "browser" : "node",
treeShaking: true,
bundle: true,
minify: prod,
sourcemap: "inline",
external: [...ignoredExternal, ...external],
define,
dropLabels,
logLevel: "silent",
plugins,
// https://github.com/evanw/esbuild/issues/3944
footer:
name === "web-preload"
? {
js: `\n//# sourceURL=${name}.js`
}
: undefined
};
if (name === "browser") {
const coreExtensionsJson = {};
function readDir(dir) {
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = dir + "/" + file;
const normalizedPath = filePath.replace("./dist/core-extensions/", "");
if (fs.statSync(filePath).isDirectory()) {
readDir(filePath);
} else {
coreExtensionsJson[normalizedPath] = fs.readFileSync(filePath, "utf8");
}
}
}
readDir("./dist/core-extensions");
esbuildConfig.banner = {
js: `window._moonlight_coreExtensionsStr = ${JSON.stringify(JSON.stringify(coreExtensionsJson))};`
};
}
if (watch) {
const ctx = await esbuild.context(esbuildConfig);
await ctx.watch();
} else {
await esbuild.build(esbuildConfig);
}
}
async function buildExt(ext, side, fileExt) {
const outdir = path.join("./dist", "core-extensions", ext);
if (!fs.existsSync(outdir)) {
fs.mkdirSync(outdir, { recursive: true });
}
const entryPoints = [`packages/core-extensions/src/${ext}/${side}.${fileExt}`];
const wpModulesDir = `packages/core-extensions/src/${ext}/webpackModules`;
if (fs.existsSync(wpModulesDir) && side === "index") {
const wpModules = fs.opendirSync(wpModulesDir);
for await (const wpModule of wpModules) {
if (wpModule.isFile()) {
entryPoints.push(`packages/core-extensions/src/${ext}/webpackModules/${wpModule.name}`);
} else {
for (const fileExt of ["ts", "tsx"]) {
const path = `packages/core-extensions/src/${ext}/webpackModules/${wpModule.name}/index.${fileExt}`;
if (fs.existsSync(path)) {
entryPoints.push({
in: path,
out: `webpackModules/${wpModule.name}`
});
}
}
}
}
}
const wpImportPlugin = {
name: "webpackImports",
setup(build) {
build.onResolve({ filter: /^@moonlight-mod\/wp\// }, (args) => {
const wpModule = args.path.replace(/^@moonlight-mod\/wp\//, "");
return {
path: wpModule,
external: true
};
});
}
};
const styleInput = `packages/core-extensions/src/${ext}/style.css`;
const styleOutput = `dist/core-extensions/${ext}/style.css`;
const esbuildConfig = {
entryPoints,
outdir,
format: "iife",
globalName: "module.exports",
platform: "node",
treeShaking: true,
bundle: true,
sourcemap: prod ? false : "inline",
external,
logOverride: {
"commonjs-variable-in-esm": "verbose"
},
logLevel: "silent",
plugins: [
copyStaticFiles({
src: `./packages/core-extensions/src/${ext}/manifest.json`,
dest: `./dist/core-extensions/${ext}/manifest.json`
}),
...(fs.existsSync(styleInput)
? [
copyStaticFiles({
src: styleInput,
dest: styleOutput
})
]
: []),
wpImportPlugin,
deduplicatedLogging,
taggedBuildLog(`ext/${ext}`)
]
};
if (watch) {
const ctx = await esbuild.context(esbuildConfig);
await ctx.watch();
} else {
await esbuild.build(esbuildConfig);
}
}
const promises = [];
if (clean) {
fs.rmSync("./dist", { recursive: true, force: true });
} else if (browser) {
build("browser", "packages/browser/src/index.ts");
} else {
for (const [name, entry] of Object.entries(config)) {
promises.push(build(name, entry));
}
const coreExtensions = fs.readdirSync("./packages/core-extensions/src");
for (const ext of coreExtensions) {
for (const fileExt of ["ts", "tsx"]) {
for (const type of ["index", "node", "host"]) {
if (fs.existsSync(`./packages/core-extensions/src/${ext}/${type}.${fileExt}`)) {
promises.push(buildExt(ext, type, fileExt));
}
}
}
}
}
await Promise.all(promises);