Skip to content

Instantly share code, notes, and snippets.

@LowByteFox
Created April 7, 2023 08:46
Show Gist options
  • Save LowByteFox/20ea9ee3da8c2b0d66e60e3ccedaf1f9 to your computer and use it in GitHub Desktop.
Save LowByteFox/20ea9ee3da8c2b0d66e60e3ccedaf1f9 to your computer and use it in GitHub Desktop.
Bun bun bundling files and being node_modulesless
// code extracted from https://github.com/Fire-The-Fox/buchta/blob/master/src/bundler.ts
// Show it some ❤️ by giving it a ⭐
import { spawnSync } from "bun";
import { existsSync, readFileSync, unlinkSync, writeFileSync } from "fs";
import { dirname, relative } from "path";
// run as bun ./index.ts a.ts b.ts ...
const a = process.argv
a.shift()
a.shift()
spawnSync(["bun", "bun", ...a]);
if (!existsSync(`${process.cwd()}/node_modules.bun`)) process.exit(1);
const { stdout } = spawnSync(["bun", `${process.cwd()}/node_modules.bun`]);
const bundleCode = stdout!.toString();
spawnSync(["bun", "build", "--outdir", ".out", ...a]);
process.chdir(".out");
for (const el of a) {
const content = readFileSync(el, {encoding: "utf-8"});
const noLocalhost = content.replaceAll(/http.+?(?=\/n)/g, "");
const newContent = noLocalhost.replaceAll(/\/node_modules.+bun/g, relative(dirname(process.cwd() + "/" + el), process.cwd() + "/bundle.js"));
writeFileSync(el, newContent);
}
writeFileSync("./bundle.js", bundleCode);
process.chdir("..");
unlinkSync("./node_modules.bun");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment