Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How does Bun's bundler handle file types like JSON and TOML


How does Bun's bundler handle file types like JSON and TOML


Bun's Built-in Loaders

Bun uses the file extension to determine which built-in loader should be used to parse the file. Every loader has a name, such as `js`, `tsx`, or `json`. These names are used when building plugins that extend Bun with custom loaders[1].

JSON Loader

JSON files can be directly imported. Bun will parse them with its fast native JSON parser[1].

js
import config from "./config.json";
config.logLevel; // => "debug"

During bundling, the parsed JSON is inlined into the bundle as a JavaScript object[1].

js
var config = {
  logLevel: "debug",
  // ...other fields 
};
config.logLevel;

If a `.json` file is passed as an entrypoint, it will be converted to a `.js` module that `export default`s the parsed object[1].

TOML Loader

TOML files can be directly imported. Bun will parse them with its fast native TOML parser[1].

js
import config from "./bunfig.toml";
config.logLevel; // => "debug"

During bundling, the parsed TOML is inlined into the bundle as a JavaScript object[1].

js
var config = {
  logLevel: "debug",
  // ...other fields
};
config.logLevel;

If a `.toml` file is passed as an entrypoint, it will be converted to a `.js` module that `export default`s the parsed object[1].

In addition to the usual suspects like TypeScript and JSX, the Bun bundler handles more interesting cases like JSON and TOML, which will be inlined into the bundle[2].

Citations:
[1] https://bun.sh/docs/bundler/loaders
[2] https://www.infoworld.com/article/2338610/bundling-with-the-bunjs-bundler.html
[3] https://betterstack.com/community/guides/scaling-nodejs/introduction-to-bun-for-nodejs-users/
[4] https://stackoverflow.com/questions/77106237/how-to-genereate-type-definitions-with-bun-build-bundle
[5] https://github.com/softwarearchitect817/Bun-fast-JavaScript-runtime
[6] https://shaneosullivan.wordpress.com/2023/05/17/using-bun-js-as-a-bundler/
[7] https://www.telerik.com/blogs/getting-started-bun-react-developers
[8] https://github.com/oven-sh/bun/actions/runs/5265074270