GoonCord/build.mjs

160 lines
5.6 KiB
JavaScript
Raw Normal View History

2024-02-08 14:52:05 +08:00
// eslint-disable-next-line simple-import-sort/imports
import swc from "@swc/core";
2024-02-08 14:52:05 +08:00
import { execSync } from "child_process";
import esbuild from "esbuild";
2024-03-06 01:57:59 +08:00
import { readFile } from "fs/promises";
2024-02-08 14:52:05 +08:00
import { createServer } from "http";
2023-12-21 14:02:32 +08:00
import { argv } from "process";
2024-02-08 14:52:05 +08:00
const isFlag = (s, l) => argv.slice(2).some(c => c === `-${s}` || c === `--${l}`);
2023-12-21 14:02:32 +08:00
2024-02-08 14:52:05 +08:00
const isRelease = isFlag("r", "release");
const shouldServe = isFlag("s", "serve");
const shouldWatch = isFlag("w", "watch");
const commitHash = execSync("git rev-parse --short HEAD").toString().trim();
2022-10-18 23:04:55 +01:00
try {
2024-02-08 14:52:05 +08:00
const ctx = await esbuild.context({
entryPoints: ["src/entry.js"],
bundle: true,
2024-02-08 14:52:05 +08:00
minify: isRelease,
format: "iife",
target: "esnext",
supported: {
// Hermes does not actually supports const and let, even though it syntactically
// accepts it, but it's treated just like 'var' and causes issues
"const-and-let": false
},
2024-02-09 10:37:36 +08:00
outfile: "dist/bunny.js",
2024-02-08 14:52:05 +08:00
keepNames: true,
footer: {
2024-02-09 10:37:36 +08:00
js: "//# sourceURL=bunny"
2024-02-08 14:52:05 +08:00
},
loader: { ".png": "dataurl" },
legalComments: "none",
2024-02-09 11:02:45 +08:00
alias: {
2024-03-06 01:57:59 +08:00
"@lib/*": "src/lib/*",
"@core/*": "src/core/*",
"@metro/*": "src/lib/metro/*",
"@ui/*": "src/lib/ui/*",
"@types": "src/utils/types.ts",
2024-02-08 14:52:05 +08:00
},
plugins: [
{
name: "runtimeGlobalAlias",
setup: async build => {
const globalMap = {
"react": "globalThis.React",
"react-native": "globalThis.ReactNative"
};
Object.keys(globalMap).forEach(key => {
const filter = new RegExp(`^${key}$`);
build.onResolve({ filter }, args => ({
namespace: "glob-" + key, path: args.path
}));
build.onLoad({ filter, namespace: "glob-" + key }, () => ({
contents: `Object.defineProperty(module, 'exports', { get: () => ${globalMap[key]} })`,
resolveDir: "src",
}));
});
}
},
2024-02-09 11:02:45 +08:00
{
name: "buildLog",
setup: async build => {
build.onStart(() => {
console.clear();
2024-03-06 01:57:59 +08:00
console.log(`Building with commit hash "${commitHash}", isRelease="${isRelease}"`);
2024-02-09 11:02:45 +08:00
});
build.onEnd(result => {
const { outfile } = build.initialOptions;
console.log(`Built with ${result.errors?.length} errors!`);
});
}
},
{
name: "swc",
2024-02-08 14:52:05 +08:00
setup(build) {
2024-02-09 11:02:45 +08:00
let timeString = Number(new Date).toString(36);
2024-03-06 01:57:59 +08:00
2024-02-09 11:02:45 +08:00
build.onStart(() => {
timeString = Number(new Date).toString(36);
console.log(`swc plugin: time-string="${timeString}"`);
2024-03-06 01:57:59 +08:00
});
2024-02-27 17:22:23 +08:00
build.onLoad({ filter: /\.[jt]sx?$/ }, async args => {
const result = await swc.transformFile(args.path, {
jsc: {
externalHelpers: true,
2024-02-09 11:02:45 +08:00
transform: {
constModules: {
2024-02-09 11:02:45 +08:00
globals: {
"bunny-build": {
version: `"${isRelease ? commitHash : timeString}"`
2024-02-09 11:02:45 +08:00
}
}
}
},
},
env: {
targets: "defaults",
include: [
"transform-classes",
"transform-arrow-functions",
"transform-block-scoping",
"transform-class-properties"
],
exclude: [
"transform-parameters"
2024-02-08 14:52:05 +08:00
]
},
});
2024-02-08 14:52:05 +08:00
return { contents: result.code };
});
2024-02-08 14:52:05 +08:00
}
}
2024-02-09 11:02:45 +08:00
],
external: ["commit-hash"]
2022-10-18 23:04:55 +01:00
});
2024-02-08 14:52:05 +08:00
if (shouldWatch) {
await ctx.watch();
console.log("Watching...");
}
if (shouldServe) {
await ctx.rebuild();
const server = createServer(async (req, res) => {
try {
2024-02-09 10:37:36 +08:00
if (req.url === "/vendetta.js" || req.url === "/pyoncord.js" || req.url === "/bunny.js") {
2024-02-08 14:52:05 +08:00
await ctx.rebuild();
res.writeHead(200);
2024-02-09 10:37:36 +08:00
res.end(await readFile("./dist/bunny.js"), "utf-8");
2024-02-08 14:52:05 +08:00
} else {
res.writeHead(404);
res.end();
}
} catch (error) {
res.writeHead(500);
res.end();
}
}).listen(4040);
// @ts-ignore
console.log(`Serving on port ${server.address()?.port}, CTRL+C to stop`);
}
if (!shouldServe && !shouldWatch) {
ctx.rebuild();
ctx.dispose();
}
2022-10-18 23:04:55 +01:00
} catch (e) {
console.error("Build failed...", e);
process.exit(1);
2023-03-31 16:46:55 +02:00
}