From 18f21feaaea0a4330e2b098c549b2a516cc8d352 Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Mon, 21 Jul 2025 21:03:50 +0700 Subject: [PATCH] feat: compile to bytecode --- scripts/build.mjs | 20 +++++++++++++++++++- scripts/serve.mjs | 4 ++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/scripts/build.mjs b/scripts/build.mjs index 2d593da..37676a6 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -1,7 +1,7 @@ // @ts-nocheck /* eslint-disable no-restricted-syntax */ import swc from "@swc/core"; -import { execSync } from "child_process"; +import { execFileSync, execSync } from "child_process"; import crypto from "crypto"; import { build } from "esbuild"; import globalPlugin from "esbuild-plugin-globals"; @@ -10,6 +10,7 @@ import { fileURLToPath } from "url"; import yargs from "yargs-parser"; import { printBuildSuccess } from "./util.mjs"; +import { readFile } from "fs/promises"; /** @type string[] */ const metroDeps = await (async () => { @@ -121,6 +122,23 @@ export async function buildBundle(overrideConfig = {}) { const initialStartTime = performance.now(); await build({ ...config, ...overrideConfig }); + const paths = { + win32: 'win64-bin/hermesc.exe', + darwin: 'osx-bin/hermesc', + linux: 'linux64-bin/hermesc', + } + + if (!(process.platform in paths)) + throw new Error(`Unsupported platform: ${process.platform}`) + + const sdksDir = './node_modules/react-native/sdks' + const binPath = `${sdksDir}/hermesc/${paths[process.platform]}` + + execFileSync(binPath, ['-finline', '-strict', '-O', '-g1', '-reuse-prop-cache', '-optimized-eval', '-emit-binary', '-Wno-undefined-variable', '-out', config.outfile], { + input: await readFile(config.outfile), + stdio: 'pipe' + }); + return { config, context, diff --git a/scripts/serve.mjs b/scripts/serve.mjs index 2b0a893..7760f4c 100644 --- a/scripts/serve.mjs +++ b/scripts/serve.mjs @@ -27,8 +27,8 @@ export function serve(options) { timeTook ); - res.writeHead(200, { "Content-Type": "application/javascript" }); - res.end(await readFile(config.outfile, "utf-8")); + res.writeHead(200); + res.end(await readFile(config.outfile)); } catch { res.writeHead(500); res.end();