Yes, it is possible to customize the TOML parsing process in Bun. While Bun has a built-in TOML loader that uses a native parser, developers can extend or modify this behavior through the use of custom loaders.
Custom Loaders
Bun allows you to define custom loaders for different file types, including TOML. You can specify a custom loader when importing a TOML file by using the `with` attribute:
javascript
import myCustomTOML from './my.config' with { type: "toml" };
This flexibility enables you to implement your own parsing logic or to integrate third-party TOML parsers like `js-toml`, which fully complies with the TOML v1.0.0 specification. This library can be used as follows:
javascript
import { load } from 'js-toml';
const tomlString = `
name = "TOML Example"
[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00
`;
const data = load(tomlString);
console.log(data);
Limitations and Future Improvements
It's worth noting that while Bun's current TOML parser supports many features, there are still some limitations, such as incomplete support for certain data types (like dates and timestamps) and specific syntaxes. There is ongoing discussion in the Bun community regarding enhancements to better align with the full TOML specification, which may include adding support for more complex features and edge cases in future versions [1][2][4].
In summary, you can customize the TOML parsing process in Bun by leveraging custom loaders and integrating third-party libraries if needed. This allows for greater flexibility and adaptability depending on your project's requirements.
Citations:[1] https://bun.sh/docs/bundler/loaders
[2] https://github.com/oven-sh/bun/issues/5703
[3] https://bun.sh/guides/runtime/import-toml
[4] https://github.com/oven-sh/bun/discussions/116
[5] https://github.com/softwarearchitect817/Bun-fast-JavaScript-runtime
[6] https://users.rust-lang.org/t/tom-yet-another-format-preserving-toml-parser/18765
[7] https://betterstack.com/community/guides/scaling-nodejs/introduction-to-bun-for-nodejs-users/
[8] https://github.com/sunnyadn/js-toml