diff --git a/build.ts b/build.ts new file mode 100644 index 0000000..77598d2 --- /dev/null +++ b/build.ts @@ -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); +} diff --git a/package.json b/package.json index c41ae1c..de4a4fe 100644 --- a/package.json +++ b/package.json @@ -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" } -} \ No newline at end of file +} diff --git a/tsconfig.json b/tsconfig.json index ab942ee..44e9f57 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -26,7 +26,7 @@ "noFallthroughCasesInSwitch": true, // Some stricter flags (disabled by default) - "noUnusedLocals": false, + "noUnusedLocals": true, "noUnusedParameters": false, "noPropertyAccessFromIndexSignature": false }