[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:
parent
6ef8c97b86
commit
8354b3806c
5 changed files with 86 additions and 79 deletions
57
build.mjs
57
build.mjs
|
@ -1,40 +1,51 @@
|
||||||
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) {
|
||||||
console.error("Build failed...", e);
|
console.error("Build failed...", e);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
12
package.json
12
package.json
|
@ -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": {
|
||||||
|
|
|
@ -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();
|
|
|
@ -5,26 +5,23 @@ const MMKVManager = RN.NativeModules.MMKVManager as MMKVManager;
|
||||||
const DCDFileManager = RN.NativeModules.DCDFileManager as DCDFileManager;
|
const DCDFileManager = RN.NativeModules.DCDFileManager as DCDFileManager;
|
||||||
|
|
||||||
const filePathFixer: (file: string) => string = RN.Platform.select({
|
const filePathFixer: (file: string) => string = RN.Platform.select({
|
||||||
default: (f) => f,
|
default: (f) => f,
|
||||||
ios: (f) => `Documents/${f}`,
|
ios: (f) => `Documents/${f}`,
|
||||||
});
|
});
|
||||||
|
|
||||||
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"),
|
};
|
||||||
};
|
};
|
||||||
};
|
|
|
@ -21,34 +21,31 @@ 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> {
|
patcher: without(patcher, "unpatchAll"),
|
||||||
return {
|
metro: { ...metro, common: { ...common } },
|
||||||
patcher: without(patcher, "unpatchAll"),
|
constants,
|
||||||
metro: { ...metro, common: { ...common } },
|
utils,
|
||||||
constants,
|
debug: without(debug, "versionHash", "patchLogHook"),
|
||||||
utils,
|
ui: {
|
||||||
debug: without(debug, "versionHash", "patchLogHook"),
|
components,
|
||||||
ui: {
|
toasts,
|
||||||
components,
|
assets,
|
||||||
toasts,
|
...color,
|
||||||
assets,
|
},
|
||||||
...color,
|
plugins: without(plugins, "initPlugins"),
|
||||||
},
|
commands: without(commands, "patchCommands"),
|
||||||
plugins: without(plugins, "initPlugins"),
|
storage,
|
||||||
commands: without(commands, "patchCommands"),
|
settings,
|
||||||
storage,
|
loader: {
|
||||||
settings,
|
identity: window.__vendetta_loader,
|
||||||
loader: {
|
config: loaderConfig,
|
||||||
identity: window.__vendetta_loader,
|
},
|
||||||
config: loaderConfig,
|
logger,
|
||||||
},
|
version: debug.versionHash,
|
||||||
logger,
|
unload: () => {
|
||||||
version: debug.versionHash,
|
unloads.filter(i => typeof i === "function").forEach(p => p());
|
||||||
unload: () => {
|
// @ts-expect-error explode
|
||||||
unloads.filter(i => typeof i === "function").forEach(p => p());
|
delete window.vendetta;
|
||||||
// @ts-expect-error explode
|
}
|
||||||
delete window.vendetta;
|
});
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue