* Restructure * Update nav * Reorg * Reshuffle ecosystem pages * Split up runtime/runtime * Back to runtime/index * Fix issue * Split up runtime/index * Add Writing Tests page * Prettier matcher table * More updates
3.5 KiB
TypeScript
Bun natively supports TypeScript out of the box. All files are transpiled on the fly by Bun's fast native transpiler before being executed. Similar to other build tools, Bun does not perform typechecking; it simply removes type annotations from the file.
$ bun index.js
$ bun index.jsx
$ bun index.ts
$ bun index.tsx
Some aspects of Bun's runtime behavior are affected by the contents of your tsconfig.json file. Refer to Runtime > TypeScript page for details.
JSX
Bun supports .jsx and .tsx files out of the box. Bun's internal transpiler converts JSX syntax into vanilla JavaScript before execution.
function Component(props: {message: string}) {
return (
<body>
<h1 style={{color: 'red'}}>{props.message}</h1>
</body>
);
}
console.log(<Component message="Hello world!" />);
Bun implements special logging for JSX to make debugging easier.
$ bun run react.tsx
<Component message="Hello world!" />
JSON and TOML
JSON and TOML files can be directly imported from a source file. The contents will be loaded and returned as a JavaScript object.
import pkg from "./package.json";
import data from "./data.toml";
WASM
As of v0.5.2, experimental support exists for WASI, the WebAssembly System Interface. To run a .wasm binary with Bun:
$ bun ./my-wasm-app.wasm
# if the filename doesn't end with ".wasm"
$ bun run ./my-wasm-app.whatever
{% callout %}
Note — WASI support is based on wasi-js. Currently, it only supports WASI binaries that use the wasi_snapshot_preview1 or wasi_unstable APIs. Bun's implementation is not fully optimized for performance; this will become more of a priority as WASM grows in popularity.
{% /callout %}
Unknown file types
By default, when Bun encounters an import with an unsupported file extension, it returns an absolute path to the location on disk.
import file from "./movie.mp4";
file;
// /path/to/movie.mp4
Note: This behavior only applies when executing a file with bun run! When using Bun's bundler, the behavior may be different depending on your bundler configuration.
Custom loaders
Support for additional file types can be implemented with plugins. Refer to Runtime > Plugins for full documentation.