.js .jsx .cjs .mjs .mts .cts .ts .tsx .css .json .jsonc .toml .yaml .yml .txt .wasm .node .html
Importing non-JS files
type import attribute:
Built-in loaders
js — JavaScript
Default for .cjs and .mjs. Parses the file and applies dead-code elimination and tree shaking. Bun does not down-convert syntax.
jsx — JavaScript + JSX
Default for .js and .jsx. Same as js, but JSX syntax is also supported. JSX is transformed to plain JavaScript based on your tsconfig.json compilerOptions.jsx setting.
ts — TypeScript
Default for .ts, .mts, .cts. Strips all TypeScript syntax, then behaves like the js loader. No type checking is performed.
tsx — TypeScript + JSX
Default for .tsx. Transpiles both TypeScript and JSX to vanilla JavaScript.
json — JSON
Default for .json. JSON files can be imported directly and are inlined as JavaScript objects in the bundle.
.json file is used as a bundler entrypoint, it is converted to a .js module that export defaults the parsed object.
jsonc — JSON with Comments
Default for .jsonc. Strips comments and trailing commas, then behaves like the json loader. Bun automatically uses this loader for tsconfig.json, jsconfig.json, package.json, and bun.lock.
toml — TOML
Default for .toml. Parses the file using Bun’s native TOML parser and inlines the result as a JavaScript object.
yaml — YAML
Default for .yaml and .yml. Parses the file using Bun’s native YAML parser and inlines the result as a JavaScript object.
text — Plain text
Default for .txt. File contents are read and inlined as a string.
css — CSS
Default for .css. CSS files can be imported from JavaScript. The bundler processes @import statements and url() references, and all imported CSS is combined into a single .css file in outdir.
html — HTML
Default for .html. Processes an HTML file and bundles any referenced assets.
<script src="...">— bundled and hashed<link rel="stylesheet" href="...">— bundled and hashed<img src="...">— hashed- External URLs (
http://,https://) — preserved as-is
The
html loader behaves differently depending on context. With bun build ./index.html it produces a static site. When imported from server code and used with Bun.serve(), it enables the fullstack dev server with HMR. See Fullstack Development for more.file — File (assets)
Default for all unrecognized extensions. The file is copied to outdir as-is and the import is replaced with the relative path to the copied file.
publicPath configured:
naming.asset (default: [name]-[hash].[ext]).
sqlite — SQLite database
Requires with { type: "sqlite" }. Loads the database using bun:sqlite.
embed: "true":
napi — Native addon
Default for .node. Native addons can be imported at runtime:
In the bundler,
.node files are treated as assets (copied using the file loader).Custom loaders in Bun.build()
Map additional file extensions to built-in loaders using the loader option:
- JavaScript API
- CLI
Loader name reference
| Loader | Description |
|---|---|
js | JavaScript (no JSX) |
jsx | JavaScript + JSX |
ts | TypeScript |
tsx | TypeScript + JSX |
json | JSON → inlined object |
jsonc | JSON with comments → inlined object |
toml | TOML → inlined object |
yaml | YAML → inlined object |
text | Text file → inlined string |
css | CSS bundler |
html | HTML bundler |
file | Asset — copied to outdir, import becomes path |
dataurl | Asset — inlined as data: URL |
sqlite | SQLite database (via bun:sqlite) |
napi | Native Node addon (.node) |
wasm | WebAssembly module |