Skip to content

Bun 打包器开箱即用实现了一组默认加载器。

根据经验法则:打包器和运行时都开箱即用支持相同的文件类型集。

.js .cjs .mjs .mts .cts .ts .tsx .jsx .css .json .jsonc .toml .yaml .yml .txt .wasm .node .html .sh

Bun 使用文件扩展名来确定应使用哪个内置加载器来解析文件。每个加载器都有一个名称,如 jstsxjson。这些名称在使用扩展 Bun 的自定义加载器构建插件时使用。

您可以使用 'type' 导入属性显式指定要使用的加载器。

ts
import my_toml from "./my_file" with { type: "toml" };
// 或使用动态导入
const { default: my_toml } = await import("./my_file", { with: { type: "toml" } });

内置加载器

js

JavaScript 加载器。 .cjs.mjs 的默认加载器。

解析代码并应用一组默认转换,如死代码消除和 tree shaking。注意 Bun 此时不尝试降级转换语法。


jsx

JavaScript + JSX 加载器。 .js.jsx 的默认加载器。

js 加载器相同,但支持 JSX 语法。默认情况下,JSX 降级转换为纯 JavaScript;如何完成此操作取决于 tsconfig.json 中的 jsx* 编译器选项。有关更多信息,请参阅 TypeScript 关于 JSX 的文档


ts

TypeScript 加载器。 .ts.mts.cts 的默认加载器。

移除所有 TypeScript 语法,然后行为与 js 加载器相同。Bun 不执行类型检查。


tsx

TypeScript + JSX 加载器。 .tsx 的默认加载器。

将 TypeScript 和 JSX 转译为 vanilla JavaScript。


json

JSON 加载器。 .json 的默认加载器。

JSON 文件可以直接导入。

js
import pkg from "./package.json";
pkg.name; // => "my-package"

在打包期间,解析的 JSON 作为 JavaScript 对象内联到包中。

js
const pkg = {
  name: "my-package",
  // ... 其他字段
};

pkg.name;

如果将 .json 文件作为入口点传递给打包器,它将转换为 export default 解析对象的 .js 模块。

json
{
  "name": "John Doe",
  "age": 35,
  "email": "johndoe@example.com"
}
js
export default {
  name: "John Doe",
  age: 35,
  email: "johndoe@example.com",
};

jsonc

带注释的 JSON 加载器。 .jsonc 的默认加载器。

JSONC(带注释的 JSON)文件可以直接导入。Bun 将解析它们,移除注释和尾随逗号。

js
import config from "./config.jsonc";
console.log(config);

在打包期间,解析的 JSONC 作为 JavaScript 对象内联到包中,与 json 加载器相同。

js
var config = {
  option: "value",
};

NOTE

Bun 自动为 `tsconfig.json`、`jsconfig.json`、`package.json` 和 `bun.lock` 文件使用 `jsonc` 加载器。

toml

TOML 加载器。 .toml 的默认加载器。

TOML 文件可以直接导入。Bun 将使用其快速原生 TOML 解析器解析它们。

js
import config from "./bunfig.toml";
config.logLevel; // => "debug"

// 通过导入属性:
// import myCustomTOML from './my.config' with {type: "toml"};

在打包期间,解析的 TOML 作为 JavaScript 对象内联到包中。

js
var config = {
  logLevel: "debug",
  // ...其他字段
};
config.logLevel;

如果将 .toml 文件作为入口点传递,它将转换为 export default 解析对象的 .js 模块。

toml
name = "John Doe"
age = 35
email = "johndoe@example.com"
js
export default {
  name: "John Doe",
  age: 35,
  email: "johndoe@example.com",
};

yaml

YAML 加载器。 .yaml.yml 的默认加载器。

YAML 文件可以直接导入。Bun 将使用其快速原生 YAML 解析器解析它们。

js
import config from "./config.yaml";
console.log(config);

// 通过导入属性:
import data from "./data.txt" with { type: "yaml" };

在打包期间,解析的 YAML 作为 JavaScript 对象内联到包中。

js
var config = {
  name: "my-app",
  version: "1.0.0",
  // ...其他字段
};

如果将 .yaml.yml 文件作为入口点传递,它将转换为 export default 解析对象的 .js 模块。

yaml
name: John Doe
age: 35
email: johndoe@example.com
js
export default {
  name: "John Doe",
  age: 35,
  email: "johndoe@example.com",
};

text

文本加载器。 .txt 的默认加载器。

文本文件的内容被读取并作为字符串内联到包中。文本文件可以直接导入。文件被读取并返回为字符串。

js
import contents from "./file.txt";
console.log(contents); // => "Hello, world!"

// 要将 html 文件导入为文本
// "type" 属性可用于覆盖默认加载器。
import html from "./index.html" with { type: "text" };

当在构建期间引用时,内容作为字符串内联到包中。

js
var contents = `Hello, world!`;
console.log(contents);

如果将 .txt 文件作为入口点传递,它将转换为 export default 文件内容的 .js 模块。

txt
Hello, world!
js
export default "Hello, world!";

napi

原生插件加载器。 .node 的默认加载器。

在运行时,原生插件可以直接导入。

js
import addon from "./addon.node";
console.log(addon);

NOTE

在打包器中,`.node` 文件使用文件加载器处理。

sqlite

SQLite 加载器。 需要 with { "type": "sqlite" } 导入属性。

在运行时和打包器中,SQLite 数据库可以直接导入。这将使用 bun:sqlite 加载数据库。

js
import db from "./my.db" with { type: "sqlite" };

默认情况下,数据库在包外部(以便您可以使用在其他地方加载的数据库),因此磁盘上的数据库文件不会打包到最终输出中。

您可以使用 "embed" 属性更改此行为:

js
// 将数据库嵌入到包中
import db from "./my.db" with { type: "sqlite", embed: "true" };

html

HTML 加载器。 .html 的默认加载器。

html 加载器处理 HTML 文件并打包任何引用的资源。它将:

  • 打包和哈希引用的 JavaScript 文件(<script src="...">
  • 打包和哈希引用的 CSS 文件(<link rel="stylesheet" href="...">
  • 哈希引用的图像(<img src="...">
  • 保留外部 URL(默认情况下,任何以 http://https:// 开头的内容)

例如,给定此 HTML 文件:

html
<!DOCTYPE html>
<html>
  <body>
    <img src="./image.jpg" alt="本地图像" />
    <img src="https://example.com/image.jpg" alt="外部图像" />
    <script type="module" src="./script.js"></script>
  </body>
</html>

它将输出一个新的 HTML 文件,其中包含打包的资源:

html
<!DOCTYPE html>
<html>
  <body>
    <img src="./image-HASHED.jpg" alt="本地图像" />
    <img src="https://example.com/image.jpg" alt="外部图像" />
    <script type="module" src="./output-ALSO-HASHED.js"></script>
  </body>
</html>

在底层,它使用 lol-html 提取脚本和链接标签作为入口点,以及其他资源作为外部。

支持的 HTML 选择器列表

目前,选择器列表为:

  • 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]

NOTE

不同上下文中的 HTML 加载器行为

html 加载器根据使用方式表现不同:

  • 静态构建:运行 bun build ./index.html 时,Bun 生成静态站点,所有资源打包和哈希。
  • 运行时:运行 bun run server.ts(其中 server.ts 导入 HTML 文件)时,Bun 在开发期间即时打包资源,启用热模块替换等功能。
  • 全栈构建:运行 bun build --target=bun server.ts(其中 server.ts 导入 HTML 文件)时,导入解析为清单对象,Bun.serve 使用该对象在生产环境中有效地提供预打包资源。

css

CSS 加载器。 .css 的默认加载器。

CSS 文件可以直接导入。打包器将解析和打包 CSS 文件,处理 @import 语句和 url() 引用。

js
import "./styles.css";

在打包期间,所有导入的 CSS 文件打包到输出目录中的单个 .css 文件中。

css
.my-class {
  background: url("./image.png");
}

sh

Bun Shell 加载器。 .sh 文件的默认加载器。

此加载器用于解析 Bun Shell 脚本。它仅在启动 Bun 本身时支持,因此在打包器或运行时中不可用。

bash
bun run ./script.sh

file

文件加载器。 所有未识别文件类型的默认加载器。

文件加载器将导入解析为导入文件的路径/URL。它通常用于引用媒体或字体资源。

js
// logo.ts
import logo from "./logo.svg";
console.log(logo);

在运行时,Bun 检查 logo.svg 文件是否存在,并将其转换为磁盘上 logo.svg 位置的绝对路径。

bash
bun run logo.ts
# 输出:/path/to/project/logo.svg

在打包器中,事情略有不同。文件按原样复制到 outdir,导入解析为指向复制文件的相对路径。

js
// 输出
var logo = "./logo.svg";
console.log(logo);

如果为 publicPath 指定值,导入将使用该值作为前缀来构造绝对路径/URL。

公共路径解析的导入
""(默认)/logo.svg
"/assets"/assets/logo.svg
"https://cdn.example.com/"https://cdn.example.com/logo.svg

NOTE

复制文件的位置和文件名由 `naming.asset` 的值确定。

此加载器按原样复制到 outdir 中。复制文件的文件名使用 naming.asset 的值确定。

Bun学习网由www.bunjs.com.cn整理维护