bun-types 1.3.2-canary.20251106T140813 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (316) hide show
  1. package/bun.d.ts +102 -6
  2. package/docs/bundler/bytecode.mdx +465 -0
  3. package/docs/bundler/css.mdx +1024 -0
  4. package/docs/bundler/esbuild.mdx +253 -0
  5. package/docs/bundler/executables.mdx +535 -0
  6. package/docs/bundler/fullstack.mdx +1064 -0
  7. package/docs/bundler/hot-reloading.mdx +229 -0
  8. package/docs/bundler/html-static.mdx +386 -0
  9. package/docs/bundler/index.mdx +1499 -0
  10. package/docs/bundler/loaders.mdx +356 -0
  11. package/docs/bundler/macros.mdx +328 -0
  12. package/docs/bundler/minifier.mdx +1306 -0
  13. package/docs/bundler/plugins.mdx +411 -0
  14. package/docs/feedback.mdx +85 -0
  15. package/docs/guides/binary/arraybuffer-to-array.mdx +29 -0
  16. package/docs/guides/binary/arraybuffer-to-blob.mdx +26 -0
  17. package/docs/guides/binary/arraybuffer-to-buffer.mdx +27 -0
  18. package/docs/guides/binary/arraybuffer-to-string.mdx +17 -0
  19. package/docs/guides/binary/arraybuffer-to-typedarray.mdx +41 -0
  20. package/docs/guides/binary/blob-to-arraybuffer.mdx +16 -0
  21. package/docs/guides/binary/blob-to-dataview.mdx +16 -0
  22. package/docs/guides/binary/blob-to-stream.mdx +16 -0
  23. package/docs/guides/binary/blob-to-string.mdx +17 -0
  24. package/docs/guides/binary/blob-to-typedarray.mdx +16 -0
  25. package/docs/guides/binary/buffer-to-arraybuffer.mdx +16 -0
  26. package/docs/guides/binary/buffer-to-blob.mdx +16 -0
  27. package/docs/guides/binary/buffer-to-readablestream.mdx +43 -0
  28. package/docs/guides/binary/buffer-to-string.mdx +27 -0
  29. package/docs/guides/binary/buffer-to-typedarray.mdx +16 -0
  30. package/docs/guides/binary/dataview-to-string.mdx +17 -0
  31. package/docs/guides/binary/typedarray-to-arraybuffer.mdx +27 -0
  32. package/docs/guides/binary/typedarray-to-blob.mdx +18 -0
  33. package/docs/guides/binary/typedarray-to-buffer.mdx +16 -0
  34. package/docs/guides/binary/typedarray-to-dataview.mdx +16 -0
  35. package/docs/guides/binary/typedarray-to-readablestream.mdx +43 -0
  36. package/docs/guides/binary/typedarray-to-string.mdx +18 -0
  37. package/docs/guides/deployment/aws-lambda.mdx +204 -0
  38. package/docs/guides/deployment/digital-ocean.mdx +161 -0
  39. package/docs/guides/deployment/google-cloud-run.mdx +197 -0
  40. package/docs/guides/deployment/railway.mdx +145 -0
  41. package/docs/guides/deployment/render.mdx +82 -0
  42. package/docs/guides/deployment/vercel.mdx +99 -0
  43. package/docs/guides/ecosystem/astro.mdx +82 -0
  44. package/docs/guides/ecosystem/discordjs.mdx +80 -0
  45. package/docs/guides/ecosystem/docker.mdx +151 -0
  46. package/docs/guides/ecosystem/drizzle.mdx +195 -0
  47. package/docs/guides/ecosystem/edgedb.mdx +257 -0
  48. package/docs/guides/ecosystem/elysia.mdx +31 -0
  49. package/docs/guides/ecosystem/express.mdx +43 -0
  50. package/docs/guides/ecosystem/hono.mdx +47 -0
  51. package/docs/guides/ecosystem/mongoose.mdx +92 -0
  52. package/docs/guides/ecosystem/neon-drizzle.mdx +234 -0
  53. package/docs/guides/ecosystem/neon-serverless-postgres.mdx +60 -0
  54. package/docs/guides/ecosystem/nextjs.mdx +57 -0
  55. package/docs/guides/ecosystem/nuxt.mdx +90 -0
  56. package/docs/guides/ecosystem/pm2.mdx +55 -0
  57. package/docs/guides/ecosystem/prisma-postgres.mdx +169 -0
  58. package/docs/guides/ecosystem/prisma.mdx +164 -0
  59. package/docs/guides/ecosystem/qwik.mdx +114 -0
  60. package/docs/guides/ecosystem/react.mdx +52 -0
  61. package/docs/guides/ecosystem/remix.mdx +97 -0
  62. package/docs/guides/ecosystem/sentry.mdx +54 -0
  63. package/docs/guides/ecosystem/solidstart.mdx +66 -0
  64. package/docs/guides/ecosystem/ssr-react.mdx +49 -0
  65. package/docs/guides/ecosystem/stric.mdx +54 -0
  66. package/docs/guides/ecosystem/sveltekit.mdx +138 -0
  67. package/docs/guides/ecosystem/systemd.mdx +114 -0
  68. package/docs/guides/ecosystem/upstash.mdx +87 -0
  69. package/docs/guides/ecosystem/vite.mdx +77 -0
  70. package/docs/guides/html-rewriter/extract-links.mdx +72 -0
  71. package/docs/guides/html-rewriter/extract-social-meta.mdx +97 -0
  72. package/docs/guides/http/cluster.mdx +69 -0
  73. package/docs/guides/http/fetch-unix.mdx +35 -0
  74. package/docs/guides/http/fetch.mdx +26 -0
  75. package/docs/guides/http/file-uploads.mdx +97 -0
  76. package/docs/guides/http/hot.mdx +28 -0
  77. package/docs/guides/http/proxy.mdx +26 -0
  78. package/docs/guides/http/server.mdx +48 -0
  79. package/docs/guides/http/simple.mdx +20 -0
  80. package/docs/guides/http/stream-file.mdx +50 -0
  81. package/docs/guides/http/stream-iterator.mdx +49 -0
  82. package/docs/guides/http/stream-node-streams-in-bun.mdx +22 -0
  83. package/docs/guides/http/tls.mdx +32 -0
  84. package/docs/guides/index.mdx +10 -0
  85. package/docs/guides/install/add-dev.mdx +28 -0
  86. package/docs/guides/install/add-git.mdx +38 -0
  87. package/docs/guides/install/add-optional.mdx +27 -0
  88. package/docs/guides/install/add-peer.mdx +45 -0
  89. package/docs/guides/install/add-tarball.mdx +35 -0
  90. package/docs/guides/install/add.mdx +44 -0
  91. package/docs/guides/install/azure-artifacts.mdx +76 -0
  92. package/docs/guides/install/cicd.mdx +43 -0
  93. package/docs/guides/install/custom-registry.mdx +32 -0
  94. package/docs/guides/install/from-npm-install-to-bun-install.mdx +230 -0
  95. package/docs/guides/install/git-diff-bun-lockfile.mdx +47 -0
  96. package/docs/guides/install/jfrog-artifactory.mdx +28 -0
  97. package/docs/guides/install/npm-alias.mdx +25 -0
  98. package/docs/guides/install/registry-scope.mdx +40 -0
  99. package/docs/guides/install/trusted.mdx +50 -0
  100. package/docs/guides/install/workspaces.mdx +70 -0
  101. package/docs/guides/install/yarnlock.mdx +50 -0
  102. package/docs/guides/process/argv.mdx +66 -0
  103. package/docs/guides/process/ctrl-c.mdx +18 -0
  104. package/docs/guides/process/ipc.mdx +69 -0
  105. package/docs/guides/process/nanoseconds.mdx +15 -0
  106. package/docs/guides/process/os-signals.mdx +41 -0
  107. package/docs/guides/process/spawn-stderr.mdx +34 -0
  108. package/docs/guides/process/spawn-stdout.mdx +28 -0
  109. package/docs/guides/process/spawn.mdx +43 -0
  110. package/docs/guides/process/stdin.mdx +62 -0
  111. package/docs/guides/read-file/arraybuffer.mdx +30 -0
  112. package/docs/guides/read-file/buffer.mdx +21 -0
  113. package/docs/guides/read-file/exists.mdx +18 -0
  114. package/docs/guides/read-file/json.mdx +19 -0
  115. package/docs/guides/read-file/mime.mdx +22 -0
  116. package/docs/guides/read-file/stream.mdx +28 -0
  117. package/docs/guides/read-file/string.mdx +24 -0
  118. package/docs/guides/read-file/uint8array.mdx +23 -0
  119. package/docs/guides/read-file/watch.mdx +66 -0
  120. package/docs/guides/runtime/build-time-constants.mdx +295 -0
  121. package/docs/guides/runtime/cicd.mdx +45 -0
  122. package/docs/guides/runtime/codesign-macos-executable.mdx +61 -0
  123. package/docs/guides/runtime/define-constant.mdx +149 -0
  124. package/docs/guides/runtime/delete-directory.mdx +39 -0
  125. package/docs/guides/runtime/delete-file.mdx +21 -0
  126. package/docs/guides/runtime/heap-snapshot.mdx +28 -0
  127. package/docs/guides/runtime/import-html.mdx +17 -0
  128. package/docs/guides/runtime/import-json.mdx +46 -0
  129. package/docs/guides/runtime/import-toml.mdx +32 -0
  130. package/docs/guides/runtime/import-yaml.mdx +104 -0
  131. package/docs/guides/runtime/read-env.mdx +37 -0
  132. package/docs/guides/runtime/set-env.mdx +51 -0
  133. package/docs/guides/runtime/shell.mdx +42 -0
  134. package/docs/guides/runtime/timezone.mdx +38 -0
  135. package/docs/guides/runtime/tsconfig-paths.mdx +31 -0
  136. package/docs/guides/runtime/typescript.mdx +51 -0
  137. package/docs/guides/runtime/vscode-debugger.mdx +48 -0
  138. package/docs/guides/runtime/web-debugger.mdx +103 -0
  139. package/docs/guides/streams/node-readable-to-arraybuffer.mdx +13 -0
  140. package/docs/guides/streams/node-readable-to-blob.mdx +13 -0
  141. package/docs/guides/streams/node-readable-to-json.mdx +14 -0
  142. package/docs/guides/streams/node-readable-to-string.mdx +14 -0
  143. package/docs/guides/streams/node-readable-to-uint8array.mdx +13 -0
  144. package/docs/guides/streams/to-array.mdx +16 -0
  145. package/docs/guides/streams/to-arraybuffer.mdx +16 -0
  146. package/docs/guides/streams/to-blob.mdx +16 -0
  147. package/docs/guides/streams/to-buffer.mdx +17 -0
  148. package/docs/guides/streams/to-json.mdx +16 -0
  149. package/docs/guides/streams/to-string.mdx +16 -0
  150. package/docs/guides/streams/to-typedarray.mdx +24 -0
  151. package/docs/guides/test/bail.mdx +24 -0
  152. package/docs/guides/test/coverage-threshold.mdx +67 -0
  153. package/docs/guides/test/coverage.mdx +49 -0
  154. package/docs/guides/test/happy-dom.mdx +73 -0
  155. package/docs/guides/test/migrate-from-jest.mdx +125 -0
  156. package/docs/guides/test/mock-clock.mdx +50 -0
  157. package/docs/guides/test/mock-functions.mdx +70 -0
  158. package/docs/guides/test/rerun-each.mdx +16 -0
  159. package/docs/guides/test/run-tests.mdx +116 -0
  160. package/docs/guides/test/skip-tests.mdx +43 -0
  161. package/docs/guides/test/snapshot.mdx +102 -0
  162. package/docs/guides/test/spy-on.mdx +49 -0
  163. package/docs/guides/test/svelte-test.mdx +113 -0
  164. package/docs/guides/test/testing-library.mdx +93 -0
  165. package/docs/guides/test/timeout.mdx +17 -0
  166. package/docs/guides/test/todo-tests.mdx +74 -0
  167. package/docs/guides/test/update-snapshots.mdx +49 -0
  168. package/docs/guides/test/watch-mode.mdx +24 -0
  169. package/docs/guides/util/base64.mdx +17 -0
  170. package/docs/guides/util/deep-equals.mdx +41 -0
  171. package/docs/guides/util/deflate.mdx +20 -0
  172. package/docs/guides/util/detect-bun.mdx +25 -0
  173. package/docs/guides/util/entrypoint.mdx +19 -0
  174. package/docs/guides/util/escape-html.mdx +24 -0
  175. package/docs/guides/util/file-url-to-path.mdx +16 -0
  176. package/docs/guides/util/gzip.mdx +20 -0
  177. package/docs/guides/util/hash-a-password.mdx +56 -0
  178. package/docs/guides/util/import-meta-dir.mdx +15 -0
  179. package/docs/guides/util/import-meta-file.mdx +15 -0
  180. package/docs/guides/util/import-meta-path.mdx +15 -0
  181. package/docs/guides/util/javascript-uuid.mdx +25 -0
  182. package/docs/guides/util/main.mdx +43 -0
  183. package/docs/guides/util/path-to-file-url.mdx +16 -0
  184. package/docs/guides/util/sleep.mdx +24 -0
  185. package/docs/guides/util/version.mdx +23 -0
  186. package/docs/guides/util/which-path-to-executable-bin.mdx +17 -0
  187. package/docs/guides/websocket/compression.mdx +33 -0
  188. package/docs/guides/websocket/context.mdx +74 -0
  189. package/docs/guides/websocket/pubsub.mdx +40 -0
  190. package/docs/guides/websocket/simple.mdx +35 -0
  191. package/docs/guides/write-file/append.mdx +54 -0
  192. package/docs/guides/write-file/basic.mdx +46 -0
  193. package/docs/guides/write-file/blob.mdx +30 -0
  194. package/docs/guides/write-file/cat.mdx +19 -0
  195. package/docs/guides/write-file/file-cp.mdx +18 -0
  196. package/docs/guides/write-file/filesink.mdx +54 -0
  197. package/docs/guides/write-file/response.mdx +19 -0
  198. package/docs/guides/write-file/stdout.mdx +23 -0
  199. package/docs/guides/write-file/stream.mdx +19 -0
  200. package/docs/guides/write-file/unlink.mdx +18 -0
  201. package/docs/index.mdx +133 -0
  202. package/docs/installation.mdx +365 -0
  203. package/docs/pm/bunx.mdx +83 -0
  204. package/docs/pm/catalogs.mdx +292 -0
  205. package/docs/pm/cli/add.mdx +179 -0
  206. package/docs/pm/cli/audit.mdx +60 -0
  207. package/docs/pm/cli/install.mdx +471 -0
  208. package/docs/pm/cli/link.mdx +48 -0
  209. package/docs/pm/cli/outdated.mdx +197 -0
  210. package/docs/pm/cli/patch.mdx +69 -0
  211. package/docs/pm/cli/pm.mdx +319 -0
  212. package/docs/pm/cli/publish.mdx +123 -0
  213. package/docs/pm/cli/remove.mdx +16 -0
  214. package/docs/pm/cli/update.mdx +140 -0
  215. package/docs/pm/cli/why.mdx +84 -0
  216. package/docs/pm/filter.mdx +102 -0
  217. package/docs/pm/global-cache.mdx +72 -0
  218. package/docs/pm/isolated-installs.mdx +205 -0
  219. package/docs/pm/lifecycle.mdx +57 -0
  220. package/docs/pm/lockfile.mdx +64 -0
  221. package/docs/pm/npmrc.mdx +111 -0
  222. package/docs/pm/overrides.mdx +83 -0
  223. package/docs/pm/scopes-registries.mdx +35 -0
  224. package/docs/pm/security-scanner-api.mdx +95 -0
  225. package/docs/pm/workspaces.mdx +109 -0
  226. package/docs/project/benchmarking.mdx +218 -0
  227. package/docs/project/bindgen.mdx +223 -0
  228. package/docs/project/building-windows.mdx +133 -0
  229. package/docs/project/contributing.mdx +343 -0
  230. package/docs/project/feedback.mdx +20 -0
  231. package/docs/project/license.mdx +78 -0
  232. package/docs/project/roadmap.mdx +8 -0
  233. package/docs/quickstart.mdx +240 -0
  234. package/docs/runtime/auto-install.mdx +97 -0
  235. package/docs/runtime/binary-data.mdx +846 -0
  236. package/docs/runtime/bun-apis.mdx +59 -0
  237. package/docs/runtime/bunfig.mdx +642 -0
  238. package/docs/runtime/c-compiler.mdx +204 -0
  239. package/docs/runtime/child-process.mdx +532 -0
  240. package/docs/runtime/color.mdx +267 -0
  241. package/docs/runtime/console.mdx +67 -0
  242. package/docs/runtime/cookies.mdx +454 -0
  243. package/docs/runtime/debugger.mdx +335 -0
  244. package/docs/runtime/environment-variables.mdx +214 -0
  245. package/docs/runtime/ffi.mdx +565 -0
  246. package/docs/runtime/file-io.mdx +306 -0
  247. package/docs/runtime/file-system-router.mdx +118 -0
  248. package/docs/runtime/file-types.mdx +354 -0
  249. package/docs/runtime/glob.mdx +181 -0
  250. package/docs/runtime/globals.mdx +72 -0
  251. package/docs/runtime/hashing.mdx +315 -0
  252. package/docs/runtime/html-rewriter.mdx +340 -0
  253. package/docs/runtime/http/cookies.mdx +79 -0
  254. package/docs/runtime/http/error-handling.mdx +40 -0
  255. package/docs/runtime/http/metrics.mdx +36 -0
  256. package/docs/runtime/http/routing.mdx +289 -0
  257. package/docs/runtime/http/server.mdx +647 -0
  258. package/docs/runtime/http/tls.mdx +101 -0
  259. package/docs/runtime/http/websockets.mdx +404 -0
  260. package/docs/runtime/index.mdx +223 -0
  261. package/docs/runtime/jsx.mdx +115 -0
  262. package/docs/runtime/module-resolution.mdx +342 -0
  263. package/docs/runtime/networking/dns.mdx +111 -0
  264. package/docs/runtime/networking/fetch.mdx +468 -0
  265. package/docs/runtime/networking/tcp.mdx +239 -0
  266. package/docs/runtime/networking/udp.mdx +129 -0
  267. package/docs/runtime/node-api.mdx +19 -0
  268. package/docs/runtime/nodejs-compat.mdx +468 -0
  269. package/docs/runtime/plugins.mdx +405 -0
  270. package/docs/runtime/redis.mdx +582 -0
  271. package/docs/runtime/s3.mdx +863 -0
  272. package/docs/runtime/secrets.mdx +336 -0
  273. package/docs/runtime/semver.mdx +57 -0
  274. package/docs/runtime/shell.mdx +637 -0
  275. package/docs/runtime/sql.mdx +1404 -0
  276. package/docs/runtime/sqlite.mdx +699 -0
  277. package/docs/runtime/streams.mdx +232 -0
  278. package/docs/runtime/templating/create.mdx +269 -0
  279. package/docs/runtime/templating/init.mdx +58 -0
  280. package/docs/runtime/transpiler.mdx +288 -0
  281. package/docs/runtime/typescript.mdx +58 -0
  282. package/docs/runtime/utils.mdx +922 -0
  283. package/docs/runtime/watch-mode.mdx +161 -0
  284. package/docs/runtime/web-apis.mdx +29 -0
  285. package/docs/runtime/workers.mdx +328 -0
  286. package/docs/runtime/yaml.mdx +469 -0
  287. package/docs/snippets/cli/add.mdx +166 -0
  288. package/docs/snippets/cli/build.mdx +196 -0
  289. package/docs/snippets/cli/feedback.mdx +17 -0
  290. package/docs/snippets/cli/init.mdx +84 -0
  291. package/docs/snippets/cli/install.mdx +173 -0
  292. package/docs/snippets/cli/link.mdx +163 -0
  293. package/docs/snippets/cli/outdated.mdx +140 -0
  294. package/docs/snippets/cli/patch.mdx +171 -0
  295. package/docs/snippets/cli/publish.mdx +198 -0
  296. package/docs/snippets/cli/remove.mdx +146 -0
  297. package/docs/snippets/cli/run.mdx +293 -0
  298. package/docs/snippets/cli/test.mdx +100 -0
  299. package/docs/snippets/cli/update.mdx +144 -0
  300. package/docs/snippets/product-card.mdx +32 -0
  301. package/docs/snippets/product-tiles.mdx +94 -0
  302. package/docs/test/code-coverage.mdx +409 -0
  303. package/docs/test/configuration.mdx +467 -0
  304. package/docs/test/dates-times.mdx +129 -0
  305. package/docs/test/discovery.mdx +90 -0
  306. package/docs/test/dom.mdx +226 -0
  307. package/docs/test/index.mdx +380 -0
  308. package/docs/test/lifecycle.mdx +348 -0
  309. package/docs/test/mocks.mdx +637 -0
  310. package/docs/test/reporters.mdx +117 -0
  311. package/docs/test/runtime-behavior.mdx +342 -0
  312. package/docs/test/snapshots.mdx +434 -0
  313. package/docs/test/writing-tests.mdx +635 -0
  314. package/docs/typescript.mdx +54 -0
  315. package/package.json +8 -6
  316. package/test.d.ts +2 -2
@@ -0,0 +1,356 @@
1
+ ---
2
+ title: Loaders
3
+ description: Built-in loaders for the Bun bundler and runtime
4
+ ---
5
+
6
+ The Bun bundler implements a set of default loaders out of the box.
7
+
8
+ > As a rule of thumb: **the bundler and the runtime both support the same set of file types out of the box.**
9
+
10
+ `.js` `.cjs` `.mjs` `.mts` `.cts` `.ts` `.tsx` `.jsx` `.toml` `.json` `.txt` `.wasm` `.node` `.html`
11
+
12
+ Bun uses the file extension to determine which built-in loader should be used to parse the file. Every loader has a name, such as `js`, `tsx`, or `json`. These names are used when building plugins that extend Bun with custom loaders.
13
+
14
+ You can explicitly specify which loader to use using the `'loader'` import attribute.
15
+
16
+ ```ts title="index.ts" icon="/icons/typescript.svg"
17
+ import my_toml from "./my_file" with { loader: "toml" };
18
+ ```
19
+
20
+ ## Built-in loaders
21
+
22
+ ### `js`
23
+
24
+ **JavaScript loader.** Default for `.cjs` and `.mjs`.
25
+
26
+ Parses the code and applies a set of default transforms like dead-code elimination and tree shaking. Note that Bun does not attempt to down-convert syntax at the moment.
27
+
28
+ ---
29
+
30
+ ### `jsx`
31
+
32
+ **JavaScript + JSX loader.** Default for `.js` and `.jsx`.
33
+
34
+ Same as the `js` loader, but JSX syntax is supported. By default, JSX is down-converted to plain JavaScript; the details of how this is done depends on the `jsx*` compiler options in your `tsconfig.json`. Refer to the [TypeScript documentation on JSX](https://www.typescriptlang.org/tsconfig#jsx) for more information.
35
+
36
+ ---
37
+
38
+ ### `ts`
39
+
40
+ **TypeScript loader.** Default for `.ts`, `.mts`, and `.cts`.
41
+
42
+ Strips out all TypeScript syntax, then behaves identically to the `js` loader. Bun does not perform typechecking.
43
+
44
+ ---
45
+
46
+ ### `tsx`
47
+
48
+ **TypeScript + JSX loader.** Default for `.tsx`.
49
+
50
+ Transpiles both TypeScript and JSX to vanilla JavaScript.
51
+
52
+ ---
53
+
54
+ ### `json`
55
+
56
+ **JSON loader.** Default for `.json`.
57
+
58
+ JSON files can be directly imported.
59
+
60
+ ```js
61
+ import pkg from "./package.json";
62
+ pkg.name; // => "my-package"
63
+ ```
64
+
65
+ During bundling, the parsed JSON is inlined into the bundle as a JavaScript object.
66
+
67
+ ```js
68
+ const pkg = {
69
+ name: "my-package",
70
+ // ... other fields
71
+ };
72
+
73
+ pkg.name;
74
+ ```
75
+
76
+ If a `.json` file is passed as an entrypoint to the bundler, it will be converted to a `.js` module that `export default`s the parsed object.
77
+
78
+ <CodeGroup>
79
+
80
+ ```json Input
81
+ {
82
+ "name": "John Doe",
83
+ "age": 35,
84
+ "email": "johndoe@example.com"
85
+ }
86
+ ```
87
+
88
+ ```ts Output
89
+ export default {
90
+ name: "John Doe",
91
+ age: 35,
92
+ email: "johndoe@example.com",
93
+ };
94
+ ```
95
+
96
+ </CodeGroup>
97
+
98
+ ---
99
+
100
+ ### toml
101
+
102
+ **TOML loader.** Default for `.toml`.
103
+
104
+ TOML files can be directly imported. Bun will parse them with its fast native TOML parser.
105
+
106
+ ```js
107
+ import config from "./bunfig.toml";
108
+ config.logLevel; // => "debug"
109
+
110
+ // via import attribute:
111
+ // import myCustomTOML from './my.config' with {type: "toml"};
112
+ ```
113
+
114
+ During bundling, the parsed TOML is inlined into the bundle as a JavaScript object.
115
+
116
+ ```js
117
+ var config = {
118
+ logLevel: "debug",
119
+ // ...other fields
120
+ };
121
+ config.logLevel;
122
+ ```
123
+
124
+ If a `.toml` file is passed as an entrypoint, it will be converted to a `.js` module that `export default`s the parsed object.
125
+
126
+ <CodeGroup>
127
+
128
+ ```toml Input
129
+ name = "John Doe"
130
+ age = 35
131
+ email = "johndoe@example.com"
132
+ ```
133
+
134
+ ```ts Output
135
+ export default {
136
+ name: "John Doe",
137
+ age: 35,
138
+ email: "johndoe@example.com",
139
+ };
140
+ ```
141
+
142
+ </CodeGroup>
143
+
144
+ ---
145
+
146
+ ### text
147
+
148
+ **Text loader.** Default for `.txt`.
149
+
150
+ The contents of the text file are read and inlined into the bundle as a string. Text files can be directly imported. The file is read and returned as a string.
151
+
152
+ ```js
153
+ import contents from "./file.txt";
154
+ console.log(contents); // => "Hello, world!"
155
+
156
+ // To import an html file as text
157
+ // The "type" attribute can be used to override the default loader.
158
+ import html from "./index.html" with { type: "text" };
159
+ ```
160
+
161
+ When referenced during a build, the contents are inlined into the bundle as a string.
162
+
163
+ ```js
164
+ var contents = `Hello, world!`;
165
+ console.log(contents);
166
+ ```
167
+
168
+ If a `.txt` file is passed as an entrypoint, it will be converted to a `.js` module that `export default`s the file contents.
169
+
170
+ <CodeGroup>
171
+
172
+ ```txt Input
173
+ Hello, world!
174
+ ```
175
+
176
+ ```ts Output
177
+ export default "Hello, world!";
178
+ ```
179
+
180
+ </CodeGroup>
181
+
182
+ ---
183
+
184
+ ### napi
185
+
186
+ **Native addon loader.** Default for `.node`.
187
+
188
+ In the runtime, native addons can be directly imported.
189
+
190
+ ```js
191
+ import addon from "./addon.node";
192
+ console.log(addon);
193
+ ```
194
+
195
+ <Note>In the bundler, `.node` files are handled using the file loader.</Note>
196
+
197
+ ---
198
+
199
+ ### sqlite
200
+
201
+ **SQLite loader.** Requires `with { "type": "sqlite" }` import attribute.
202
+
203
+ In the runtime and bundler, SQLite databases can be directly imported. This will load the database using `bun:sqlite`.
204
+
205
+ ```js
206
+ import db from "./my.db" with { type: "sqlite" };
207
+ ```
208
+
209
+ <Warning>This is only supported when the target is `bun`.</Warning>
210
+
211
+ By default, the database is external to the bundle (so that you can potentially use a database loaded elsewhere), so the database file on-disk won't be bundled into the final output.
212
+
213
+ You can change this behavior with the `"embed"` attribute:
214
+
215
+ ```js
216
+ // embed the database into the bundle
217
+ import db from "./my.db" with { type: "sqlite", embed: "true" };
218
+ ```
219
+
220
+ <Info>
221
+ When using a standalone executable, the database is embedded into the single-file executable.
222
+
223
+ Otherwise, the database to embed is copied into the `outdir` with a hashed filename.
224
+
225
+ </Info>
226
+
227
+ ---
228
+
229
+ ### html
230
+
231
+ The `html` loader processes HTML files and bundles any referenced assets. It will:
232
+
233
+ - Bundle and hash referenced JavaScript files (`<script src="...">`)
234
+ - Bundle and hash referenced CSS files (`<link rel="stylesheet" href="...">`)
235
+ - Hash referenced images (`<img src="...">`)
236
+ - Preserve external URLs (by default, anything starting with `http://` or `https://`)
237
+
238
+ For example, given this HTML file:
239
+
240
+ ```html title="src/index.html"
241
+ <!DOCTYPE html>
242
+ <html>
243
+ <body>
244
+ <img src="./image.jpg" alt="Local image" />
245
+ <img src="https://example.com/image.jpg" alt="External image" />
246
+ <script type="module" src="./script.js"></script>
247
+ </body>
248
+ </html>
249
+ ```
250
+
251
+ It will output a new HTML file with the bundled assets:
252
+
253
+ ```html title="dist/index.html"
254
+ <!DOCTYPE html>
255
+ <html>
256
+ <body>
257
+ <img src="./image-HASHED.jpg" alt="Local image" />
258
+ <img src="https://example.com/image.jpg" alt="External image" />
259
+ <script type="module" src="./output-ALSO-HASHED.js"></script>
260
+ </body>
261
+ </html>
262
+ ```
263
+
264
+ Under the hood, it uses [`lol-html`](https://github.com/cloudflare/lol-html) to extract script and link tags as entrypoints, and other assets as external.
265
+
266
+ <Accordion title="List of supported HTML selectors">
267
+ Currently, the list of selectors is:
268
+
269
+ - `audio[src]`
270
+ - `iframe[src]`
271
+ - `img[src]`
272
+ - `img[srcset]`
273
+ - `link:not([rel~='stylesheet']):not([rel~='modulepreload']):not([rel~='manifest']):not([rel~='icon']):not([rel~='apple-touch-icon'])[href]`
274
+ - `link[as='font'][href], link[type^='font/'][href]`
275
+ - `link[as='image'][href]`
276
+ - `link[as='style'][href]`
277
+ - `link[as='video'][href], link[as='audio'][href]`
278
+ - `link[as='worker'][href]`
279
+ - `link[rel='icon'][href], link[rel='apple-touch-icon'][href]`
280
+ - `link[rel='manifest'][href]`
281
+ - `link[rel='stylesheet'][href]`
282
+ - `script[src]`
283
+ - `source[src]`
284
+ - `source[srcset]`
285
+ - `video[poster]`
286
+ - `video[src]`
287
+
288
+ </Accordion>
289
+
290
+ <Note>
291
+
292
+ **HTML Loader Behavior in Different Contexts**
293
+
294
+ The `html` loader behaves differently depending on how it's used:
295
+
296
+ - Static Build: When you run `bun build ./index.html`, Bun produces a static site with all assets bundled and hashed.
297
+ - Runtime: When you run `bun run server.ts` (where `server.ts` imports an HTML file), Bun bundles assets on-the-fly during development, enabling features like hot module replacement.
298
+ - Full-stack Build: When you run `bun build --target=bun server.ts` (where `server.ts` imports an HTML file), the import resolves to a manifest object that `Bun.serve` uses to efficiently serve pre-bundled assets in production.
299
+
300
+ </Note>
301
+
302
+ ---
303
+
304
+ ### sh
305
+
306
+ **Bun Shell loader.** Default for `.sh` files.
307
+
308
+ 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 in the runtime.
309
+
310
+ ```bash
311
+ bun run ./script.sh
312
+ ```
313
+
314
+ ---
315
+
316
+ ### file
317
+
318
+ **File loader.** Default for all unrecognized file types.
319
+
320
+ The file loader resolves the import as a path/URL to the imported file. It's commonly used for referencing media or font assets.
321
+
322
+ ```js
323
+ // logo.ts
324
+ import logo from "./logo.svg";
325
+ console.log(logo);
326
+ ```
327
+
328
+ In the runtime, Bun checks that the `logo.svg` file exists and converts it to an absolute path to the location of `logo.svg` on disk.
329
+
330
+ ```bash
331
+ bun run logo.ts
332
+ # Output: /path/to/project/logo.svg
333
+ ```
334
+
335
+ In the bundler, things are slightly different. The file is copied into `outdir` as-is, and the import is resolved as a relative path pointing to the copied file.
336
+
337
+ ```js
338
+ // Output
339
+ var logo = "./logo.svg";
340
+ console.log(logo);
341
+ ```
342
+
343
+ If a value is specified for `publicPath`, the import will use value as a prefix to construct an absolute path/URL.
344
+
345
+ | Public path | Resolved import |
346
+ | ---------------------------- | ---------------------------------- |
347
+ | `""` (default) | `/logo.svg` |
348
+ | `"/assets"` | `/assets/logo.svg` |
349
+ | `"https://cdn.example.com/"` | `https://cdn.example.com/logo.svg` |
350
+
351
+ <Note>
352
+ The location and file name of the copied file is determined by the value of `naming.asset`.
353
+
354
+ This loader is copied into the `outdir` as-is. The name of the copied file is determined using the value of `naming.asset`.
355
+
356
+ </Note>
@@ -0,0 +1,328 @@
1
+ ---
2
+ title: Macros
3
+ description: Run JavaScript functions at bundle-time with Bun macros
4
+ ---
5
+
6
+ Macros are a mechanism for running JavaScript functions at bundle-time. The value returned from these functions are directly inlined into your bundle.
7
+
8
+ As a toy example, consider this simple function that returns a random number.
9
+
10
+ ```ts title="random.ts" icon="/icons/typescript.svg"
11
+ export function random() {
12
+ return Math.random();
13
+ }
14
+ ```
15
+
16
+ This is just a regular function in a regular file, but we can use it as a macro like so:
17
+
18
+ ```tsx title="cli.tsx" icon="/icons/typescript.svg"
19
+ import { random } from "./random.ts" with { type: "macro" };
20
+
21
+ console.log(`Your random number is ${random()}`);
22
+ ```
23
+
24
+ <Note>
25
+ Macros are indicated using import attribute syntax. If you haven't seen this syntax before, it's a Stage 3 TC39
26
+ proposal that lets you attach additional metadata to import statements.
27
+ </Note>
28
+
29
+ Now we'll bundle this file with `bun build`. The bundled file will be printed to stdout.
30
+
31
+ ```bash terminal icon="terminal"
32
+ bun build ./cli.tsx
33
+ ```
34
+
35
+ ```js
36
+ console.log(`Your random number is ${0.6805550949689833}`);
37
+ ```
38
+
39
+ As you can see, the source code of the `random` function occurs nowhere in the bundle. Instead, it is executed during bundling and function call (`random()`) is replaced with the result of the function. Since the source code will never be included in the bundle, macros can safely perform privileged operations like reading from a database.
40
+
41
+ ## When to use macros
42
+
43
+ If you have several build scripts for small things where you would otherwise have a one-off build script, bundle-time code execution can be easier to maintain. It lives with the rest of your code, it runs with the rest of the build, it is automatically parallelized, and if it fails, the build fails too.
44
+
45
+ If you find yourself running a lot of code at bundle-time though, consider running a server instead.
46
+
47
+ ## Import attributes
48
+
49
+ Bun Macros are import statements annotated using either:
50
+
51
+ - `with { type: 'macro' }` — an import attribute, a Stage 3 ECMA Script proposal
52
+ - `assert { type: 'macro' }` — an import assertion, an earlier incarnation of import attributes that has now been abandoned (but is already supported by a number of browsers and runtimes)
53
+
54
+ ## Security considerations
55
+
56
+ Macros must explicitly be imported with `{ type: "macro" }` in order to be executed at bundle-time. These imports have no effect if they are not called, unlike regular JavaScript imports which may have side effects.
57
+
58
+ You can disable macros entirely by passing the `--no-macros` flag to Bun. It produces a build error like this:
59
+
60
+ ```
61
+ error: Macros are disabled
62
+
63
+ foo();
64
+ ^
65
+ ./hello.js:3:1 53
66
+ ```
67
+
68
+ To reduce the potential attack surface for malicious packages, macros cannot be invoked from inside `node_modules/**/*`. If a package attempts to invoke a macro, you'll see an error like this:
69
+
70
+ ```
71
+ error: For security reasons, macros cannot be run from node_modules.
72
+
73
+ beEvil();
74
+ ^
75
+ node_modules/evil/index.js:3:1 50
76
+ ```
77
+
78
+ Your application code can still import macros from `node_modules` and invoke them.
79
+
80
+ ```ts title="cli.tsx" icon="/icons/typescript.svg"
81
+ import { macro } from "some-package" with { type: "macro" };
82
+
83
+ macro();
84
+ ```
85
+
86
+ ## Export condition "macro"
87
+
88
+ When shipping a library containing a macro to npm or another package registry, use the `"macro"` export condition to provide a special version of your package exclusively for the macro environment.
89
+
90
+ ```json title="package.json" icon="file-code"
91
+ {
92
+ "name": "my-package",
93
+ "exports": {
94
+ "import": "./index.js",
95
+ "require": "./index.js",
96
+ "default": "./index.js",
97
+ "macro": "./index.macro.js"
98
+ }
99
+ }
100
+ ```
101
+
102
+ With this configuration, users can consume your package at runtime or at bundle-time using the same import specifier:
103
+
104
+ ```ts title="index.ts" icon="/icons/typescript.svg"
105
+ import pkg from "my-package"; // runtime import
106
+ import { macro } from "my-package" with { type: "macro" }; // macro import
107
+ ```
108
+
109
+ The first import will resolve to `./node_modules/my-package/index.js`, while the second will be resolved by Bun's bundler to `./node_modules/my-package/index.macro.js`.
110
+
111
+ ## Execution
112
+
113
+ When Bun's transpiler sees a macro import, it calls the function inside the transpiler using Bun's JavaScript runtime and converts the return value from JavaScript into an AST node. These JavaScript functions are called at bundle-time, not runtime.
114
+
115
+ Macros are executed synchronously in the transpiler during the visiting phase—before plugins and before the transpiler generates the AST. They are executed in the order they are imported. The transpiler will wait for the macro to finish executing before continuing. The transpiler will also await any Promise returned by a macro.
116
+
117
+ Bun's bundler is multi-threaded. As such, macros execute in parallel inside of multiple spawned JavaScript "workers".
118
+
119
+ ## Dead code elimination
120
+
121
+ The bundler performs dead code elimination after running and inlining macros. So given the following macro:
122
+
123
+ ```ts title="returnFalse.ts" icon="/icons/typescript.svg"
124
+ export function returnFalse() {
125
+ return false;
126
+ }
127
+ ```
128
+
129
+ ...then bundling the following file will produce an empty bundle, provided that the minify syntax option is enabled.
130
+
131
+ ```ts title="index.ts" icon="/icons/typescript.svg"
132
+ import { returnFalse } from "./returnFalse.ts" with { type: "macro" };
133
+
134
+ if (returnFalse()) {
135
+ console.log("This code is eliminated");
136
+ }
137
+ ```
138
+
139
+ ## Serializability
140
+
141
+ Bun's transpiler needs to be able to serialize the result of the macro so it can be inlined into the AST. All JSON-compatible data structures are supported:
142
+
143
+ ```ts title="macro.ts" icon="/icons/typescript.svg"
144
+ export function getObject() {
145
+ return {
146
+ foo: "bar",
147
+ baz: 123,
148
+ array: [1, 2, { nested: "value" }],
149
+ };
150
+ }
151
+ ```
152
+
153
+ Macros can be async, or return Promise instances. Bun's transpiler will automatically await the Promise and inline the result.
154
+
155
+ ```ts title="macro.ts" icon="/icons/typescript.svg"
156
+ export async function getText() {
157
+ return "async value";
158
+ }
159
+ ```
160
+
161
+ The transpiler implements special logic for serializing common data formats like `Response`, `Blob`, `TypedArray`.
162
+
163
+ - **TypedArray**: Resolves to a base64-encoded string.
164
+ - **Response**: Bun will read the `Content-Type` and serialize accordingly; for instance, a Response with type `application/json` will be automatically parsed into an object and `text/plain` will be inlined as a string. Responses with an unrecognized or undefined type will be base-64 encoded.
165
+ - **Blob**: As with Response, the serialization depends on the `type` property.
166
+
167
+ The result of `fetch` is `Promise<Response>`, so it can be directly returned.
168
+
169
+ ```ts title="macro.ts" icon="/icons/typescript.svg"
170
+ export function getObject() {
171
+ return fetch("https://bun.com");
172
+ }
173
+ ```
174
+
175
+ Functions and instances of most classes (except those mentioned above) are not serializable.
176
+
177
+ ```ts title="macro.ts" icon="/icons/typescript.svg"
178
+ export function getText(url: string) {
179
+ // this doesn't work!
180
+ return () => {};
181
+ }
182
+ ```
183
+
184
+ ## Arguments
185
+
186
+ Macros can accept inputs, but only in limited cases. The value must be statically known. For example, the following is not allowed:
187
+
188
+ ```ts title="index.ts" icon="/icons/typescript.svg"
189
+ import { getText } from "./getText.ts" with { type: "macro" };
190
+
191
+ export function howLong() {
192
+ // the value of `foo` cannot be statically known
193
+ const foo = Math.random() ? "foo" : "bar";
194
+
195
+ const text = getText(`https://example.com/${foo}`);
196
+ console.log("The page is ", text.length, " characters long");
197
+ }
198
+ ```
199
+
200
+ However, if the value of `foo` is known at bundle-time (say, if it's a constant or the result of another macro) then it's allowed:
201
+
202
+ ```ts title="index.ts" icon="/icons/typescript.svg"
203
+ import { getText } from "./getText.ts" with { type: "macro" };
204
+ import { getFoo } from "./getFoo.ts" with { type: "macro" };
205
+
206
+ export function howLong() {
207
+ // this works because getFoo() is statically known
208
+ const foo = getFoo();
209
+ const text = getText(`https://example.com/${foo}`);
210
+ console.log("The page is", text.length, "characters long");
211
+ }
212
+ ```
213
+
214
+ This outputs:
215
+
216
+ ```js
217
+ function howLong() {
218
+ console.log("The page is", 1322, "characters long");
219
+ }
220
+ export { howLong };
221
+ ```
222
+
223
+ ## Examples
224
+
225
+ ### Embed latest git commit hash
226
+
227
+ ```ts title="getGitCommitHash.ts" icon="/icons/typescript.svg"
228
+ export function getGitCommitHash() {
229
+ const { stdout } = Bun.spawnSync({
230
+ cmd: ["git", "rev-parse", "HEAD"],
231
+ stdout: "pipe",
232
+ });
233
+
234
+ return stdout.toString();
235
+ }
236
+ ```
237
+
238
+ When we build it, the `getGitCommitHash` is replaced with the result of calling the function:
239
+
240
+ <CodeGroup>
241
+
242
+ ```ts input
243
+ import { getGitCommitHash } from "./getGitCommitHash.ts" with { type: "macro" };
244
+
245
+ console.log(`The current Git commit hash is ${getGitCommitHash()}`);
246
+ ```
247
+
248
+ ```ts output
249
+ console.log(`The current Git commit hash is 3ee3259104e4507cf62c160f0ff5357ec4c7a7f8`);
250
+ ```
251
+
252
+ </CodeGroup>
253
+
254
+ <Info>
255
+ You're probably thinking "Why not just use `process.env.GIT_COMMIT_HASH`?" Well, you can do that too. But can you do
256
+ this with an environment variable?
257
+ </Info>
258
+
259
+ ### Make fetch() requests at bundle-time
260
+
261
+ In this example, we make an outgoing HTTP request using `fetch()`, parse the HTML response using `HTMLRewriter`, and return an object containing the title and meta tags–all at bundle-time.
262
+
263
+ ```ts title="meta.ts" icon="/icons/typescript.svg"
264
+ export async function extractMetaTags(url: string) {
265
+ const response = await fetch(url);
266
+ const meta = {
267
+ title: "",
268
+ };
269
+ new HTMLRewriter()
270
+ .on("title", {
271
+ text(element) {
272
+ meta.title += element.text;
273
+ },
274
+ })
275
+ .on("meta", {
276
+ element(element) {
277
+ const name =
278
+ element.getAttribute("name") || element.getAttribute("property") || element.getAttribute("itemprop");
279
+
280
+ if (name) meta[name] = element.getAttribute("content");
281
+ },
282
+ })
283
+ .transform(response);
284
+
285
+ return meta;
286
+ }
287
+ ```
288
+
289
+ The `extractMetaTags` function is erased at bundle-time and replaced with the result of the function call. This means that the fetch request happens at bundle-time, and the result is embedded in the bundle. Also, the branch throwing the error is eliminated since it's unreachable.
290
+
291
+ <CodeGroup>
292
+
293
+ ```jsx input
294
+ import { extractMetaTags } from "./meta.ts" with { type: "macro" };
295
+
296
+ export const Head = () => {
297
+ const headTags = extractMetaTags("https://example.com");
298
+
299
+ if (headTags.title !== "Example Domain") {
300
+ throw new Error("Expected title to be 'Example Domain'");
301
+ }
302
+
303
+ return (
304
+ <head>
305
+ <title>{headTags.title}</title>
306
+ <meta name="viewport" content={headTags.viewport} />
307
+ </head>
308
+ );
309
+ };
310
+ ```
311
+
312
+ ```jsx output
313
+ export const Head = () => {
314
+ const headTags = {
315
+ title: "Example Domain",
316
+ viewport: "width=device-width, initial-scale=1",
317
+ };
318
+
319
+ return (
320
+ <head>
321
+ <title>{headTags.title}</title>
322
+ <meta name="viewport" content={headTags.viewport} />
323
+ </head>
324
+ );
325
+ };
326
+ ```
327
+
328
+ </CodeGroup>