diff --git a/build.ts b/build.ts index 77598d2..0442750 100644 --- a/build.ts +++ b/build.ts @@ -1,5 +1,12 @@ #!/usr/bin/env bun -import { watch, readFile } from "node:fs/promises" +import { watch, readFile } from "node:fs/promises"; + +enum BuildTarget { + ALL = 'all', + ANDROID_APP = 'android-app', + MOBILE = 'mobile', + WEBOS = 'webos', +} const postProcess = (str: string): string => { // Unescape unicode charaters @@ -13,34 +20,45 @@ const postProcess = (str: string): string => { return str; } -const build = (config?: any={}) => { +const build = async (target: BuildTarget, config?: any={}) => { + console.log('--- Building:', target); const startTime = performance.now(); - return Bun.build({ + let outputFileName = 'better-xcloud'; + if (target !== BuildTarget.ALL) { + outputFileName += `.${target}`; + } + outputFileName += '.user.js'; + + let output = await Bun.build({ entrypoints: ['src/index.ts'], outdir: './dist', - }).then(async (output) => { - if (!output.success) { - console.log(output); - process.exit(1); - } - const {path} = output.outputs[0]; - let result = postProcess(await readFile(path, 'utf-8')); - await Bun.write( - path, - await readFile('src/header.txt', 'utf-8') + result - ); - console.log(`done in ${performance.now() - startTime} ms`); - }) + naming: outputFileName, + define: { + 'Bun.env.BUILD_TARGET': JSON.stringify(target), + }, + }); + + if (!output.success) { + console.log(output); + process.exit(1); + } + + const {path} = output.outputs[0]; + let result = postProcess(await readFile(path, 'utf-8')); + const header = await readFile('src/header.txt', 'utf-8'); + await Bun.write(path, header + result); + console.log(`[${target}] done in ${performance.now() - startTime} ms`); } +const buildTargets = [ + BuildTarget.ALL, + BuildTarget.ANDROID_APP, + BuildTarget.MOBILE, + // BuildTarget.WEBOS, +]; + const config = {}; -await build(config); - -if (!process.argv.includes('--watch')) process.exit(0); - -for await (const event of watch('./src', {recursive: true})) { - const {filename} = event; - console.log(filename) - await build(config); +for (const target of buildTargets) { + await build(target, config); } diff --git a/package.json b/package.json index de4a4fe..de00ba3 100644 --- a/package.json +++ b/package.json @@ -3,8 +3,7 @@ "module": "src/index.ts", "type": "module", "bin": { - "build": "build.ts", - "build-watch": "build.ts --watch" + "build": "build.ts" }, "devDependencies": { "@types/bun": "latest" diff --git a/src/build-config.ts b/src/build-config.ts new file mode 100644 index 0000000..07842d1 --- /dev/null +++ b/src/build-config.ts @@ -0,0 +1,8 @@ +const BuildConfig = { + TARGET: Bun.env.BUILD_TARGET, +}; + +export const getBuildConfig = () => { + console.log(BuildConfig); + return BuildConfig; +}; diff --git a/src/index.ts b/src/index.ts index 69825e5..da4d359 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +import { getBuildConfig } from "./build-config" with { type: "macro" }; import "./utils/global"; import { BxEvent } from "./modules/bx-event"; import { BX_FLAGS } from "./modules/bx-flags";