Bun's bundler supports a variety of file types out of the box:
JavaScript and TypeScript
- `.js` and `.jsx` files are treated as JavaScript
- `.ts` and `.tsx` files are automatically transpiled to JavaScript[1][5]
JSON and TOML
- `.json` and `.toml` files are parsed at compile time and inlined as JSON[1][4]
Plain Text
- `.txt` files are inlined as strings[1]
Assets
Everything else is treated as an asset. Assets are copied into the `outdir` directory as-is, and the import is replaced with a relative path or URL to the file, for example:
js
import logo from "./images/logo.png";
console.log(logo);
js
var logo = "./images/logo.png";
console.log(logo);
[1]The bundler supports the following targets:
- `"browser"` (default): TypeScript and JSX are automatically transpiled to JavaScript
- `"bun"`: Bun and Node.js APIs are supported and left untouched
- `"node"`: Currently identical to `"bun"`, but may add polyfills in the future[1]
Bun's bundler is designed to be fast, with a unified plugin API that works with both the bundler and runtime. It can also generate standalone executables from TypeScript and JavaScript scripts using the `--compile` flag[1][4].
Citations:[1] https://bun.sh/blog/bun-bundler
[2] https://stackoverflow.com/questions/77106237/how-to-genereate-type-definitions-with-bun-build-bundle
[3] https://stackoverflow.com/questions/78703631/how-to-set-up-bun-to-compile-and-bundle-the-typescript-files-in-a-directory
[4] https://www.infoworld.com/article/2338610/bundling-with-the-bunjs-bundler.html
[5] https://bun.sh/docs/bundler
[6] https://github.com/softwarearchitect817/Bun-fast-JavaScript-runtime
[7] https://www.builder.io/blog/bun-vs-node-js
[8] https://shaneosullivan.wordpress.com/2023/05/17/using-bun-js-as-a-bundler/