Add build.ts

This commit is contained in:
redphx 2024-04-24 16:49:35 +07:00
parent 9a76731c69
commit 9446e39eb0
3 changed files with 52 additions and 2 deletions

46
build.ts Normal file
View File

@ -0,0 +1,46 @@
#!/usr/bin/env bun
import { watch, readFile } from "node:fs/promises"
const postProcess = (str: string): string => {
// Unescape unicode charaters
str = unescape((str.replace(/\\u/g, '%u')));
// Replace \x00 to normal character
str = str.replaceAll(/\\x[A-F0-9]{2}/g, (e) => String.fromCharCode(parseInt(e.substring(2), 16)));
// Replace "globalThis." with "var";
str = str.replaceAll('globalThis.', 'var ');
return str;
}
const build = (config?: any={}) => {
const startTime = performance.now();
return 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`);
})
}
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);
}

View File

@ -2,10 +2,14 @@
"name": "better-xcloud",
"module": "src/index.ts",
"type": "module",
"bin": {
"build": "build.ts",
"build-watch": "build.ts --watch"
},
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
}
}
}

View File

@ -26,7 +26,7 @@
"noFallthroughCasesInSwitch": true,
// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedLocals": true,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}