bun-types 1.3.2-canary.20251105T140650 → 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 (370) hide show
  1. package/bun.d.ts +102 -6
  2. package/docs/README.md +28 -0
  3. package/docs/bundler/bytecode.mdx +465 -0
  4. package/docs/bundler/{css.md → css.mdx} +97 -101
  5. package/docs/bundler/esbuild.mdx +253 -0
  6. package/docs/bundler/{executables.md → executables.mdx} +130 -221
  7. package/docs/bundler/fullstack.mdx +1064 -0
  8. package/docs/bundler/{hmr.md → hot-reloading.mdx} +74 -79
  9. package/docs/bundler/{html.md → html-static.mdx} +148 -111
  10. package/docs/bundler/index.mdx +1499 -0
  11. package/docs/bundler/loaders.mdx +356 -0
  12. package/docs/bundler/{macros.md → macros.mdx} +83 -84
  13. package/docs/bundler/minifier.mdx +1306 -0
  14. package/docs/bundler/plugins.mdx +411 -0
  15. package/docs/feedback.mdx +85 -0
  16. package/docs/guides/binary/{arraybuffer-to-array.md → arraybuffer-to-array.mdx} +3 -1
  17. package/docs/guides/binary/{arraybuffer-to-blob.md → arraybuffer-to-blob.mdx} +3 -1
  18. package/docs/guides/binary/{arraybuffer-to-buffer.md → arraybuffer-to-buffer.mdx} +3 -1
  19. package/docs/guides/binary/{arraybuffer-to-string.md → arraybuffer-to-string.mdx} +3 -1
  20. package/docs/guides/binary/{arraybuffer-to-typedarray.md → arraybuffer-to-typedarray.mdx} +3 -1
  21. package/docs/guides/binary/{blob-to-arraybuffer.md → blob-to-arraybuffer.mdx} +3 -1
  22. package/docs/guides/binary/{blob-to-dataview.md → blob-to-dataview.mdx} +3 -1
  23. package/docs/guides/binary/{blob-to-stream.md → blob-to-stream.mdx} +3 -1
  24. package/docs/guides/binary/{blob-to-string.md → blob-to-string.mdx} +3 -1
  25. package/docs/guides/binary/{blob-to-typedarray.md → blob-to-typedarray.mdx} +3 -1
  26. package/docs/guides/binary/{buffer-to-arraybuffer.md → buffer-to-arraybuffer.mdx} +3 -1
  27. package/docs/guides/binary/{buffer-to-blob.md → buffer-to-blob.mdx} +3 -1
  28. package/docs/guides/binary/{buffer-to-readablestream.md → buffer-to-readablestream.mdx} +3 -1
  29. package/docs/guides/binary/{buffer-to-string.md → buffer-to-string.mdx} +3 -1
  30. package/docs/guides/binary/{buffer-to-typedarray.md → buffer-to-typedarray.mdx} +3 -1
  31. package/docs/guides/binary/{dataview-to-string.md → dataview-to-string.mdx} +3 -1
  32. package/docs/guides/binary/{typedarray-to-arraybuffer.md → typedarray-to-arraybuffer.mdx} +3 -1
  33. package/docs/guides/binary/{typedarray-to-blob.md → typedarray-to-blob.mdx} +3 -1
  34. package/docs/guides/binary/{typedarray-to-buffer.md → typedarray-to-buffer.mdx} +3 -1
  35. package/docs/guides/binary/{typedarray-to-dataview.md → typedarray-to-dataview.mdx} +3 -1
  36. package/docs/guides/binary/{typedarray-to-readablestream.md → typedarray-to-readablestream.mdx} +3 -1
  37. package/docs/guides/binary/{typedarray-to-string.md → typedarray-to-string.mdx} +3 -1
  38. package/docs/guides/deployment/aws-lambda.mdx +204 -0
  39. package/docs/guides/deployment/digital-ocean.mdx +161 -0
  40. package/docs/guides/deployment/google-cloud-run.mdx +197 -0
  41. package/docs/guides/deployment/{railway.md → railway.mdx} +42 -54
  42. package/docs/guides/deployment/render.mdx +82 -0
  43. package/docs/guides/deployment/vercel.mdx +99 -0
  44. package/docs/guides/ecosystem/{astro.md → astro.mdx} +16 -6
  45. package/docs/guides/ecosystem/{discordjs.md → discordjs.mdx} +21 -18
  46. package/docs/guides/ecosystem/{docker.md → docker.mdx} +25 -14
  47. package/docs/guides/ecosystem/{drizzle.md → drizzle.mdx} +32 -22
  48. package/docs/guides/ecosystem/{edgedb.md → edgedb.mdx} +63 -34
  49. package/docs/guides/ecosystem/{elysia.md → elysia.mdx} +11 -11
  50. package/docs/guides/ecosystem/{express.md → express.mdx} +12 -9
  51. package/docs/guides/ecosystem/{hono.md → hono.mdx} +16 -8
  52. package/docs/guides/ecosystem/{mongoose.md → mongoose.mdx} +26 -21
  53. package/docs/guides/ecosystem/{neon-drizzle.md → neon-drizzle.mdx} +49 -35
  54. package/docs/guides/ecosystem/{neon-serverless-postgres.md → neon-serverless-postgres.mdx} +16 -11
  55. package/docs/guides/ecosystem/{nextjs.md → nextjs.mdx} +18 -10
  56. package/docs/guides/ecosystem/{nuxt.md → nuxt.mdx} +45 -11
  57. package/docs/guides/ecosystem/{pm2.md → pm2.mdx} +7 -9
  58. package/docs/guides/ecosystem/prisma-postgres.mdx +169 -0
  59. package/docs/guides/ecosystem/prisma.mdx +164 -0
  60. package/docs/guides/ecosystem/{qwik.md → qwik.mdx} +18 -11
  61. package/docs/guides/ecosystem/{react.md → react.mdx} +9 -7
  62. package/docs/guides/ecosystem/{remix.md → remix.mdx} +45 -26
  63. package/docs/guides/ecosystem/{sentry.md → sentry.mdx} +7 -5
  64. package/docs/guides/ecosystem/{solidstart.md → solidstart.mdx} +21 -13
  65. package/docs/guides/ecosystem/{ssr-react.md → ssr-react.mdx} +9 -11
  66. package/docs/guides/ecosystem/{stric.md → stric.mdx} +16 -17
  67. package/docs/guides/ecosystem/{sveltekit.md → sveltekit.mdx} +42 -29
  68. package/docs/guides/ecosystem/{systemd.md → systemd.mdx} +27 -26
  69. package/docs/guides/ecosystem/upstash.mdx +87 -0
  70. package/docs/guides/ecosystem/{vite.md → vite.mdx} +20 -14
  71. package/docs/guides/html-rewriter/{extract-links.md → extract-links.mdx} +11 -7
  72. package/docs/guides/html-rewriter/{extract-social-meta.md → extract-social-meta.mdx} +6 -2
  73. package/docs/guides/http/{cluster.md → cluster.mdx} +10 -7
  74. package/docs/guides/http/{fetch-unix.md → fetch-unix.mdx} +5 -3
  75. package/docs/guides/http/{fetch.md → fetch.mdx} +5 -3
  76. package/docs/guides/http/{file-uploads.md → file-uploads.mdx} +18 -15
  77. package/docs/guides/http/{hot.md → hot.mdx} +9 -7
  78. package/docs/guides/http/{proxy.md → proxy.mdx} +7 -5
  79. package/docs/guides/http/{server.md → server.mdx} +4 -2
  80. package/docs/guides/http/{simple.md → simple.mdx} +4 -2
  81. package/docs/guides/http/{stream-file.md → stream-file.mdx} +6 -4
  82. package/docs/guides/http/{stream-iterator.md → stream-iterator.mdx} +5 -3
  83. package/docs/guides/http/{stream-node-streams-in-bun.md → stream-node-streams-in-bun.mdx} +4 -2
  84. package/docs/guides/http/{tls.md → tls.mdx} +5 -3
  85. package/docs/guides/index.mdx +10 -0
  86. package/docs/guides/install/{add-dev.md → add-dev.mdx} +8 -6
  87. package/docs/guides/install/add-git.mdx +38 -0
  88. package/docs/guides/install/{add-optional.md → add-optional.mdx} +7 -5
  89. package/docs/guides/install/{add-peer.md → add-peer.mdx} +13 -11
  90. package/docs/guides/install/{add-tarball.md → add-tarball.mdx} +7 -5
  91. package/docs/guides/install/{add.md → add.mdx} +12 -10
  92. package/docs/guides/install/{azure-artifacts.md → azure-artifacts.mdx} +15 -12
  93. package/docs/guides/{runtime/cicd.md → install/cicd.mdx} +13 -13
  94. package/docs/guides/install/{custom-registry.md → custom-registry.mdx} +6 -4
  95. package/docs/guides/install/{from-npm-install-to-bun-install.md → from-npm-install-to-bun-install.mdx} +50 -34
  96. package/docs/guides/install/{git-diff-bun-lockfile.md → git-diff-bun-lockfile.mdx} +14 -11
  97. package/docs/guides/install/{jfrog-artifactory.md → jfrog-artifactory.mdx} +4 -4
  98. package/docs/guides/install/{npm-alias.md → npm-alias.mdx} +6 -4
  99. package/docs/guides/install/{registry-scope.md → registry-scope.mdx} +6 -4
  100. package/docs/guides/install/{trusted.md → trusted.mdx} +22 -18
  101. package/docs/guides/install/{workspaces.md → workspaces.mdx} +13 -13
  102. package/docs/guides/install/{yarnlock.md → yarnlock.mdx} +15 -9
  103. package/docs/guides/process/{argv.md → argv.mdx} +17 -9
  104. package/docs/guides/process/{ctrl-c.md → ctrl-c.mdx} +4 -2
  105. package/docs/guides/process/{ipc.md → ipc.mdx} +13 -10
  106. package/docs/guides/process/{nanoseconds.md → nanoseconds.mdx} +3 -1
  107. package/docs/guides/process/{os-signals.md → os-signals.mdx} +3 -1
  108. package/docs/guides/process/{spawn-stderr.md → spawn-stderr.mdx} +4 -1
  109. package/docs/guides/process/{spawn-stdout.md → spawn-stdout.mdx} +3 -1
  110. package/docs/guides/process/{spawn.md → spawn.mdx} +3 -1
  111. package/docs/guides/process/{stdin.md → stdin.mdx} +15 -7
  112. package/docs/guides/read-file/{arraybuffer.md → arraybuffer.mdx} +5 -3
  113. package/docs/guides/read-file/{buffer.md → buffer.mdx} +4 -2
  114. package/docs/guides/read-file/{exists.md → exists.mdx} +4 -2
  115. package/docs/guides/read-file/{json.md → json.mdx} +4 -2
  116. package/docs/guides/read-file/{mime.md → mime.mdx} +3 -1
  117. package/docs/guides/read-file/{stream.md → stream.mdx} +3 -1
  118. package/docs/guides/read-file/{string.md → string.mdx} +3 -1
  119. package/docs/guides/read-file/{uint8array.md → uint8array.mdx} +3 -1
  120. package/docs/guides/read-file/{watch.md → watch.mdx} +6 -8
  121. package/docs/guides/runtime/{build-time-constants.md → build-time-constants.mdx} +24 -22
  122. package/docs/guides/{install/cicd.md → runtime/cicd.mdx} +11 -7
  123. package/docs/guides/runtime/{codesign-macos-executable.md → codesign-macos-executable.mdx} +14 -9
  124. package/docs/guides/runtime/{define-constant.md → define-constant.mdx} +20 -16
  125. package/docs/guides/runtime/{delete-directory.md → delete-directory.mdx} +5 -3
  126. package/docs/guides/runtime/{delete-file.md → delete-file.mdx} +4 -2
  127. package/docs/guides/runtime/{heap-snapshot.md → heap-snapshot.mdx} +5 -3
  128. package/docs/guides/runtime/{import-html.md → import-html.mdx} +4 -2
  129. package/docs/guides/runtime/{import-json.md → import-json.mdx} +6 -4
  130. package/docs/guides/runtime/{import-toml.md → import-toml.mdx} +5 -3
  131. package/docs/guides/runtime/{import-yaml.md → import-yaml.mdx} +9 -7
  132. package/docs/guides/runtime/{read-env.md → read-env.mdx} +10 -5
  133. package/docs/guides/runtime/{set-env.md → set-env.mdx} +14 -10
  134. package/docs/guides/runtime/{shell.md → shell.mdx} +6 -4
  135. package/docs/guides/runtime/{timezone.md → timezone.mdx} +10 -7
  136. package/docs/guides/runtime/{tsconfig-paths.md → tsconfig-paths.mdx} +5 -3
  137. package/docs/guides/runtime/{typescript.md → typescript.mdx} +8 -6
  138. package/docs/guides/runtime/{vscode-debugger.md → vscode-debugger.mdx} +12 -11
  139. package/docs/guides/runtime/{web-debugger.md → web-debugger.mdx} +39 -18
  140. package/docs/guides/streams/{node-readable-to-arraybuffer.md → node-readable-to-arraybuffer.mdx} +3 -1
  141. package/docs/guides/streams/{node-readable-to-blob.md → node-readable-to-blob.mdx} +3 -1
  142. package/docs/guides/streams/{node-readable-to-json.md → node-readable-to-json.mdx} +3 -1
  143. package/docs/guides/streams/{node-readable-to-string.md → node-readable-to-string.mdx} +3 -1
  144. package/docs/guides/streams/{node-readable-to-uint8array.md → node-readable-to-uint8array.mdx} +3 -1
  145. package/docs/guides/streams/{to-array.md → to-array.mdx} +3 -1
  146. package/docs/guides/streams/{to-arraybuffer.md → to-arraybuffer.mdx} +3 -1
  147. package/docs/guides/streams/{to-blob.md → to-blob.mdx} +3 -1
  148. package/docs/guides/streams/{to-buffer.md → to-buffer.mdx} +3 -1
  149. package/docs/guides/streams/{to-json.md → to-json.mdx} +4 -2
  150. package/docs/guides/streams/{to-string.md → to-string.mdx} +3 -1
  151. package/docs/guides/streams/{to-typedarray.md → to-typedarray.mdx} +3 -1
  152. package/docs/guides/test/{bail.md → bail.mdx} +7 -5
  153. package/docs/guides/test/{coverage-threshold.md → coverage-threshold.mdx} +14 -7
  154. package/docs/guides/test/{coverage.md → coverage.mdx} +9 -4
  155. package/docs/guides/test/{happy-dom.md → happy-dom.mdx} +13 -8
  156. package/docs/guides/test/{migrate-from-jest.md → migrate-from-jest.mdx} +19 -37
  157. package/docs/guides/test/{mock-clock.md → mock-clock.mdx} +3 -1
  158. package/docs/guides/test/{mock-functions.md → mock-functions.mdx} +7 -5
  159. package/docs/guides/test/{rerun-each.md → rerun-each.mdx} +5 -3
  160. package/docs/guides/test/{run-tests.md → run-tests.mdx} +16 -11
  161. package/docs/guides/test/{skip-tests.md → skip-tests.mdx} +8 -4
  162. package/docs/guides/test/{snapshot.md → snapshot.mdx} +20 -13
  163. package/docs/guides/test/spy-on.mdx +49 -0
  164. package/docs/guides/test/{svelte-test.md → svelte-test.mdx} +16 -23
  165. package/docs/guides/test/{testing-library.md → testing-library.mdx} +18 -17
  166. package/docs/guides/test/{timeout.md → timeout.mdx} +5 -3
  167. package/docs/guides/test/{todo-tests.md → todo-tests.mdx} +13 -6
  168. package/docs/guides/test/{update-snapshots.md → update-snapshots.mdx} +9 -6
  169. package/docs/guides/test/{watch-mode.md → watch-mode.mdx} +9 -4
  170. package/docs/guides/util/{base64.md → base64.mdx} +3 -1
  171. package/docs/guides/util/{deep-equals.md → deep-equals.mdx} +5 -3
  172. package/docs/guides/util/{deflate.md → deflate.mdx} +3 -1
  173. package/docs/guides/util/{detect-bun.md → detect-bun.mdx} +3 -1
  174. package/docs/guides/util/{entrypoint.md → entrypoint.mdx} +4 -2
  175. package/docs/guides/util/{escape-html.md → escape-html.mdx} +3 -1
  176. package/docs/guides/util/{file-url-to-path.md → file-url-to-path.mdx} +3 -1
  177. package/docs/guides/util/{gzip.md → gzip.mdx} +3 -1
  178. package/docs/guides/util/{hash-a-password.md → hash-a-password.mdx} +3 -1
  179. package/docs/guides/util/{import-meta-dir.md → import-meta-dir.mdx} +4 -2
  180. package/docs/guides/util/{import-meta-file.md → import-meta-file.mdx} +4 -2
  181. package/docs/guides/util/{import-meta-path.md → import-meta-path.mdx} +4 -2
  182. package/docs/guides/util/{javascript-uuid.md → javascript-uuid.mdx} +3 -1
  183. package/docs/guides/util/{main.md → main.mdx} +19 -8
  184. package/docs/guides/util/{path-to-file-url.md → path-to-file-url.mdx} +3 -1
  185. package/docs/guides/util/{sleep.md → sleep.mdx} +3 -1
  186. package/docs/guides/util/{version.md → version.mdx} +6 -4
  187. package/docs/guides/util/{which-path-to-executable-bin.md → which-path-to-executable-bin.mdx} +4 -2
  188. package/docs/guides/websocket/{compression.md → compression.mdx} +5 -3
  189. package/docs/guides/websocket/{context.md → context.mdx} +9 -12
  190. package/docs/guides/websocket/{pubsub.md → pubsub.mdx} +5 -6
  191. package/docs/guides/websocket/{simple.md → simple.mdx} +5 -3
  192. package/docs/guides/write-file/{append.md → append.mdx} +3 -1
  193. package/docs/guides/write-file/{basic.md → basic.mdx} +3 -1
  194. package/docs/guides/write-file/{blob.md → blob.mdx} +3 -1
  195. package/docs/guides/write-file/{cat.md → cat.mdx} +4 -2
  196. package/docs/guides/write-file/{file-cp.md → file-cp.mdx} +3 -1
  197. package/docs/guides/write-file/{filesink.md → filesink.mdx} +3 -1
  198. package/docs/guides/write-file/{response.md → response.mdx} +3 -1
  199. package/docs/guides/write-file/{stdout.md → stdout.mdx} +3 -1
  200. package/docs/guides/write-file/{stream.md → stream.mdx} +3 -1
  201. package/docs/guides/write-file/{unlink.md → unlink.mdx} +3 -1
  202. package/docs/{index.md → index.mdx} +97 -41
  203. package/docs/installation.mdx +365 -0
  204. package/docs/{cli/bunx.md → pm/bunx.mdx} +25 -31
  205. package/docs/{install/catalogs.md → pm/catalogs.mdx} +18 -24
  206. package/docs/{cli/add.md → pm/cli/add.mdx} +61 -55
  207. package/docs/{install/audit.md → pm/cli/audit.mdx} +11 -8
  208. package/docs/{cli/install.md → pm/cli/install.mdx} +208 -81
  209. package/docs/{cli/link.md → pm/cli/link.mdx} +27 -19
  210. package/docs/{cli/outdated.md → pm/cli/outdated.mdx} +119 -12
  211. package/docs/{install/patch.md → pm/cli/patch.mdx} +25 -15
  212. package/docs/{cli/pm.md → pm/cli/pm.mdx} +96 -62
  213. package/docs/{cli/publish.md → pm/cli/publish.mdx} +38 -35
  214. package/docs/pm/cli/remove.mdx +16 -0
  215. package/docs/{cli/update.md → pm/cli/update.mdx} +26 -15
  216. package/docs/{cli/why.md → pm/cli/why.mdx} +27 -10
  217. package/docs/{cli/filter.md → pm/filter.mdx} +26 -14
  218. package/docs/{install/cache.md → pm/global-cache.mdx} +25 -12
  219. package/docs/{install/isolated.md → pm/isolated-installs.mdx} +32 -24
  220. package/docs/{install/lifecycle.md → pm/lifecycle.mdx} +20 -9
  221. package/docs/{install/lockfile.md → pm/lockfile.mdx} +18 -12
  222. package/docs/{install/npmrc.md → pm/npmrc.mdx} +30 -24
  223. package/docs/{install/overrides.md → pm/overrides.mdx} +47 -37
  224. package/docs/{install/registries.md → pm/scopes-registries.mdx} +8 -3
  225. package/docs/{install/security-scanner-api.md → pm/security-scanner-api.mdx} +22 -8
  226. package/docs/{install/workspaces.md → pm/workspaces.mdx} +17 -22
  227. package/docs/project/{benchmarking.md → benchmarking.mdx} +26 -11
  228. package/docs/project/{bindgen.md → bindgen.mdx} +12 -14
  229. package/docs/project/{building-windows.md → building-windows.mdx} +18 -47
  230. package/docs/project/{contributing.md → contributing.mdx} +56 -60
  231. package/docs/project/feedback.mdx +20 -0
  232. package/docs/project/license.mdx +78 -0
  233. package/docs/project/roadmap.mdx +8 -0
  234. package/docs/quickstart.mdx +240 -0
  235. package/docs/runtime/{autoimport.md → auto-install.mdx} +34 -31
  236. package/docs/{api/binary-data.md → runtime/binary-data.mdx} +77 -269
  237. package/docs/runtime/bun-apis.mdx +59 -0
  238. package/docs/runtime/{bunfig.md → bunfig.mdx} +95 -225
  239. package/docs/{api/cc.md → runtime/c-compiler.mdx} +16 -9
  240. package/docs/{api/spawn.md → runtime/child-process.mdx} +69 -119
  241. package/docs/{api/color.md → runtime/color.mdx} +6 -1
  242. package/docs/{api/console.md → runtime/console.mdx} +17 -7
  243. package/docs/{api/cookie.md → runtime/cookies.mdx} +24 -19
  244. package/docs/runtime/{debugger.md → debugger.mdx} +61 -51
  245. package/docs/runtime/environment-variables.mdx +214 -0
  246. package/docs/{api/ffi.md → runtime/ffi.mdx} +53 -45
  247. package/docs/{api/file-io.md → runtime/file-io.mdx} +41 -101
  248. package/docs/{api/file-system-router.md → runtime/file-system-router.mdx} +8 -2
  249. package/docs/{bundler/loaders.md → runtime/file-types.mdx} +56 -112
  250. package/docs/{api/glob.md → runtime/glob.mdx} +4 -1
  251. package/docs/runtime/globals.mdx +72 -0
  252. package/docs/{api/hashing.md → runtime/hashing.mdx} +18 -22
  253. package/docs/{api/html-rewriter.md → runtime/html-rewriter.mdx} +36 -30
  254. package/docs/runtime/http/cookies.mdx +79 -0
  255. package/docs/runtime/http/error-handling.mdx +40 -0
  256. package/docs/runtime/http/metrics.mdx +36 -0
  257. package/docs/runtime/http/routing.mdx +289 -0
  258. package/docs/runtime/http/server.mdx +647 -0
  259. package/docs/runtime/http/tls.mdx +101 -0
  260. package/docs/{api/websockets.md → runtime/http/websockets.mdx} +62 -231
  261. package/docs/{cli/run.md → runtime/index.mdx} +70 -108
  262. package/docs/runtime/jsx.mdx +115 -0
  263. package/docs/runtime/{modules.md → module-resolution.mdx} +83 -61
  264. package/docs/{api/dns.md → runtime/networking/dns.mdx} +23 -22
  265. package/docs/{api/fetch.md → runtime/networking/fetch.mdx} +10 -5
  266. package/docs/{api/tcp.md → runtime/networking/tcp.mdx} +59 -41
  267. package/docs/{api/udp.md → runtime/networking/udp.mdx} +14 -8
  268. package/docs/{api/node-api.md → runtime/node-api.mdx} +5 -0
  269. package/docs/runtime/{nodejs-apis.md → nodejs-compat.mdx} +9 -4
  270. package/docs/{bundler/plugins.md → runtime/plugins.mdx} +17 -61
  271. package/docs/{api/redis.md → runtime/redis.mdx} +65 -80
  272. package/docs/{api/s3.md → runtime/s3.mdx} +79 -66
  273. package/docs/{api/secrets.md → runtime/secrets.mdx} +27 -10
  274. package/docs/{api/semver.md → runtime/semver.mdx} +9 -4
  275. package/docs/runtime/{shell.md → shell.mdx} +52 -20
  276. package/docs/{api/sql.md → runtime/sql.mdx} +129 -99
  277. package/docs/{api/sqlite.md → runtime/sqlite.mdx} +162 -157
  278. package/docs/{api/streams.md → runtime/streams.mdx} +33 -31
  279. package/docs/{cli/bun-create.md → runtime/templating/create.mdx} +74 -143
  280. package/docs/{cli/init.md → runtime/templating/init.mdx} +24 -51
  281. package/docs/{api/transpiler.md → runtime/transpiler.mdx} +50 -38
  282. package/docs/{typescript.md → runtime/typescript.mdx} +7 -2
  283. package/docs/{api/utils.md → runtime/utils.mdx} +81 -50
  284. package/docs/runtime/{hot.md → watch-mode.mdx} +53 -37
  285. package/docs/runtime/web-apis.mdx +29 -0
  286. package/docs/{api/workers.md → runtime/workers.mdx} +28 -22
  287. package/docs/{api/yaml.md → runtime/yaml.mdx} +33 -232
  288. package/docs/snippets/cli/add.mdx +166 -0
  289. package/docs/snippets/cli/build.mdx +196 -0
  290. package/docs/snippets/cli/feedback.mdx +17 -0
  291. package/docs/snippets/cli/init.mdx +84 -0
  292. package/docs/snippets/cli/install.mdx +173 -0
  293. package/docs/snippets/cli/link.mdx +163 -0
  294. package/docs/snippets/cli/outdated.mdx +140 -0
  295. package/docs/snippets/cli/patch.mdx +171 -0
  296. package/docs/snippets/cli/publish.mdx +198 -0
  297. package/docs/snippets/cli/remove.mdx +146 -0
  298. package/docs/snippets/cli/run.mdx +293 -0
  299. package/docs/snippets/cli/test.mdx +100 -0
  300. package/docs/snippets/cli/update.mdx +144 -0
  301. package/docs/snippets/product-card.mdx +32 -0
  302. package/docs/snippets/product-tiles.mdx +94 -0
  303. package/docs/test/code-coverage.mdx +409 -0
  304. package/docs/test/configuration.mdx +467 -0
  305. package/docs/test/{time.md → dates-times.mdx} +23 -20
  306. package/docs/test/{discovery.md → discovery.mdx} +15 -10
  307. package/docs/test/dom.mdx +226 -0
  308. package/docs/{cli/test.md → test/index.mdx} +77 -94
  309. package/docs/test/lifecycle.mdx +348 -0
  310. package/docs/test/mocks.mdx +637 -0
  311. package/docs/test/{reporters.md → reporters.mdx} +15 -15
  312. package/docs/test/runtime-behavior.mdx +342 -0
  313. package/docs/test/snapshots.mdx +434 -0
  314. package/docs/test/writing-tests.mdx +635 -0
  315. package/docs/typescript.mdx +54 -0
  316. package/package.json +8 -6
  317. package/test.d.ts +2 -2
  318. package/docs/api/file.md +0 -19
  319. package/docs/api/globals.md +0 -387
  320. package/docs/api/http.md +0 -1408
  321. package/docs/api/import-meta.md +0 -69
  322. package/docs/benchmarks.md +0 -120
  323. package/docs/bun-flavored-toml.md +0 -42
  324. package/docs/bundler/css_modules.md +0 -145
  325. package/docs/bundler/fullstack.md +0 -418
  326. package/docs/bundler/index.md +0 -1735
  327. package/docs/bundler/intro.md +0 -75
  328. package/docs/bundler/vs-esbuild.md +0 -1127
  329. package/docs/cli/bun-completions.md +0 -3
  330. package/docs/cli/bun-install.md +0 -349
  331. package/docs/cli/bun-upgrade.md +0 -39
  332. package/docs/cli/info.md +0 -65
  333. package/docs/cli/patch-commit.md +0 -11
  334. package/docs/cli/remove.md +0 -7
  335. package/docs/cli/unlink.md +0 -9
  336. package/docs/contributing/upgrading-webkit.md +0 -57
  337. package/docs/ecosystem/elysia.md +0 -24
  338. package/docs/ecosystem/express.md +0 -37
  339. package/docs/ecosystem/hono.md +0 -18
  340. package/docs/ecosystem/react.md +0 -65
  341. package/docs/ecosystem/stric.md +0 -38
  342. package/docs/guides/ecosystem/prisma.md +0 -141
  343. package/docs/guides/ecosystem/render.md +0 -79
  344. package/docs/guides/install/add-git.md +0 -36
  345. package/docs/guides/test/spy-on.md +0 -46
  346. package/docs/install/index.md +0 -248
  347. package/docs/installation.md +0 -327
  348. package/docs/project/asan.md +0 -124
  349. package/docs/project/internals/build-process-for-ci.md +0 -75
  350. package/docs/project/licensing.md +0 -73
  351. package/docs/project/roadmap.md +0 -87
  352. package/docs/quickstart.md +0 -157
  353. package/docs/runtime/bun-apis.md +0 -207
  354. package/docs/runtime/env.md +0 -253
  355. package/docs/runtime/index.md +0 -312
  356. package/docs/runtime/jsx.md +0 -385
  357. package/docs/runtime/loaders.md +0 -130
  358. package/docs/runtime/plugins.md +0 -561
  359. package/docs/runtime/typescript.md +0 -139
  360. package/docs/runtime/web-apis.md +0 -128
  361. package/docs/test/configuration.md +0 -154
  362. package/docs/test/coverage.md +0 -142
  363. package/docs/test/dom.md +0 -75
  364. package/docs/test/examples/concurrent-test-glob.md +0 -132
  365. package/docs/test/hot.md +0 -15
  366. package/docs/test/lifecycle.md +0 -81
  367. package/docs/test/mocks.md +0 -313
  368. package/docs/test/runtime-behavior.md +0 -95
  369. package/docs/test/snapshots.md +0 -68
  370. package/docs/test/writing.md +0 -825
@@ -1,1735 +0,0 @@
1
- Bun's fast native bundler can be used via the `bun build` CLI command or the `Bun.build()` JavaScript API.
2
-
3
- {% codetabs group="a" %}
4
-
5
- ```ts#JavaScript
6
- await Bun.build({
7
- entrypoints: ['./index.tsx'],
8
- outdir: './build',
9
- });
10
- ```
11
-
12
- ```sh#CLI
13
- $ bun build ./index.tsx --outdir ./build
14
- ```
15
-
16
- {% /codetabs %}
17
-
18
- It's fast. The numbers below represent performance on esbuild's [three.js benchmark](https://github.com/oven-sh/bun/tree/main/bench/bundle).
19
-
20
- {% image src="/images/bundler-speed.png" caption="Bundling 10 copies of three.js from scratch, with sourcemaps and minification" /%}
21
-
22
- ## Why bundle?
23
-
24
- The bundler is a key piece of infrastructure in the JavaScript ecosystem. As a brief overview of why bundling is so important:
25
-
26
- - **Reducing HTTP requests.** A single package in `node_modules` may consist of hundreds of files, and large applications may have dozens of such dependencies. Loading each of these files with a separate HTTP request becomes untenable very quickly, so bundlers are used to convert our application source code into a smaller number of self-contained "bundles" that can be loaded with a single request.
27
- - **Code transforms.** Modern apps are commonly built with languages or tools like TypeScript, JSX, and CSS modules, all of which must be converted into plain JavaScript and CSS before they can be consumed by a browser. The bundler is the natural place to configure these transformations.
28
- - **Framework features.** Frameworks rely on bundler plugins & code transformations to implement common patterns like file-system routing, client-server code co-location (think `getServerSideProps` or Remix loaders), and server components.
29
- - **Full-stack Applications.** Bun's bundler can handle both server and client code in a single command, enabling optimized production builds and single-file executables. With build-time HTML imports, you can bundle your entire application — frontend assets and backend server — into a single deployable unit.
30
-
31
- Let's jump into the bundler API.
32
-
33
- {% callout %}
34
- Note that the Bun bundler is not intended to replace `tsc` for typechecking or generating type declarations.
35
- {% /callout %}
36
-
37
- ## Basic example
38
-
39
- Let's build our first bundle. You have the following two files, which implement a simple client-side rendered React app.
40
-
41
- {% codetabs %}
42
-
43
- ```tsx#./index.tsx
44
- import * as ReactDOM from 'react-dom/client';
45
- import {Component} from "./Component"
46
-
47
- const root = ReactDOM.createRoot(document.getElementById('root')!);
48
- root.render(<Component message="Sup!" />)
49
- ```
50
-
51
- ```tsx#./Component.tsx
52
- export function Component(props: {message: string}) {
53
- return <p>{props.message}</p>
54
- }
55
- ```
56
-
57
- {% /codetabs %}
58
-
59
- Here, `index.tsx` is the "entrypoint" to our application. Commonly, this will be a script that performs some _side effect_, like starting a server or—in this case—initializing a React root. Because we're using TypeScript & JSX, we need to bundle our code before it can be sent to the browser.
60
-
61
- To create our bundle:
62
-
63
- {% codetabs group="a" %}
64
-
65
- ```ts#JavaScript
66
- await Bun.build({
67
- entrypoints: ['./index.tsx'],
68
- outdir: './out',
69
- })
70
- ```
71
-
72
- ```bash#CLI
73
- $ bun build ./index.tsx --outdir ./out
74
- ```
75
-
76
- {% /codetabs %}
77
-
78
- For each file specified in `entrypoints`, Bun will generate a new bundle. This bundle will be written to disk in the `./out` directory (as resolved from the current working directory). After running the build, the file system looks like this:
79
-
80
- ```ts
81
- .
82
- ├── index.tsx
83
- ├── Component.tsx
84
- └── out
85
- └── index.js
86
- ```
87
-
88
- The contents of `out/index.js` will look something like this:
89
-
90
- ```js#out/index.js
91
- // ...
92
- // ~20k lines of code
93
- // including the contents of `react-dom/client` and all its dependencies
94
- // this is where the $jsxDEV and $createRoot functions are defined
95
-
96
-
97
- // Component.tsx
98
- function Component(props) {
99
- return $jsxDEV("p", {
100
- children: props.message
101
- }, undefined, false, undefined, this);
102
- }
103
-
104
- // index.tsx
105
- var rootNode = document.getElementById("root");
106
- var root = $createRoot(rootNode);
107
- root.render($jsxDEV(Component, {
108
- message: "Sup!"
109
- }, undefined, false, undefined, this));
110
- ```
111
-
112
- {% details summary="Tutorial: Run this file in your browser" %}
113
- We can load this file in the browser to see our app in action. Create an `index.html` file in the `out` directory:
114
-
115
- ```bash
116
- $ touch out/index.html
117
- ```
118
-
119
- Then paste the following contents into it:
120
-
121
- ```html
122
- <html>
123
- <body>
124
- <div id="root"></div>
125
- <script type="module" src="/index.js"></script>
126
- </body>
127
- </html>
128
- ```
129
-
130
- Then spin up a static file server serving the `out` directory:
131
-
132
- ```bash
133
- $ bunx serve out
134
- ```
135
-
136
- Visit `http://localhost:5000` to see your bundled app in action.
137
-
138
- {% /details %}
139
-
140
- ## Watch mode
141
-
142
- Like the runtime and test runner, the bundler supports watch mode natively.
143
-
144
- ```sh
145
- $ bun build ./index.tsx --outdir ./out --watch
146
- ```
147
-
148
- ## Content types
149
-
150
- Like the Bun runtime, the bundler supports an array of file types out of the box. The following table breaks down the bundler's set of standard "loaders". Refer to [Bundler > File types](https://bun.com/docs/runtime/loaders) for full documentation.
151
-
152
- {% table %}
153
-
154
- - Extensions
155
- - Details
156
-
157
- ---
158
-
159
- - `.js` `.jsx`, `.cjs` `.mjs` `.mts` `.cts` `.ts` `.tsx`
160
- - Uses Bun's built-in transpiler to parse the file and transpile TypeScript/JSX syntax to vanilla JavaScript. The bundler executes a set of default transforms including dead code elimination and tree shaking. At the moment Bun does not attempt to down-convert syntax; if you use recently ECMAScript syntax, that will be reflected in the bundled code.
161
-
162
- ---
163
-
164
- - `.json`
165
- - JSON files are parsed and inlined into the bundle as a JavaScript object.
166
-
167
- ```ts
168
- import pkg from "./package.json";
169
- pkg.name; // => "my-package"
170
- ```
171
-
172
- ---
173
-
174
- - `.toml`
175
- - TOML files are parsed and inlined into the bundle as a JavaScript object.
176
-
177
- ```ts
178
- import config from "./bunfig.toml";
179
- config.logLevel; // => "debug"
180
- ```
181
-
182
- ---
183
-
184
- - `.txt`
185
- - The contents of the text file are read and inlined into the bundle as a string.
186
-
187
- ```ts
188
- import contents from "./file.txt";
189
- console.log(contents); // => "Hello, world!"
190
- ```
191
-
192
- ---
193
-
194
- - `.node` `.wasm`
195
- - These files are supported by the Bun runtime, but during bundling they are treated as [assets](#assets).
196
-
197
- {% /table %}
198
-
199
- ### Assets
200
-
201
- If the bundler encounters an import with an unrecognized extension, it treats the imported file as an _external file_. The referenced file is copied as-is into `outdir`, and the import is resolved as a _path_ to the file.
202
-
203
- {% codetabs %}
204
-
205
- ```ts#Input
206
- // bundle entrypoint
207
- import logo from "./logo.svg";
208
- console.log(logo);
209
- ```
210
-
211
- ```ts#Output
212
- // bundled output
213
- var logo = "./logo-ab237dfe.svg";
214
- console.log(logo);
215
- ```
216
-
217
- {% /codetabs %}
218
-
219
- {% callout %}
220
- The exact behavior of the file loader is also impacted by [`naming`](#naming) and [`publicPath`](#publicpath).
221
- {% /callout %}
222
-
223
- Refer to the [Bundler > Loaders](https://bun.com/docs/bundler/loaders#file) page for more complete documentation on the file loader.
224
-
225
- ### Plugins
226
-
227
- The behavior described in this table can be overridden or extended with [plugins](https://bun.com/docs/bundler/plugins). Refer to the [Bundler > Loaders](https://bun.com/docs/bundler/plugins) page for complete documentation.
228
-
229
- ## API
230
-
231
- ### `entrypoints`
232
-
233
- **Required.** An array of paths corresponding to the entrypoints of our application. One bundle will be generated for each entrypoint.
234
-
235
- {% codetabs group="a" %}
236
-
237
- ```ts#JavaScript
238
- const result = await Bun.build({
239
- entrypoints: ["./index.ts"],
240
- });
241
- // => { success: boolean, outputs: BuildArtifact[], logs: BuildMessage[] }
242
- ```
243
-
244
- ```bash#CLI
245
- $ bun build --entrypoints ./index.ts
246
- # the bundle will be printed to stdout
247
- # <bundled code>
248
- ```
249
-
250
- {% /codetabs %}
251
-
252
- ### `outdir`
253
-
254
- The directory where output files will be written.
255
-
256
- {% codetabs group="a" %}
257
-
258
- ```ts#JavaScript
259
- const result = await Bun.build({
260
- entrypoints: ['./index.ts'],
261
- outdir: './out'
262
- });
263
- // => { success: boolean, outputs: BuildArtifact[], logs: BuildMessage[] }
264
- ```
265
-
266
- ```bash#CLI
267
- $ bun build --entrypoints ./index.ts --outdir ./out
268
- # a summary of bundled files will be printed to stdout
269
- ```
270
-
271
- {% /codetabs %}
272
-
273
- If `outdir` is not passed to the JavaScript API, bundled code will not be written to disk. Bundled files are returned in an array of `BuildArtifact` objects. These objects are Blobs with extra properties; see [Outputs](#outputs) for complete documentation.
274
-
275
- ```ts
276
- const result = await Bun.build({
277
- entrypoints: ["./index.ts"],
278
- });
279
-
280
- for (const res of result.outputs) {
281
- // Can be consumed as blobs
282
- await res.text();
283
-
284
- // Bun will set Content-Type and Etag headers
285
- new Response(res);
286
-
287
- // Can be written manually, but you should use `outdir` in this case.
288
- Bun.write(path.join("out", res.path), res);
289
- }
290
- ```
291
-
292
- When `outdir` is set, the `path` property on a `BuildArtifact` will be the absolute path to where it was written to.
293
-
294
- ### `target`
295
-
296
- The intended execution environment for the bundle.
297
-
298
- {% codetabs group="a" %}
299
-
300
- ```ts#JavaScript
301
- await Bun.build({
302
- entrypoints: ['./index.ts'],
303
- outdir: './out',
304
- target: 'browser', // default
305
- })
306
- ```
307
-
308
- ```bash#CLI
309
- $ bun build --entrypoints ./index.ts --outdir ./out --target browser
310
- ```
311
-
312
- {% /codetabs %}
313
-
314
- Depending on the target, Bun will apply different module resolution rules and optimizations.
315
-
316
- ### Module resolution
317
-
318
- Bun supports the `NODE_PATH` environment variable for additional module resolution paths:
319
-
320
- ```bash
321
- NODE_PATH=./src bun build ./entry.js --outdir ./dist
322
- ```
323
-
324
- <!-- - Module resolution. For example, when bundling for the browser, Bun will prioritize the `"browser"` export condition when resolving imports. An error will be thrown if any Node.js or Bun built-ins are imported or used, e.g. `node:fs` or `Bun.serve`. -->
325
-
326
- {% table %}
327
-
328
- ---
329
-
330
- - `browser`
331
- - _Default._ For generating bundles that are intended for execution by a browser. Prioritizes the `"browser"` export condition when resolving imports. Importing any built-in modules, like `node:events` or `node:path` will work, but calling some functions, like `fs.readFile` will not work.
332
-
333
- ---
334
-
335
- - `bun`
336
- - For generating bundles that are intended to be run by the Bun runtime. In many cases, it isn't necessary to bundle server-side code; you can directly execute the source code without modification. However, bundling your server code can reduce startup times and improve running performance. This is the target to use for building full-stack applications with build-time HTML imports, where both server and client code are bundled together.
337
-
338
- All bundles generated with `target: "bun"` are marked with a special `// @bun` pragma, which indicates to the Bun runtime that there's no need to re-transpile the file before execution.
339
-
340
- If any entrypoints contains a Bun shebang (`#!/usr/bin/env bun`) the bundler will default to `target: "bun"` instead of `"browser"`.
341
-
342
- When using `target: "bun"` and `format: "cjs"` together, the `// @bun @bun-cjs` pragma is added and the CommonJS wrapper function is not compatible with Node.js.
343
-
344
- ---
345
-
346
- - `node`
347
- - For generating bundles that are intended to be run by Node.js. Prioritizes the `"node"` export condition when resolving imports, and outputs `.mjs`. In the future, this will automatically polyfill the `Bun` global and other built-in `bun:*` modules, though this is not yet implemented.
348
-
349
- {% /table %}
350
-
351
- ### `format`
352
-
353
- Specifies the module format to be used in the generated bundles.
354
-
355
- Bun defaults to `"esm"`, and provides experimental support for `"cjs"` and `"iife"`.
356
-
357
- #### `format: "esm"` - ES Module
358
-
359
- This is the default format, which supports ES Module syntax including top-level `await`, import.meta, and more.
360
-
361
- {% codetabs %}
362
-
363
- ```ts#JavaScript
364
- await Bun.build({
365
- entrypoints: ['./index.tsx'],
366
- outdir: './out',
367
- format: "esm",
368
- })
369
- ```
370
-
371
- ```bash#CLI
372
- $ bun build ./index.tsx --outdir ./out --format esm
373
- ```
374
-
375
- {% /codetabs %}
376
-
377
- To use ES Module syntax in browsers, set `format` to `"esm"` and make sure your `<script type="module">` tag has `type="module"` set.
378
-
379
- #### `format: "cjs"` - CommonJS
380
-
381
- To build a CommonJS module, set `format` to `"cjs"`. When choosing `"cjs"`, the default target changes from `"browser"` (esm) to `"node"` (cjs). CommonJS modules transpiled with `format: "cjs", target: "node"` can be executed in both Bun and Node.js (assuming the APIs in use are supported by both).
382
-
383
- {% codetabs %}
384
-
385
- ```ts#JavaScript
386
- await Bun.build({
387
- entrypoints: ['./index.tsx'],
388
- outdir: './out',
389
- format: "cjs",
390
- })
391
- ```
392
-
393
- ```bash#CLI
394
- $ bun build ./index.tsx --outdir ./out --format cjs
395
- ```
396
-
397
- {% /codetabs %}
398
-
399
- #### `format: "iife"` - IIFE
400
-
401
- TODO: document IIFE once we support globalNames.
402
-
403
- ### `jsx`
404
-
405
- Configure JSX transform behavior. Allows fine-grained control over how JSX is compiled.
406
-
407
- **Classic runtime example** (uses `factory` and `fragment`):
408
-
409
- {% codetabs %}
410
-
411
- ```ts#JavaScript
412
- await Bun.build({
413
- entrypoints: ['./app.tsx'],
414
- outdir: './out',
415
- jsx: {
416
- factory: 'h',
417
- fragment: 'Fragment',
418
- runtime: 'classic',
419
- },
420
- })
421
- ```
422
-
423
- ```bash#CLI
424
- # JSX configuration is handled via bunfig.toml or tsconfig.json
425
- $ bun build ./app.tsx --outdir ./out
426
- ```
427
-
428
- {% /codetabs %}
429
-
430
- **Automatic runtime example** (uses `importSource`):
431
-
432
- {% codetabs %}
433
-
434
- ```ts#JavaScript
435
- await Bun.build({
436
- entrypoints: ['./app.tsx'],
437
- outdir: './out',
438
- jsx: {
439
- importSource: 'preact',
440
- runtime: 'automatic',
441
- },
442
- })
443
- ```
444
-
445
- ```bash#CLI
446
- # JSX configuration is handled via bunfig.toml or tsconfig.json
447
- $ bun build ./app.tsx --outdir ./out
448
- ```
449
-
450
- {% /codetabs %}
451
-
452
- ### `splitting`
453
-
454
- Whether to enable code splitting.
455
-
456
- {% codetabs group="a" %}
457
-
458
- ```ts#JavaScript
459
- await Bun.build({
460
- entrypoints: ['./index.tsx'],
461
- outdir: './out',
462
- splitting: false, // default
463
- })
464
- ```
465
-
466
- ```bash#CLI
467
- $ bun build ./index.tsx --outdir ./out --splitting
468
- ```
469
-
470
- {% /codetabs %}
471
-
472
- When `true`, the bundler will enable _code splitting_. When multiple entrypoints both import the same file, module, or set of files/modules, it's often useful to split the shared code into a separate bundle. This shared bundle is known as a _chunk_. Consider the following files:
473
-
474
- {% codetabs %}
475
-
476
- ```ts#entry-a.ts
477
- import { shared } from './shared.ts';
478
- ```
479
-
480
- ```ts#entry-b.ts
481
- import { shared } from './shared.ts';
482
- ```
483
-
484
- ```ts#shared.ts
485
- export const shared = 'shared';
486
- ```
487
-
488
- {% /codetabs %}
489
-
490
- To bundle `entry-a.ts` and `entry-b.ts` with code-splitting enabled:
491
-
492
- {% codetabs group="a" %}
493
-
494
- ```ts#JavaScript
495
- await Bun.build({
496
- entrypoints: ['./entry-a.ts', './entry-b.ts'],
497
- outdir: './out',
498
- splitting: true,
499
- })
500
- ```
501
-
502
- ```bash#CLI
503
- $ bun build ./entry-a.ts ./entry-b.ts --outdir ./out --splitting
504
- ```
505
-
506
- {% /codetabs %}
507
-
508
- Running this build will result in the following files:
509
-
510
- ```txt
511
- .
512
- ├── entry-a.tsx
513
- ├── entry-b.tsx
514
- ├── shared.tsx
515
- └── out
516
- ├── entry-a.js
517
- ├── entry-b.js
518
- └── chunk-2fce6291bf86559d.js
519
-
520
- ```
521
-
522
- The generated `chunk-2fce6291bf86559d.js` file contains the shared code. To avoid collisions, the file name automatically includes a content hash by default. This can be customized with [`naming`](#naming).
523
-
524
- ### `plugins`
525
-
526
- A list of plugins to use during bundling.
527
-
528
- {% codetabs group="a" %}
529
-
530
- ```ts#JavaScript
531
- await Bun.build({
532
- entrypoints: ['./index.tsx'],
533
- outdir: './out',
534
- plugins: [/* ... */],
535
- })
536
- ```
537
-
538
- ```bash#CLI
539
- n/a
540
- ```
541
-
542
- {% /codetabs %}
543
-
544
- Bun implements a universal plugin system for both Bun's runtime and bundler. Refer to the [plugin documentation](https://bun.com/docs/bundler/plugins) for complete documentation.
545
-
546
- <!-- ### `manifest`
547
-
548
- Whether to return a build manifest in the result of `Bun.build`.
549
-
550
- ```ts
551
- const result = await Bun.build({
552
- entrypoints: ["./index.tsx"],
553
- outdir: "./out",
554
- manifest: true, // default is true
555
- });
556
-
557
- console.log(result.manifest);
558
- ```
559
-
560
- {% details summary="Manifest structure" %}
561
-
562
- The manifest has the following form:
563
-
564
- ```ts
565
- export type BuildManifest = {
566
- inputs: {
567
- [path: string]: {
568
- output: {
569
- path: string;
570
- };
571
- imports: {
572
- path: string;
573
- kind: ImportKind;
574
- external?: boolean;
575
- }[];
576
- };
577
- };
578
- outputs: {
579
- [path: string]: {
580
- type: "chunk" | "entry-point" | "asset";
581
- inputs: { path: string }[];
582
- imports: {
583
- path: string;
584
- kind: ImportKind;
585
- external?: boolean;
586
- asset?: boolean;
587
- }[];
588
- exports: string[];
589
- };
590
- };
591
- };
592
-
593
- export type ImportKind =
594
- | "entry-point-build"
595
- | "entry-point-run"
596
- | "import-statement"
597
- | "require-call"
598
- | "dynamic-import"
599
- | "require-resolve"
600
- | "import-rule"
601
- | "url-token";
602
- ```
603
-
604
- {% /details %}
605
-
606
- By design, the manifest is a simple JSON object that can easily be serialized or written to disk. It is also compatible with esbuild's [`metafile`](https://esbuild.github.io/api/#metafile) format. -->
607
-
608
- ### `env`
609
-
610
- Controls how environment variables are handled during bundling. Internally, this uses `define` to inject environment variables into the bundle, but makes it easier to specify the environment variables to inject.
611
-
612
- #### `env: "inline"`
613
-
614
- Injects environment variables into the bundled output by converting `process.env.FOO` references to string literals containing the actual environment variable values.
615
-
616
- {% codetabs group="a" %}
617
-
618
- ```ts#JavaScript
619
- await Bun.build({
620
- entrypoints: ['./index.tsx'],
621
- outdir: './out',
622
- env: "inline",
623
- })
624
- ```
625
-
626
- ```bash#CLI
627
- $ FOO=bar BAZ=123 bun build ./index.tsx --outdir ./out --env inline
628
- ```
629
-
630
- {% /codetabs %}
631
-
632
- For the input below:
633
-
634
- ```js#input.js
635
- console.log(process.env.FOO);
636
- console.log(process.env.BAZ);
637
- ```
638
-
639
- The generated bundle will contain the following code:
640
-
641
- ```js#output.js
642
- console.log("bar");
643
- console.log("123");
644
- ```
645
-
646
- #### `env: "PUBLIC_*"` (prefix)
647
-
648
- Inlines environment variables matching the given prefix (the part before the `*` character), replacing `process.env.FOO` with the actual environment variable value. This is useful for selectively inlining environment variables for things like public-facing URLs or client-side tokens, without worrying about injecting private credentials into output bundles.
649
-
650
- {% codetabs group="a" %}
651
-
652
- ```ts#JavaScript
653
- await Bun.build({
654
- entrypoints: ['./index.tsx'],
655
- outdir: './out',
656
-
657
- // Inline all env vars that start with "ACME_PUBLIC_"
658
- env: "ACME_PUBLIC_*",
659
- })
660
- ```
661
-
662
- ```bash#CLI
663
- $ FOO=bar BAZ=123 ACME_PUBLIC_URL=https://acme.com bun build ./index.tsx --outdir ./out --env 'ACME_PUBLIC_*'
664
- ```
665
-
666
- {% /codetabs %}
667
-
668
- For example, given the following environment variables:
669
-
670
- ```bash
671
- $ FOO=bar BAZ=123 ACME_PUBLIC_URL=https://acme.com
672
- ```
673
-
674
- And source code:
675
-
676
- ```ts#index.tsx
677
- console.log(process.env.FOO);
678
- console.log(process.env.ACME_PUBLIC_URL);
679
- console.log(process.env.BAZ);
680
- ```
681
-
682
- The generated bundle will contain the following code:
683
-
684
- ```js
685
- console.log(process.env.FOO);
686
- console.log("https://acme.com");
687
- console.log(process.env.BAZ);
688
- ```
689
-
690
- #### `env: "disable"`
691
-
692
- Disables environment variable injection entirely.
693
-
694
- For example, given the following environment variables:
695
-
696
- ```bash
697
- $ FOO=bar BAZ=123 ACME_PUBLIC_URL=https://acme.com
698
- ```
699
-
700
- And source code:
701
-
702
- ```ts#index.tsx
703
- console.log(process.env.FOO);
704
- console.log(process.env.ACME_PUBLIC_URL);
705
- console.log(process.env.BAZ);
706
- ```
707
-
708
- The generated bundle will contain the following code:
709
-
710
- ```js
711
- console.log(process.env.FOO);
712
- console.log(process.env.BAZ);
713
- ```
714
-
715
- ### `sourcemap`
716
-
717
- Specifies the type of sourcemap to generate.
718
-
719
- {% codetabs group="a" %}
720
-
721
- ```ts#JavaScript
722
- await Bun.build({
723
- entrypoints: ['./index.tsx'],
724
- outdir: './out',
725
- sourcemap: 'linked', // default 'none'
726
- })
727
- ```
728
-
729
- ```bash#CLI
730
- $ bun build ./index.tsx --outdir ./out --sourcemap=linked
731
- ```
732
-
733
- {% /codetabs %}
734
-
735
- {% table %}
736
-
737
- ---
738
-
739
- - `"none"`
740
- - _Default._ No sourcemap is generated.
741
-
742
- ---
743
-
744
- - `"linked"`
745
- - A separate `*.js.map` file is created alongside each `*.js` bundle using a `//# sourceMappingURL` comment to link the two. Requires `--outdir` to be set. The base URL of this can be customized with `--public-path`.
746
-
747
- ```ts
748
- // <bundled code here>
749
-
750
- //# sourceMappingURL=bundle.js.map
751
- ```
752
-
753
- ---
754
-
755
- - `"external"`
756
- - A separate `*.js.map` file is created alongside each `*.js` bundle without inserting a `//# sourceMappingURL` comment.
757
-
758
- {% /table %}
759
-
760
- {% callout %}
761
-
762
- Generated bundles contain a [debug id](https://sentry.engineering/blog/the-case-for-debug-ids) that can be used to associate a bundle with its corresponding sourcemap. This `debugId` is added as a comment at the bottom of the file.
763
-
764
- ```ts
765
- // <generated bundle code>
766
-
767
- //# debugId=<DEBUG ID>
768
- ```
769
-
770
- ---
771
-
772
- - `"inline"`
773
- - A sourcemap is generated and appended to the end of the generated bundle as a base64 payload.
774
-
775
- ```ts
776
- // <bundled code here>
777
-
778
- //# sourceMappingURL=data:application/json;base64,<encoded sourcemap here>
779
- ```
780
-
781
- The associated `*.js.map` sourcemap will be a JSON file containing an equivalent `debugId` property.
782
-
783
- {% /callout %}
784
-
785
- ### `minify`
786
-
787
- Whether to enable minification. Default `false`.
788
-
789
- {% callout %}
790
- When targeting `bun`, identifiers will be minified by default.
791
- {% /callout %}
792
-
793
- {% callout %}
794
- When `minify.syntax` is enabled, unused function and class expression names are removed unless `minify.keepNames` is set to `true` or `--keep-names` flag is used.
795
- {% /callout %}
796
-
797
- To enable all minification options:
798
-
799
- {% codetabs group="a" %}
800
-
801
- ```ts#JavaScript
802
- await Bun.build({
803
- entrypoints: ['./index.tsx'],
804
- outdir: './out',
805
- minify: true, // default false
806
- })
807
- ```
808
-
809
- ```bash#CLI
810
- $ bun build ./index.tsx --outdir ./out --minify
811
- ```
812
-
813
- {% /codetabs %}
814
-
815
- To granularly enable certain minifications:
816
-
817
- {% codetabs group="a" %}
818
-
819
- ```ts#JavaScript
820
- await Bun.build({
821
- entrypoints: ['./index.tsx'],
822
- outdir: './out',
823
- minify: {
824
- whitespace: true,
825
- identifiers: true,
826
- syntax: true,
827
- keepNames: false, // default
828
- },
829
- })
830
- ```
831
-
832
- ```bash#CLI
833
- $ bun build ./index.tsx --outdir ./out --minify-whitespace --minify-identifiers --minify-syntax
834
-
835
- # To preserve function and class names during minification:
836
- $ bun build ./index.tsx --outdir ./out --minify --keep-names
837
- ```
838
-
839
- {% /codetabs %}
840
-
841
- <!-- ### `treeshaking`
842
-
843
- boolean; -->
844
-
845
- ### `external`
846
-
847
- A list of import paths to consider _external_. Defaults to `[]`.
848
-
849
- {% codetabs group="a" %}
850
-
851
- ```ts#JavaScript
852
- await Bun.build({
853
- entrypoints: ['./index.tsx'],
854
- outdir: './out',
855
- external: ["lodash", "react"], // default: []
856
- })
857
- ```
858
-
859
- ```bash#CLI
860
- $ bun build ./index.tsx --outdir ./out --external lodash --external react
861
- ```
862
-
863
- {% /codetabs %}
864
-
865
- An external import is one that will not be included in the final bundle. Instead, the `import` statement will be left as-is, to be resolved at runtime.
866
-
867
- For instance, consider the following entrypoint file:
868
-
869
- ```ts#index.tsx
870
- import _ from "lodash";
871
- import {z} from "zod";
872
-
873
- const value = z.string().parse("Hello world!")
874
- console.log(_.upperCase(value));
875
- ```
876
-
877
- Normally, bundling `index.tsx` would generate a bundle containing the entire source code of the `"zod"` package. If instead, we want to leave the `import` statement as-is, we can mark it as external:
878
-
879
- {% codetabs group="a" %}
880
-
881
- ```ts#JavaScript
882
- await Bun.build({
883
- entrypoints: ['./index.tsx'],
884
- outdir: './out',
885
- external: ['zod'],
886
- })
887
- ```
888
-
889
- ```bash#CLI
890
- $ bun build ./index.tsx --outdir ./out --external zod
891
- ```
892
-
893
- {% /codetabs %}
894
-
895
- The generated bundle will look something like this:
896
-
897
- ```js#out/index.js
898
- import {z} from "zod";
899
-
900
- // ...
901
- // the contents of the "lodash" package
902
- // including the `_.upperCase` function
903
-
904
- var value = z.string().parse("Hello world!")
905
- console.log(_.upperCase(value));
906
- ```
907
-
908
- To mark all imports as external, use the wildcard `*`:
909
-
910
- {% codetabs %}
911
-
912
- ```ts#JavaScript
913
- await Bun.build({
914
- entrypoints: ['./index.tsx'],
915
- outdir: './out',
916
- external: ['*'],
917
- })
918
- ```
919
-
920
- ```bash#CLI
921
- $ bun build ./index.tsx --outdir ./out --external '*'
922
- ```
923
-
924
- {% /codetabs %}
925
-
926
- ### `packages`
927
-
928
- Control whatever package dependencies are included to bundle or not. Possible values: `bundle` (default), `external`. Bun treats any import which path do not start with `.`, `..` or `/` as package.
929
-
930
- {% codetabs group="a" %}
931
-
932
- ```ts#JavaScript
933
- await Bun.build({
934
- entrypoints: ['./index.ts'],
935
- packages: 'external',
936
- })
937
- ```
938
-
939
- ```bash#CLI
940
- $ bun build ./index.ts --packages external
941
- ```
942
-
943
- {% /codetabs %}
944
-
945
- ### `naming`
946
-
947
- Customizes the generated file names. Defaults to `./[dir]/[name].[ext]`.
948
-
949
- {% codetabs group="a" %}
950
-
951
- ```ts#JavaScript
952
- await Bun.build({
953
- entrypoints: ['./index.tsx'],
954
- outdir: './out',
955
- naming: "[dir]/[name].[ext]", // default
956
- })
957
- ```
958
-
959
- ```bash#CLI
960
- $ bun build ./index.tsx --outdir ./out --entry-naming [dir]/[name].[ext]
961
- ```
962
-
963
- {% /codetabs %}
964
-
965
- By default, the names of the generated bundles are based on the name of the associated entrypoint.
966
-
967
- ```txt
968
- .
969
- ├── index.tsx
970
- └── out
971
- └── index.js
972
- ```
973
-
974
- With multiple entrypoints, the generated file hierarchy will reflect the directory structure of the entrypoints.
975
-
976
- ```txt
977
- .
978
- ├── index.tsx
979
- └── nested
980
- └── index.tsx
981
- └── out
982
- ├── index.js
983
- └── nested
984
- └── index.js
985
- ```
986
-
987
- The names and locations of the generated files can be customized with the `naming` field. This field accepts a template string that is used to generate the filenames for all bundles corresponding to entrypoints. where the following tokens are replaced with their corresponding values:
988
-
989
- - `[name]` - The name of the entrypoint file, without the extension.
990
- - `[ext]` - The extension of the generated bundle.
991
- - `[hash]` - A hash of the bundle contents.
992
- - `[dir]` - The relative path from the project root to the parent directory of the source file.
993
-
994
- For example:
995
-
996
- {% table %}
997
-
998
- - Token
999
- - `[name]`
1000
- - `[ext]`
1001
- - `[hash]`
1002
- - `[dir]`
1003
-
1004
- ---
1005
-
1006
- - `./index.tsx`
1007
- - `index`
1008
- - `js`
1009
- - `a1b2c3d4`
1010
- - `""` (empty string)
1011
-
1012
- ---
1013
-
1014
- - `./nested/entry.ts`
1015
- - `entry`
1016
- - `js`
1017
- - `c3d4e5f6`
1018
- - `"nested"`
1019
-
1020
- {% /table %}
1021
-
1022
- We can combine these tokens to create a template string. For instance, to include the hash in the generated bundle names:
1023
-
1024
- {% codetabs group="a" %}
1025
-
1026
- ```ts#JavaScript
1027
- await Bun.build({
1028
- entrypoints: ['./index.tsx'],
1029
- outdir: './out',
1030
- naming: 'files/[dir]/[name]-[hash].[ext]',
1031
- })
1032
- ```
1033
-
1034
- ```bash#CLI
1035
- $ bun build ./index.tsx --outdir ./out --entry-naming [name]-[hash].[ext]
1036
- ```
1037
-
1038
- {% /codetabs %}
1039
-
1040
- This build would result in the following file structure:
1041
-
1042
- ```txt
1043
- .
1044
- ├── index.tsx
1045
- └── out
1046
- └── files
1047
- └── index-a1b2c3d4.js
1048
- ```
1049
-
1050
- When a `string` is provided for the `naming` field, it is used only for bundles _that correspond to entrypoints_. The names of [chunks](#splitting) and copied assets are not affected. Using the JavaScript API, separate template strings can be specified for each type of generated file.
1051
-
1052
- {% codetabs group="a" %}
1053
-
1054
- ```ts#JavaScript
1055
- await Bun.build({
1056
- entrypoints: ['./index.tsx'],
1057
- outdir: './out',
1058
- naming: {
1059
- // default values
1060
- entry: '[dir]/[name].[ext]',
1061
- chunk: '[name]-[hash].[ext]',
1062
- asset: '[name]-[hash].[ext]',
1063
- },
1064
- })
1065
- ```
1066
-
1067
- ```bash#CLI
1068
- $ bun build ./index.tsx --outdir ./out --entry-naming "[dir]/[name].[ext]" --chunk-naming "[name]-[hash].[ext]" --asset-naming "[name]-[hash].[ext]"
1069
- ```
1070
-
1071
- {% /codetabs %}
1072
-
1073
- ### `root`
1074
-
1075
- The root directory of the project.
1076
-
1077
- {% codetabs group="a" %}
1078
-
1079
- ```ts#JavaScript
1080
- await Bun.build({
1081
- entrypoints: ['./pages/a.tsx', './pages/b.tsx'],
1082
- outdir: './out',
1083
- root: '.',
1084
- })
1085
- ```
1086
-
1087
- ```bash#CLI
1088
- n/a
1089
- ```
1090
-
1091
- {% /codetabs %}
1092
-
1093
- If unspecified, it is computed to be the first common ancestor of all entrypoint files. Consider the following file structure:
1094
-
1095
- ```txt
1096
- .
1097
- └── pages
1098
- └── index.tsx
1099
- └── settings.tsx
1100
- ```
1101
-
1102
- We can build both entrypoints in the `pages` directory:
1103
-
1104
- {% codetabs group="a" %}
1105
-
1106
- ```ts#JavaScript
1107
- await Bun.build({
1108
- entrypoints: ['./pages/index.tsx', './pages/settings.tsx'],
1109
- outdir: './out',
1110
- })
1111
- ```
1112
-
1113
- ```bash#CLI
1114
- $ bun build ./pages/index.tsx ./pages/settings.tsx --outdir ./out
1115
- ```
1116
-
1117
- {% /codetabs %}
1118
-
1119
- This would result in a file structure like this:
1120
-
1121
- ```txt
1122
- .
1123
- └── pages
1124
- └── index.tsx
1125
- └── settings.tsx
1126
- └── out
1127
- └── index.js
1128
- └── settings.js
1129
- ```
1130
-
1131
- Since the `pages` directory is the first common ancestor of the entrypoint files, it is considered the project root. This means that the generated bundles live at the top level of the `out` directory; there is no `out/pages` directory.
1132
-
1133
- This behavior can be overridden by specifying the `root` option:
1134
-
1135
- {% codetabs group="a" %}
1136
-
1137
- ```ts#JavaScript
1138
- await Bun.build({
1139
- entrypoints: ['./pages/index.tsx', './pages/settings.tsx'],
1140
- outdir: './out',
1141
- root: '.',
1142
- })
1143
- ```
1144
-
1145
- ```bash#CLI
1146
- $ bun build ./pages/index.tsx ./pages/settings.tsx --outdir ./out --root .
1147
- ```
1148
-
1149
- {% /codetabs %}
1150
-
1151
- By specifying `.` as `root`, the generated file structure will look like this:
1152
-
1153
- ```txt
1154
- .
1155
- └── pages
1156
- └── index.tsx
1157
- └── settings.tsx
1158
- └── out
1159
- └── pages
1160
- └── index.js
1161
- └── settings.js
1162
- ```
1163
-
1164
- ### `publicPath`
1165
-
1166
- A prefix to be appended to any import paths in bundled code.
1167
-
1168
- In many cases, generated bundles will contain no `import` statements. After all, the goal of bundling is to combine all of the code into a single file. However there are a number of cases with the generated bundles will contain `import` statements.
1169
-
1170
- - **Asset imports** — When importing an unrecognized file type like `*.svg`, the bundler defers to the [`file` loader](https://bun.com/docs/bundler/loaders#file), which copies the file into `outdir` as is. The import is converted into a variable
1171
- - **External modules** — Files and modules can be marked as [`external`](#external), in which case they will not be included in the bundle. Instead, the `import` statement will be left in the final bundle.
1172
- - **Chunking**. When [`splitting`](#splitting) is enabled, the bundler may generate separate "chunk" files that represent code that is shared among multiple entrypoints.
1173
-
1174
- In any of these cases, the final bundles may contain paths to other files. By default these imports are _relative_. Here is an example of a simple asset import:
1175
-
1176
- {% codetabs %}
1177
-
1178
- ```ts#Input
1179
- import logo from './logo.svg';
1180
- console.log(logo);
1181
- ```
1182
-
1183
- ```ts#Output
1184
- // logo.svg is copied into <outdir>
1185
- // and hash is added to the filename to prevent collisions
1186
- var logo = './logo-a7305bdef.svg';
1187
- console.log(logo);
1188
- ```
1189
-
1190
- {% /codetabs %}
1191
-
1192
- Setting `publicPath` will prefix all file paths with the specified value.
1193
-
1194
- {% codetabs group="a" %}
1195
-
1196
- ```ts#JavaScript
1197
- await Bun.build({
1198
- entrypoints: ['./index.tsx'],
1199
- outdir: './out',
1200
- publicPath: 'https://cdn.example.com/', // default is undefined
1201
- })
1202
- ```
1203
-
1204
- ```bash#CLI
1205
- $ bun build ./index.tsx --outdir ./out --public-path https://cdn.example.com/
1206
- ```
1207
-
1208
- {% /codetabs %}
1209
-
1210
- The output file would now look something like this.
1211
-
1212
- ```ts-diff#Output
1213
- - var logo = './logo-a7305bdef.svg';
1214
- + var logo = 'https://cdn.example.com/logo-a7305bdef.svg';
1215
- ```
1216
-
1217
- ### `define`
1218
-
1219
- A map of global identifiers to be replaced at build time. Keys of this object are identifier names, and values are JSON strings that will be inlined.
1220
-
1221
- {% callout }
1222
- This is not needed to inline `process.env.NODE_ENV`, as Bun does this automatically.
1223
- {% /callout %}
1224
-
1225
- {% codetabs %}
1226
-
1227
- ```ts#JavaScript
1228
- await Bun.build({
1229
- entrypoints: ['./index.tsx'],
1230
- outdir: './out',
1231
- define: {
1232
- STRING: JSON.stringify("value"),
1233
- "nested.boolean": "true",
1234
- },
1235
- })
1236
- ```
1237
-
1238
- ```bash#CLI
1239
- $ bun build ./index.tsx --outdir ./out --define 'STRING="value"' --define "nested.boolean=true"
1240
- ```
1241
-
1242
- {% /codetabs %}
1243
-
1244
- ### `loader`
1245
-
1246
- A map of file extensions to [built-in loader names](https://bun.com/docs/bundler/loaders#built-in-loaders). This can be used to quickly customize how certain files are loaded.
1247
-
1248
- {% codetabs %}
1249
-
1250
- ```ts#JavaScript
1251
- await Bun.build({
1252
- entrypoints: ['./index.tsx'],
1253
- outdir: './out',
1254
- loader: {
1255
- ".png": "dataurl",
1256
- ".txt": "file",
1257
- },
1258
- })
1259
- ```
1260
-
1261
- ```bash#CLI
1262
- $ bun build ./index.tsx --outdir ./out --loader .png:dataurl --loader .txt:file
1263
- ```
1264
-
1265
- {% /codetabs %}
1266
-
1267
- ### `banner`
1268
-
1269
- A banner to be added to the final bundle, this can be a directive like "use client" for react or a comment block such as a license for the code.
1270
-
1271
- {% codetabs %}
1272
-
1273
- ```ts#JavaScript
1274
- await Bun.build({
1275
- entrypoints: ['./index.tsx'],
1276
- outdir: './out',
1277
- banner: '"use client";'
1278
- })
1279
- ```
1280
-
1281
- ```bash#CLI
1282
- $ bun build ./index.tsx --outdir ./out --banner "\"use client\";"
1283
- ```
1284
-
1285
- {% /codetabs %}
1286
-
1287
- ### `footer`
1288
-
1289
- A footer to be added to the final bundle, this can be something like a comment block for a license or just a fun easter egg.
1290
-
1291
- {% codetabs %}
1292
-
1293
- ```ts#JavaScript
1294
- await Bun.build({
1295
- entrypoints: ['./index.tsx'],
1296
- outdir: './out',
1297
- footer: '// built with love in SF'
1298
- })
1299
- ```
1300
-
1301
- ```bash#CLI
1302
- $ bun build ./index.tsx --outdir ./out --footer="// built with love in SF"
1303
- ```
1304
-
1305
- {% /codetabs %}
1306
-
1307
- ### `drop`
1308
-
1309
- Remove function calls from a bundle. For example, `--drop=console` will remove all calls to `console.log`. Arguments to calls will also be removed, regardless of if those arguments may have side effects. Dropping `debugger` will remove all `debugger` statements.
1310
-
1311
- {% codetabs %}
1312
-
1313
- ```ts#JavaScript
1314
- await Bun.build({
1315
- entrypoints: ['./index.tsx'],
1316
- outdir: './out',
1317
- drop: ["console", "debugger", "anyIdentifier.or.propertyAccess"],
1318
- })
1319
- ```
1320
-
1321
- ```bash#CLI
1322
- $ bun build ./index.tsx --outdir ./out --drop=console --drop=debugger --drop=anyIdentifier.or.propertyAccess
1323
- ```
1324
-
1325
- {% /codetabs %}
1326
-
1327
- ### `throw`
1328
-
1329
- Controls error handling behavior when the build fails. When set to `true` (default), the returned promise rejects with an `AggregateError`. When set to `false`, the promise resolves with a `BuildOutput` object where `success` is `false`.
1330
-
1331
- ```ts#JavaScript
1332
- // Default behavior: throws on error
1333
- try {
1334
- await Bun.build({
1335
- entrypoints: ['./index.tsx'],
1336
- throw: true, // default
1337
- });
1338
- } catch (error) {
1339
- // Handle AggregateError
1340
- console.error("Build failed:", error);
1341
- }
1342
-
1343
- // Alternative: handle errors via success property
1344
- const result = await Bun.build({
1345
- entrypoints: ['./index.tsx'],
1346
- throw: false,
1347
- });
1348
-
1349
- if (!result.success) {
1350
- console.error("Build failed with errors:", result.logs);
1351
- }
1352
- ```
1353
-
1354
- ## Outputs
1355
-
1356
- The `Bun.build` function returns a `Promise<BuildOutput>`, defined as:
1357
-
1358
- ```ts
1359
- interface BuildOutput {
1360
- outputs: BuildArtifact[];
1361
- success: boolean;
1362
- logs: Array<object>; // see docs for details
1363
- }
1364
-
1365
- interface BuildArtifact extends Blob {
1366
- kind: "entry-point" | "chunk" | "asset" | "sourcemap";
1367
- path: string;
1368
- loader: Loader;
1369
- hash: string | null;
1370
- sourcemap: BuildArtifact | null;
1371
- }
1372
- ```
1373
-
1374
- The `outputs` array contains all the files that were generated by the build. Each artifact implements the `Blob` interface.
1375
-
1376
- ```ts
1377
- const build = await Bun.build({
1378
- /* */
1379
- });
1380
-
1381
- for (const output of build.outputs) {
1382
- await output.arrayBuffer(); // => ArrayBuffer
1383
- await output.bytes(); // => Uint8Array
1384
- await output.text(); // string
1385
- }
1386
- ```
1387
-
1388
- Each artifact also contains the following properties:
1389
-
1390
- {% table %}
1391
-
1392
- ---
1393
-
1394
- - `kind`
1395
- - What kind of build output this file is. A build generates bundled entrypoints, code-split "chunks", sourcemaps, bytecode, and copied assets (like images).
1396
-
1397
- ---
1398
-
1399
- - `path`
1400
- - Absolute path to the file on disk
1401
-
1402
- ---
1403
-
1404
- - `loader`
1405
- - The loader was used to interpret the file. See [Bundler > Loaders](https://bun.com/docs/bundler/loaders) to see how Bun maps file extensions to the appropriate built-in loader.
1406
-
1407
- ---
1408
-
1409
- - `hash`
1410
- - The hash of the file contents. Always defined for assets.
1411
-
1412
- ---
1413
-
1414
- - `sourcemap`
1415
- - The sourcemap file corresponding to this file, if generated. Only defined for entrypoints and chunks.
1416
-
1417
- {% /table %}
1418
-
1419
- Similar to `BunFile`, `BuildArtifact` objects can be passed directly into `new Response()`.
1420
-
1421
- ```ts
1422
- const build = await Bun.build({
1423
- /* */
1424
- });
1425
-
1426
- const artifact = build.outputs[0];
1427
-
1428
- // Content-Type header is automatically set
1429
- return new Response(artifact);
1430
- ```
1431
-
1432
- The Bun runtime implements special pretty-printing of `BuildArtifact` object to make debugging easier.
1433
-
1434
- {% codetabs %}
1435
-
1436
- ```ts#Build_script
1437
- // build.ts
1438
- const build = await Bun.build({/* */});
1439
-
1440
- const artifact = build.outputs[0];
1441
- console.log(artifact);
1442
- ```
1443
-
1444
- ```sh#Shell_output
1445
- $ bun run build.ts
1446
- BuildArtifact (entry-point) {
1447
- path: "./index.js",
1448
- loader: "tsx",
1449
- kind: "entry-point",
1450
- hash: "824a039620219640",
1451
- Blob (114 bytes) {
1452
- type: "text/javascript;charset=utf-8"
1453
- },
1454
- sourcemap: null
1455
- }
1456
- ```
1457
-
1458
- {% /codetabs %}
1459
-
1460
- ### Bytecode
1461
-
1462
- The `bytecode: boolean` option can be used to generate bytecode for any JavaScript/TypeScript entrypoints. This can greatly improve startup times for large applications. Only supported for `"cjs"` format, only supports `"target": "bun"` and dependent on a matching version of Bun. This adds a corresponding `.jsc` file for each entrypoint.
1463
-
1464
- {% codetabs %}
1465
-
1466
- ```ts#JavaScript
1467
- await Bun.build({
1468
- entrypoints: ["./index.tsx"],
1469
- outdir: "./out",
1470
- bytecode: true,
1471
- })
1472
- ```
1473
-
1474
- ```bash#CLI
1475
- $ bun build ./index.tsx --outdir ./out --bytecode
1476
- ```
1477
-
1478
- {% /codetabs %}
1479
-
1480
- ### Executables
1481
-
1482
- Bun supports "compiling" a JavaScript/TypeScript entrypoint into a standalone executable. This executable contains a copy of the Bun binary.
1483
-
1484
- ```sh
1485
- $ bun build ./cli.tsx --outfile mycli --compile
1486
- $ ./mycli
1487
- ```
1488
-
1489
- Refer to [Bundler > Executables](https://bun.com/docs/bundler/executables) for complete documentation.
1490
-
1491
- ## Logs and errors
1492
-
1493
- <!-- 1.2 documentation -->
1494
-
1495
- On failure, `Bun.build` returns a rejected promise with an `AggregateError`. This can be logged to the console for pretty printing of the error list, or programmatically read with a `try`/`catch` block.
1496
-
1497
- ```ts
1498
- try {
1499
- const result = await Bun.build({
1500
- entrypoints: ["./index.tsx"],
1501
- outdir: "./out",
1502
- });
1503
- } catch (e) {
1504
- // TypeScript does not allow annotations on the catch clause
1505
- const error = e as AggregateError;
1506
- console.error("Build Failed");
1507
-
1508
- // Example: Using the built-in formatter
1509
- console.error(error);
1510
-
1511
- // Example: Serializing the failure as a JSON string.
1512
- console.error(JSON.stringify(error, null, 2));
1513
- }
1514
- ```
1515
-
1516
- {% callout %}
1517
-
1518
- Most of the time, an explicit `try`/`catch` is not needed, as Bun will neatly print uncaught exceptions. It is enough to just use a top-level `await` on the `Bun.build` call.
1519
-
1520
- {% /callout %}
1521
-
1522
- Each item in `error.errors` is an instance of `BuildMessage` or `ResolveMessage` (subclasses of Error), containing detailed information for each error.
1523
-
1524
- ```ts
1525
- class BuildMessage {
1526
- name: string;
1527
- position?: Position;
1528
- message: string;
1529
- level: "error" | "warning" | "info" | "debug" | "verbose";
1530
- }
1531
-
1532
- class ResolveMessage extends BuildMessage {
1533
- code: string;
1534
- referrer: string;
1535
- specifier: string;
1536
- importKind: ImportKind;
1537
- }
1538
- ```
1539
-
1540
- On build success, the returned object contains a `logs` property, which contains bundler warnings and info messages.
1541
-
1542
- ```ts
1543
- const result = await Bun.build({
1544
- entrypoints: ["./index.tsx"],
1545
- outdir: "./out",
1546
- });
1547
-
1548
- if (result.logs.length > 0) {
1549
- console.warn("Build succeeded with warnings:");
1550
- for (const message of result.logs) {
1551
- // Bun will pretty print the message object
1552
- console.warn(message);
1553
- }
1554
- }
1555
- ```
1556
-
1557
- ## Reference
1558
-
1559
- ```ts
1560
- interface Bun {
1561
- build(options: BuildOptions): Promise<BuildOutput>;
1562
- }
1563
-
1564
- interface BuildConfig {
1565
- entrypoints: string[]; // list of file path
1566
- outdir?: string; // output directory
1567
- target?: Target; // default: "browser"
1568
- /**
1569
- * Output module format. Top-level await is only supported for `"esm"`.
1570
- *
1571
- * Can be:
1572
- * - `"esm"`
1573
- * - `"cjs"` (**experimental**)
1574
- * - `"iife"` (**experimental**)
1575
- *
1576
- * @default "esm"
1577
- */
1578
- format?: "esm" | "cjs" | "iife";
1579
- /**
1580
- * JSX configuration object for controlling JSX transform behavior
1581
- */
1582
- jsx?: {
1583
- factory?: string;
1584
- fragment?: string;
1585
- importSource?: string;
1586
- runtime?: "automatic" | "classic";
1587
- };
1588
- naming?:
1589
- | string
1590
- | {
1591
- chunk?: string;
1592
- entry?: string;
1593
- asset?: string;
1594
- };
1595
- root?: string; // project root
1596
- splitting?: boolean; // default true, enable code splitting
1597
- plugins?: BunPlugin[];
1598
- external?: string[];
1599
- packages?: "bundle" | "external";
1600
- publicPath?: string;
1601
- define?: Record<string, string>;
1602
- loader?: { [k in string]: Loader };
1603
- sourcemap?: "none" | "linked" | "inline" | "external" | boolean; // default: "none", true -> "inline"
1604
- /**
1605
- * package.json `exports` conditions used when resolving imports
1606
- *
1607
- * Equivalent to `--conditions` in `bun build` or `bun run`.
1608
- *
1609
- * https://nodejs.org/api/packages.html#exports
1610
- */
1611
- conditions?: Array<string> | string;
1612
-
1613
- /**
1614
- * Controls how environment variables are handled during bundling.
1615
- *
1616
- * Can be one of:
1617
- * - `"inline"`: Injects environment variables into the bundled output by converting `process.env.FOO`
1618
- * references to string literals containing the actual environment variable values
1619
- * - `"disable"`: Disables environment variable injection entirely
1620
- * - A string ending in `*`: Inlines environment variables that match the given prefix.
1621
- * For example, `"MY_PUBLIC_*"` will only include env vars starting with "MY_PUBLIC_"
1622
- */
1623
- env?: "inline" | "disable" | `${string}*`;
1624
- minify?:
1625
- | boolean
1626
- | {
1627
- whitespace?: boolean;
1628
- syntax?: boolean;
1629
- identifiers?: boolean;
1630
- keepNames?: boolean;
1631
- };
1632
- /**
1633
- * Ignore dead code elimination/tree-shaking annotations such as @__PURE__ and package.json
1634
- * "sideEffects" fields. This should only be used as a temporary workaround for incorrect
1635
- * annotations in libraries.
1636
- */
1637
- ignoreDCEAnnotations?: boolean;
1638
- /**
1639
- * Force emitting @__PURE__ annotations even if minify.whitespace is true.
1640
- */
1641
- emitDCEAnnotations?: boolean;
1642
-
1643
- /**
1644
- * Generate bytecode for the output. This can dramatically improve cold
1645
- * start times, but will make the final output larger and slightly increase
1646
- * memory usage.
1647
- *
1648
- * Bytecode is currently only supported for CommonJS (`format: "cjs"`).
1649
- *
1650
- * Must be `target: "bun"`
1651
- * @default false
1652
- */
1653
- bytecode?: boolean;
1654
- /**
1655
- * Add a banner to the bundled code such as "use client";
1656
- */
1657
- banner?: string;
1658
- /**
1659
- * Add a footer to the bundled code such as a comment block like
1660
- *
1661
- * `// made with bun!`
1662
- */
1663
- footer?: string;
1664
-
1665
- /**
1666
- * Drop function calls to matching property accesses.
1667
- */
1668
- drop?: string[];
1669
-
1670
- /**
1671
- * When set to `true`, the returned promise rejects with an AggregateError when a build failure happens.
1672
- * When set to `false`, the `success` property of the returned object will be `false` when a build failure happens.
1673
- *
1674
- * This defaults to `true`.
1675
- */
1676
- throw?: boolean;
1677
- }
1678
-
1679
- interface BuildOutput {
1680
- outputs: BuildArtifact[];
1681
- success: boolean;
1682
- logs: Array<BuildMessage | ResolveMessage>;
1683
- }
1684
-
1685
- interface BuildArtifact extends Blob {
1686
- path: string;
1687
- loader: Loader;
1688
- hash: string | null;
1689
- kind: "entry-point" | "chunk" | "asset" | "sourcemap" | "bytecode";
1690
- sourcemap: BuildArtifact | null;
1691
- }
1692
-
1693
- type Loader =
1694
- | "js"
1695
- | "jsx"
1696
- | "ts"
1697
- | "tsx"
1698
- | "json"
1699
- | "toml"
1700
- | "file"
1701
- | "napi"
1702
- | "wasm"
1703
- | "text";
1704
-
1705
- interface BuildOutput {
1706
- outputs: BuildArtifact[];
1707
- success: boolean;
1708
- logs: Array<BuildMessage | ResolveMessage>;
1709
- }
1710
-
1711
- declare class ResolveMessage {
1712
- readonly name: "ResolveMessage";
1713
- readonly position: Position | null;
1714
- readonly code: string;
1715
- readonly message: string;
1716
- readonly referrer: string;
1717
- readonly specifier: string;
1718
- readonly importKind:
1719
- | "entry_point"
1720
- | "stmt"
1721
- | "require"
1722
- | "import"
1723
- | "dynamic"
1724
- | "require_resolve"
1725
- | "at"
1726
- | "at_conditional"
1727
- | "url"
1728
- | "internal";
1729
- readonly level: "error" | "warning" | "info" | "debug" | "verbose";
1730
-
1731
- toString(): string;
1732
- }
1733
- ```
1734
-
1735
- {% bunCLIUsage command="build" /%}