Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How does Bun inline parsed TOML into a JavaScript object


How does Bun inline parsed TOML into a JavaScript object


Bun's TOML Loader

Bun has a built-in TOML loader that can directly import `.toml` files. It uses a fast native TOML parser to parse the file contents into a JavaScript object[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 to the bundler, it will be converted to a `.js` module that `export default`s the parsed object[1].

Bun uses the `js-toml` library under the hood, which is a TOML parser for JavaScript and TypeScript that is fully compatible with the TOML v1.0.0 specification[2].

So in summary, Bun makes it easy to inline TOML configuration files into your JavaScript bundles by automatically parsing them into objects using its built-in TOML loader. This allows you to directly import and use TOML data in your code.

Citations:
[1] https://bun.sh/docs/bundler/loaders
[2] https://github.com/sunnyadn/js-toml
[3] https://docs.deno.com/examples/parsing-serializing-toml/
[4] https://github.com/softwarearchitect817/Bun-fast-JavaScript-runtime
[5] https://www.infoworld.com/article/2338610/bundling-with-the-bunjs-bundler.html
[6] https://thisdavej.com/using-toml-config-files-in-node-js-applications/
[7] https://bun.sh/blog/bun-bundler
[8] https://stackoverflow.com/questions/61351418/how-to-call-and-reuse-function-from-file