[Build] SWC-ify, allow classes and async arrow functions (#24)

* [Build] SWC-ify, allow classes and async arrow functions

* [Build] The SWC-ening is upon us.

* [Build] Tidy

* [Global] Make use of our newfound powers

* [Build] Call bundle.close when done

---------

Co-authored-by: Beef <beefers@riseup.net>
This commit is contained in:
redstonekasi 2023-02-26 01:34:16 +01:00 committed by GitHub
parent 6ef8c97b86
commit 8354b3806c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 86 additions and 79 deletions

View file

@ -1,37 +1,48 @@
import { build } from "esbuild";
import { promisify } from "util"; import { promisify } from "util";
import { exec as _exec } from "child_process"; import { exec as _exec } from "child_process";
import { replace } from "esbuild-plugin-replace";
import alias from "esbuild-plugin-alias";
import esg from "esbuild-plugin-external-global";
import fs from "fs/promises"; import fs from "fs/promises";
import path from "path";
const exec = promisify(_exec);
const tsconfig = JSON.parse(await fs.readFile("./tsconfig.json")); import { rollup } from "rollup";
const aliases = Object.fromEntries(Object.entries(tsconfig.compilerOptions.paths).map(([alias, [target]]) => [alias, path.resolve(target)])); import { swc } from "rollup-plugin-swc3";
import { typescriptPaths } from "rollup-plugin-typescript-paths";
import esbuild from "rollup-plugin-esbuild";
import replace from "@rollup/plugin-replace";
import nodeResolve from "@rollup/plugin-node-resolve";
const exec = promisify(_exec);
const commit = (await exec("git rev-parse HEAD")).stdout.trim().substring(0, 7) || "custom"; const commit = (await exec("git rev-parse HEAD")).stdout.trim().substring(0, 7) || "custom";
try { try {
await build({ const bundle = await rollup({
entryPoints: ["./src/index.ts"], input: "src/index.ts",
outfile: "./dist/vendetta.js", onwarn: () => {},
minify: true,
bundle: true,
format: "iife",
target: "esnext",
plugins: [ plugins: [
alias(aliases),
esg.externalGlobalPlugin({
"react": "window.React",
}),
replace({ replace({
"__vendettaVersion": commit, __vendettaVersion: commit,
}) preventAssignment: true,
}),
typescriptPaths(),
nodeResolve({ extensions: [".tsx", ".ts", ".jsx", ".js", ".json"] }),
swc({
env: {
targets: "defaults",
include: [
"transform-classes",
"transform-arrow-functions",
],
},
}),
esbuild({ minify: true }),
], ],
legalComments: "none",
}); });
await bundle.write({
format: "iife",
file: "dist/vendetta.js",
compact: true,
});
await bundle.close();
await fs.appendFile("./dist/vendetta.js", "//# sourceURL=Vendetta"); await fs.appendFile("./dist/vendetta.js", "//# sourceURL=Vendetta");
console.log("Build successful!"); console.log("Build successful!");
} catch (e) { } catch (e) {

View file

@ -15,13 +15,17 @@
"license": "BSD-3-Clause", "license": "BSD-3-Clause",
"devDependencies": { "devDependencies": {
"@react-native-clipboard/clipboard": "1.10.0", "@react-native-clipboard/clipboard": "1.10.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-replace": "^5.0.2",
"@swc/core": "^1.3.35",
"@types/react": "18.0.27", "@types/react": "18.0.27",
"@types/react-native": "0.70.6", "@types/react-native": "0.70.6",
"esbuild": "^0.17.5", "esbuild": "^0.17.10",
"esbuild-plugin-alias": "^0.2.1",
"esbuild-plugin-external-global": "^1.0.1",
"esbuild-plugin-replace": "^1.3.0",
"moment": "2.22.2", "moment": "2.22.2",
"rollup": "^3.17.1",
"rollup-plugin-esbuild": "^5.0.0",
"rollup-plugin-swc3": "^0.8.0",
"rollup-plugin-typescript-paths": "^1.4.0",
"typescript": "^4.9.5" "typescript": "^4.9.5"
}, },
"dependencies": { "dependencies": {

View file

@ -10,7 +10,7 @@ import logger from "@lib/logger";
// This logs in the native logging implementation, e.g. logcat // This logs in the native logging implementation, e.g. logcat
console.log("Hello from Vendetta!"); console.log("Hello from Vendetta!");
async function init() { (async () => {
try { try {
// Load everything in parallel // Load everything in parallel
const unloads = await Promise.all([ const unloads = await Promise.all([
@ -32,6 +32,4 @@ async function init() {
} catch (e: any) { } catch (e: any) {
alert(`Vendetta failed to initialize... ${e.stack || e.toString()}`); alert(`Vendetta failed to initialize... ${e.stack || e.toString()}`);
} }
}; })();
init();

View file

@ -10,20 +10,17 @@ const filePathFixer: (file: string) => string = RN.Platform.select({
}); });
export const createMMKVBackend = (store: string): StorageBackend => ({ export const createMMKVBackend = (store: string): StorageBackend => ({
get: async function() { get: async () => JSON.parse((await MMKVManager.getItem(store)) ?? "{}"),
return JSON.parse((await MMKVManager.getItem(store)) ?? "{}");
},
set: (data) => MMKVManager.setItem(store, JSON.stringify(data)), set: (data) => MMKVManager.setItem(store, JSON.stringify(data)),
}); });
export const createFileBackend = (file: string): StorageBackend => { export const createFileBackend = (file: string): StorageBackend => {
let created: boolean; let created: boolean;
return { return {
get: async function() { get: async () => {
const path = `${DCDFileManager.getConstants().DocumentsDirPath}/${file}`; const path = `${DCDFileManager.getConstants().DocumentsDirPath}/${file}`;
if (!created && !await DCDFileManager.fileExists(path)) if (!created && !(await DCDFileManager.fileExists(path))) return (created = true), DCDFileManager.writeFile("documents", filePathFixer(file), "{}", "utf8");
return (created = true, DCDFileManager.writeFile("documents", filePathFixer(file), "{}", "utf8")); return JSON.parse(await DCDFileManager.readFile(path, "utf8"));
return JSON.parse((await DCDFileManager.readFile(path, "utf8")));
}, },
set: (data) => void DCDFileManager.writeFile("documents", filePathFixer(file), JSON.stringify(data), "utf8"), set: (data) => void DCDFileManager.writeFile("documents", filePathFixer(file), JSON.stringify(data), "utf8"),
}; };

View file

@ -21,9 +21,7 @@ function without<T extends Record<string, any>>(object: T, ...keys: string[]) {
return cloned; return cloned;
} }
// I wish Hermes let me do async arrow functions export default async (unloads: any[]): Promise<VendettaObject> => ({
export default async function windowObject(unloads: any[]): Promise<VendettaObject> {
return {
patcher: without(patcher, "unpatchAll"), patcher: without(patcher, "unpatchAll"),
metro: { ...metro, common: { ...common } }, metro: { ...metro, common: { ...common } },
constants, constants,
@ -50,5 +48,4 @@ export default async function windowObject(unloads: any[]): Promise<VendettaObje
// @ts-expect-error explode // @ts-expect-error explode
delete window.vendetta; delete window.vendetta;
} }
} });
}