Support build different targets

This commit is contained in:
redphx 2024-04-25 17:26:47 +07:00
parent 02bd375fc8
commit 9a18d1697e
4 changed files with 52 additions and 26 deletions

View File

@ -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);
}

View File

@ -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"

8
src/build-config.ts Normal file
View File

@ -0,0 +1,8 @@
const BuildConfig = {
TARGET: Bun.env.BUILD_TARGET,
};
export const getBuildConfig = () => {
console.log(BuildConfig);
return BuildConfig;
};

View File

@ -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";