Skip to content

Commit

Permalink
feat(scripts/build): compile to HBC
Browse files Browse the repository at this point in the history
  • Loading branch information
PalmDevs committed Jan 5, 2025
1 parent 18824b3 commit da441a7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@revenge-mod/shared": "workspace:*",
"@revenge-mod/ui": "workspace:*",
"@revenge-mod/utils": "workspace:*",
"@unbound-mod/hermesc": "1.0.3",
"fflate": "^0.8.2",
"valibot": "^1.0.0-beta.9"
},
Expand Down
46 changes: 28 additions & 18 deletions scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { build } from 'esbuild'
import pluginGlobals from 'esbuild-plugin-globals'
import yargs from 'yargs-parser'
import shimmedDeps from '../shims/deps'
import { spawnSync } from 'child_process'

const args = yargs(process.argv.slice(2))
const { release, minify, dev } = args
Expand All @@ -24,7 +25,7 @@ const context = {
const config = {
entryPoints: ['src/index.ts'],
bundle: true,
outfile: 'dist/revenge.js',
outfile: 'dist/js/revenge.js',
format: 'iife',
splitting: false,
external: ['react', 'react-native', 'react/jsx-runtime', '@shopify/flash-list'],
Expand Down Expand Up @@ -53,13 +54,10 @@ const config = {
},
plugins: [
pluginGlobals({
...Object.keys(shimmedDeps).reduce(
(deps, name) => {
deps[name] = `require('!deps-shim!').default[${JSON.stringify(name)}]()`
return deps
},
{},
),
...Object.keys(shimmedDeps).reduce((deps, name) => {
deps[name] = `require('!deps-shim!').default[${JSON.stringify(name)}]()`
return deps
}, {}),
}),
{
name: 'swc',
Expand Down Expand Up @@ -110,15 +108,14 @@ export async function buildBundle(overrideConfig = {}) {
.quiet()
.text()
.then(res => res.trim())

config.define.__REVENGE_HASH__ = `"${context.hash}"`

const initialStartTime = performance.now()
await build({ ...config, ...overrideConfig })

return {
config,
context,
timeTook: performance.now() - initialStartTime,
}
}

Expand All @@ -127,19 +124,32 @@ const pathPassedToNode = resolvePath(process.argv[1])
const isThisFileBeingRunViaCLI = pathToThisFile.includes(pathPassedToNode)

if (isThisFileBeingRunViaCLI) {
const { timeTook } = await buildBundle()

printBuildSuccess(context.hash, release, timeTook)
const initialStartTime = performance.now()

if (minify) {
const { timeTook } = await buildBundle({
if (minify)
await buildBundle({
minifyWhitespace: true,
minifySyntax: true,
outfile: config.outfile.replace(/\.js$/, '.min.js'),
})
else await buildBundle()

printBuildSuccess(context.hash, release, timeTook, true)
}
compileToBytecode(config.outfile)
printBuildSuccess(context.hash, release, performance.now() - initialStartTime, minify)
}

export function compileToBytecode(path) {
const hermesCBin = {
win32: 'hermesc.exe',
darwin: 'darwin/hermesc',
linux: 'linux/hermesc',
}[process.platform]

spawnSync(`./node_modules/@unbound-mod/hermesc/${process.platform}/${hermesCBin}`, [
path,
'-emit-binary',
'-out',
'./dist/revenge.bundle',
])
}

export function printBuildSuccess(hash, release, timeTook, minified = false) {
Expand Down

0 comments on commit da441a7

Please sign in to comment.