From 8354b3806cd7e73ac7bbf676904be12f680b941c Mon Sep 17 00:00:00 2001 From: redstonekasi Date: Sun, 26 Feb 2023 01:34:16 +0100 Subject: [PATCH] [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 --- build.mjs | 57 ++++++++++++++++++++--------------- package.json | 12 +++++--- src/index.ts | 6 ++-- src/lib/storage/backends.ts | 31 +++++++++---------- src/lib/windowObject.ts | 59 ++++++++++++++++++------------------- 5 files changed, 86 insertions(+), 79 deletions(-) diff --git a/build.mjs b/build.mjs index 7ac954d..102e9cd 100644 --- a/build.mjs +++ b/build.mjs @@ -1,40 +1,51 @@ -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) { console.error("Build failed...", e); process.exit(1); -} \ No newline at end of file +} diff --git a/package.json b/package.json index 9fc0d54..6cb7f00 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/index.ts b/src/index.ts index 48d1242..09892eb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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(); \ No newline at end of file +})(); \ No newline at end of file diff --git a/src/lib/storage/backends.ts b/src/lib/storage/backends.ts index 9060709..0129258 100644 --- a/src/lib/storage/backends.ts +++ b/src/lib/storage/backends.ts @@ -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"), + }; +}; \ No newline at end of file diff --git a/src/lib/windowObject.ts b/src/lib/windowObject.ts index 6fcdbe2..1facb80 100644 --- a/src/lib/windowObject.ts +++ b/src/lib/windowObject.ts @@ -21,34 +21,31 @@ function without>(object: T, ...keys: string[]) { return cloned; } -// I wish Hermes let me do async arrow functions -export default async function windowObject(unloads: any[]): Promise { - 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 => ({ + 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; + } +});