[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 { 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 path from "path";
const exec = promisify(_exec);
const tsconfig = JSON.parse(await fs.readFile("./tsconfig.json"));
const aliases = Object.fromEntries(Object.entries(tsconfig.compilerOptions.paths).map(([alias, [target]]) => [alias, path.resolve(target)]));
import { rollup } from "rollup";
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";
try {
await build({
entryPoints: ["./src/index.ts"],
outfile: "./dist/vendetta.js",
minify: true,
bundle: true,
format: "iife",
target: "esnext",
const bundle = await rollup({
input: "src/index.ts",
onwarn: () => {},
plugins: [
alias(aliases),
esg.externalGlobalPlugin({
"react": "window.React",
}),
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");
console.log("Build successful!");
} catch (e) {

View file

@ -15,13 +15,17 @@
"license": "BSD-3-Clause",
"devDependencies": {
"@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-native": "0.70.6",
"esbuild": "^0.17.5",
"esbuild-plugin-alias": "^0.2.1",
"esbuild-plugin-external-global": "^1.0.1",
"esbuild-plugin-replace": "^1.3.0",
"esbuild": "^0.17.10",
"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"
},
"dependencies": {

View file

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

View file

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

View file

@ -21,34 +21,31 @@ function without<T extends Record<string, any>>(object: T, ...keys: string[]) {
return cloned;
}
// I wish Hermes let me do async arrow functions
export default async function windowObject(unloads: any[]): Promise<VendettaObject> {
return {
patcher: without(patcher, "unpatchAll"),
metro: { ...metro, common: { ...common } },
constants,
utils,
debug: without(debug, "versionHash", "patchLogHook"),
ui: {
components,
toasts,
assets,
...color,
},
plugins: without(plugins, "initPlugins"),
commands: without(commands, "patchCommands"),
storage,
settings,
loader: {
identity: window.__vendetta_loader,
config: loaderConfig,
},
logger,
version: debug.versionHash,
unload: () => {
unloads.filter(i => typeof i === "function").forEach(p => p());
// @ts-expect-error explode
delete window.vendetta;
}
export default async (unloads: any[]): Promise<VendettaObject> => ({
patcher: without(patcher, "unpatchAll"),
metro: { ...metro, common: { ...common } },
constants,
utils,
debug: without(debug, "versionHash", "patchLogHook"),
ui: {
components,
toasts,
assets,
...color,
},
plugins: without(plugins, "initPlugins"),
commands: without(commands, "patchCommands"),
storage,
settings,
loader: {
identity: window.__vendetta_loader,
config: loaderConfig,
},
logger,
version: debug.versionHash,
unload: () => {
unloads.filter(i => typeof i === "function").forEach(p => p());
// @ts-expect-error explode
delete window.vendetta;
}
}
});