As a general rule: The bundler and runtime both support the same set of file types.
.js .cjs .mjs .mts .cts .ts .tsx .jsx .css .json .jsonc .toml .yaml .yml .txt .wasm .node .html .sh
Bun uses the file extension to determine which built-in loader should be used to parse the file. Each loader has a name, such as js, tsx, or json. These names are used when building custom loader plugins that extend Bun.
You can explicitly specify which loader to use using the 'type' import attribute.
Built-in loaders
js
The JavaScript loader. Used by default for .cjs and .mjs files.
Parses the code and applies a set of default transformations like dead code elimination and tree shaking. Note that Bun does not currently attempt to down-convert syntax.
jsx
The JavaScript + JSX loader. Used by default for .js and .jsx files.
Identical to the js loader, but supports JSX syntax. By default, JSX is down-converted to vanilla JavaScript; the specifics of how JSX is transformed depends on the jsx* compiler options in your tsconfig.json. Refer to the TypeScript docs on JSX for details.
ts
The TypeScript loader. Used by default for .ts, .mts, and .cts files.
Strips all TypeScript syntax, then behaves identically to the js loader. Bun does not perform typechecking.
tsx
The TypeScript + JSX loader. Used by default for .tsx files.
Transpiles TypeScript and JSX to vanilla JavaScript.
json
The JSON loader. Used by default for .json files.
JSON files can be directly imported.
.json file is passed as an entrypoint to the bundler, it will be converted to a .js module that export defaults the parsed object.
jsonc
The JSON with comments (JSONC) loader. Used by default for .jsonc files.
JSON with comments (JSONC) files can be directly imported. Bun will parse and strip out the comments and trailing commas.
json loader.
Bun automatically uses the
jsonc loader for tsconfig.json, jsconfig.json, package.json, and bun.lock files.toml
The TOML loader. Used by default for .toml files.
TOML files can be directly imported. Bun will parse them using its fast native TOML parser.
.toml file is passed as an entrypoint to the bundler, it will be converted to a .js module that export defaults the parsed object.
yaml
The YAML loader. Used by default for .yaml and .yml files.
YAML files can be directly imported. Bun will parse them using its fast native YAML parser.
.yaml or .yml file is passed as an entrypoint to the bundler, it will be converted to a .js module that export defaults the parsed object.
text
The text loader. Used by default for .txt files.
The contents of the text file are read and inlined into the bundle as a string. Text files can be directly imported. The file contents are read and returned as a string.
.txt file is passed as an entrypoint, it will be converted to a .js module that export defaults the file contents.
napi
The native addon loader. Used by default for .node files.
At runtime, native addons can be directly imported.
In the bundler,
.node files are treated with the file loader.sqlite
The SQLite loader. Requires the with { "type": "sqlite" } import attribute.
SQLite database files can be directly imported in the runtime and bundler. The database will be loaded using bun:sqlite.
"embed" attribute:
When using standalone executables, the database is embedded into the standalone executable.Otherwise, embedded databases are copied to the output directory
outdir with a hashed filename.html
The HTML loader. Used by default for .html files.
The html loader processes HTML files and bundles all referenced assets. It will:
- Bundle and hash referenced JavaScript files (
<script src="...">) - Bundle and hash referenced CSS files (
<link rel="stylesheet" href="...">) - Hash referenced images (
<img src="...">) - Preserve external URLs (those starting with
http://orhttps://by default)
lol-html to extract script and link tags as entrypoints and other assets as external assets.
List of supported HTML selectors
List of supported HTML selectors
The following selectors are currently supported:
audio[src]iframe[src]img[src]img[srcset]link:not([rel~='stylesheet']):not([rel~='modulepreload']):not([rel~='manifest']):not([rel~='icon']):not([rel~='apple-touch-icon'])[href]link[as='font'][href], link[type^='font/'][href]link[as='image'][href]link[as='style'][href]link[as='video'][href], link[as='audio'][href]link[as='worker'][href]link[rel='icon'][href], link[rel='apple-touch-icon'][href]link[rel='manifest'][href]link[rel='stylesheet'][href]script[src]source[src]source[srcset]video[poster]video[src]
HTML loader behavior in different scenariosThe behavior of the
html loader depends on how it’s used:- Static build: When running
bun build ./index.html, Bun generates a static site with all assets bundled and hashed. - Runtime: When running
bun run server.tswhereserver.tsimports an HTML file, Bun dynamically bundles assets during development with features like hot module replacement. - Full-stack build: When running
bun build --target=bun server.tswhereserver.tsimports an HTML file, the import resolves to a manifest object for efficiently serving pre-bundled assets in production withBun.serve.
css
The CSS loader. Used by default for .css files.
CSS files can be directly imported. The bundler will parse and bundle CSS files, supporting @import statements and url() references.
.css file that is written to the output directory.
sh
The Bun Shell loader. Used by default for .sh files.
This loader is used to parse Bun Shell scripts. It’s only supported when starting Bun itself, so it’s not available in the bundler or runtime.
file
The file loader. Used by default for all unrecognized file types.
This loader resolves an import to a path or URL to the imported file. It’s commonly used to reference media or font assets.
logo.svg file exists and converts it to an absolute path to the file on disk.
outdir, and the import resolves to a relative path pointing to the copied file.
publicPath, the import will be prefixed with that value, creating an absolute path or URL.
| Public path | Resolved import |
|---|---|
"" (default) | /logo.svg |
"/assets" | /assets/logo.svg |
"https://cdn.example.com/" | https://cdn.example.com/logo.svg |
The location and filename of the copied file is determined by the value of
naming.asset.This loader copies the file as-is into outdir, with the copied filename determined by naming.asset.Custom loaders
You can define custom loaders using theloader option in the bundler API: