bun-types 1.1.37-canary.20241123T140655 → 1.1.37-canary.20241125T140601

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 (272) hide show
  1. package/docs/api/binary-data.md +1028 -0
  2. package/docs/api/cc.md +197 -0
  3. package/docs/api/color.md +262 -0
  4. package/docs/api/console.md +38 -0
  5. package/docs/api/dns.md +113 -0
  6. package/docs/api/fetch.md +308 -0
  7. package/docs/api/ffi.md +536 -0
  8. package/docs/api/file-io.md +358 -0
  9. package/docs/api/file-system-router.md +112 -0
  10. package/docs/api/file.md +19 -0
  11. package/docs/api/glob.md +157 -0
  12. package/docs/api/globals.md +387 -0
  13. package/docs/api/hashing.md +314 -0
  14. package/docs/api/html-rewriter.md +31 -0
  15. package/docs/api/http.md +661 -0
  16. package/docs/api/import-meta.md +69 -0
  17. package/docs/api/node-api.md +16 -0
  18. package/docs/api/semver.md +52 -0
  19. package/docs/api/spawn.md +441 -0
  20. package/docs/api/sqlite.md +692 -0
  21. package/docs/api/streams.md +214 -0
  22. package/docs/api/tcp.md +221 -0
  23. package/docs/api/test.md +1 -0
  24. package/docs/api/transpiler.md +274 -0
  25. package/docs/api/udp.md +125 -0
  26. package/docs/api/utils.md +773 -0
  27. package/docs/api/websockets.md +547 -0
  28. package/docs/api/workers.md +230 -0
  29. package/docs/benchmarks.md +120 -0
  30. package/docs/bun-flavored-toml.md +42 -0
  31. package/docs/bundler/executables.md +291 -0
  32. package/docs/bundler/index.md +1543 -0
  33. package/docs/bundler/intro.md +75 -0
  34. package/docs/bundler/loaders.md +288 -0
  35. package/docs/bundler/macros.md +327 -0
  36. package/docs/bundler/plugins.md +30 -0
  37. package/docs/bundler/vs-esbuild.md +1127 -0
  38. package/docs/cli/add.md +163 -0
  39. package/docs/cli/bun-completions.md +3 -0
  40. package/docs/cli/bun-create.md +254 -0
  41. package/docs/cli/bun-dev.md +21 -0
  42. package/docs/cli/bun-install.md +255 -0
  43. package/docs/cli/bun-upgrade.md +39 -0
  44. package/docs/cli/bunx.md +80 -0
  45. package/docs/cli/filter.md +57 -0
  46. package/docs/cli/init.md +40 -0
  47. package/docs/cli/install.md +205 -0
  48. package/docs/cli/link.md +38 -0
  49. package/docs/cli/outdated.md +61 -0
  50. package/docs/cli/patch-commit.md +9 -0
  51. package/docs/cli/pm.md +150 -0
  52. package/docs/cli/publish.md +107 -0
  53. package/docs/cli/remove.md +5 -0
  54. package/docs/cli/run.md +196 -0
  55. package/docs/cli/test.md +247 -0
  56. package/docs/cli/unlink.md +7 -0
  57. package/docs/cli/update.md +34 -0
  58. package/docs/contributing/upgrading-webkit.md +57 -0
  59. package/docs/ecosystem/elysia.md +24 -0
  60. package/docs/ecosystem/express.md +37 -0
  61. package/docs/ecosystem/hono.md +18 -0
  62. package/docs/ecosystem/react.md +65 -0
  63. package/docs/ecosystem/stric.md +37 -0
  64. package/docs/guides/binary/arraybuffer-to-array.md +27 -0
  65. package/docs/guides/binary/arraybuffer-to-blob.md +24 -0
  66. package/docs/guides/binary/arraybuffer-to-buffer.md +25 -0
  67. package/docs/guides/binary/arraybuffer-to-string.md +15 -0
  68. package/docs/guides/binary/arraybuffer-to-typedarray.md +39 -0
  69. package/docs/guides/binary/blob-to-arraybuffer.md +14 -0
  70. package/docs/guides/binary/blob-to-dataview.md +14 -0
  71. package/docs/guides/binary/blob-to-stream.md +14 -0
  72. package/docs/guides/binary/blob-to-string.md +15 -0
  73. package/docs/guides/binary/blob-to-typedarray.md +14 -0
  74. package/docs/guides/binary/buffer-to-arraybuffer.md +14 -0
  75. package/docs/guides/binary/buffer-to-blob.md +14 -0
  76. package/docs/guides/binary/buffer-to-readablestream.md +41 -0
  77. package/docs/guides/binary/buffer-to-string.md +25 -0
  78. package/docs/guides/binary/buffer-to-typedarray.md +14 -0
  79. package/docs/guides/binary/dataview-to-string.md +15 -0
  80. package/docs/guides/binary/typedarray-to-arraybuffer.md +25 -0
  81. package/docs/guides/binary/typedarray-to-blob.md +16 -0
  82. package/docs/guides/binary/typedarray-to-buffer.md +14 -0
  83. package/docs/guides/binary/typedarray-to-dataview.md +14 -0
  84. package/docs/guides/binary/typedarray-to-readablestream.md +41 -0
  85. package/docs/guides/binary/typedarray-to-string.md +16 -0
  86. package/docs/guides/ecosystem/astro.md +72 -0
  87. package/docs/guides/ecosystem/discordjs.md +77 -0
  88. package/docs/guides/ecosystem/docker.md +140 -0
  89. package/docs/guides/ecosystem/drizzle.md +185 -0
  90. package/docs/guides/ecosystem/edgedb.md +228 -0
  91. package/docs/guides/ecosystem/elysia.md +31 -0
  92. package/docs/guides/ecosystem/express.md +40 -0
  93. package/docs/guides/ecosystem/hono.md +39 -0
  94. package/docs/guides/ecosystem/mongoose.md +87 -0
  95. package/docs/guides/ecosystem/neon-drizzle.md +220 -0
  96. package/docs/guides/ecosystem/neon-serverless-postgres.md +55 -0
  97. package/docs/guides/ecosystem/nextjs.md +38 -0
  98. package/docs/guides/ecosystem/nuxt.md +56 -0
  99. package/docs/guides/ecosystem/pm2.md +57 -0
  100. package/docs/guides/ecosystem/prisma.md +140 -0
  101. package/docs/guides/ecosystem/qwik.md +107 -0
  102. package/docs/guides/ecosystem/react.md +49 -0
  103. package/docs/guides/ecosystem/remix.md +78 -0
  104. package/docs/guides/ecosystem/render.md +79 -0
  105. package/docs/guides/ecosystem/sentry.md +52 -0
  106. package/docs/guides/ecosystem/solidstart.md +58 -0
  107. package/docs/guides/ecosystem/ssr-react.md +51 -0
  108. package/docs/guides/ecosystem/stric.md +55 -0
  109. package/docs/guides/ecosystem/sveltekit.md +125 -0
  110. package/docs/guides/ecosystem/systemd.md +113 -0
  111. package/docs/guides/ecosystem/vite.md +70 -0
  112. package/docs/guides/http/cluster.md +66 -0
  113. package/docs/guides/http/fetch-unix.md +33 -0
  114. package/docs/guides/http/fetch.md +24 -0
  115. package/docs/guides/http/file-uploads.md +94 -0
  116. package/docs/guides/http/hot.md +22 -0
  117. package/docs/guides/http/proxy.md +24 -0
  118. package/docs/guides/http/server.md +46 -0
  119. package/docs/guides/http/simple.md +18 -0
  120. package/docs/guides/http/stream-file.md +48 -0
  121. package/docs/guides/http/stream-iterator.md +47 -0
  122. package/docs/guides/http/stream-node-streams-in-bun.md +20 -0
  123. package/docs/guides/http/tls.md +30 -0
  124. package/docs/guides/install/add-dev.md +26 -0
  125. package/docs/guides/install/add-git.md +36 -0
  126. package/docs/guides/install/add-optional.md +25 -0
  127. package/docs/guides/install/add-peer.md +17 -0
  128. package/docs/guides/install/add-tarball.md +33 -0
  129. package/docs/guides/install/add.md +42 -0
  130. package/docs/guides/install/azure-artifacts.md +73 -0
  131. package/docs/guides/install/cicd.md +41 -0
  132. package/docs/guides/install/custom-registry.md +30 -0
  133. package/docs/guides/install/from-npm-install-to-bun-install.md +214 -0
  134. package/docs/guides/install/git-diff-bun-lockfile.md +38 -0
  135. package/docs/guides/install/jfrog-artifactory.md +28 -0
  136. package/docs/guides/install/npm-alias.md +23 -0
  137. package/docs/guides/install/registry-scope.md +36 -0
  138. package/docs/guides/install/trusted.md +48 -0
  139. package/docs/guides/install/workspaces.md +70 -0
  140. package/docs/guides/install/yarnlock.md +42 -0
  141. package/docs/guides/process/argv.md +57 -0
  142. package/docs/guides/process/ctrl-c.md +16 -0
  143. package/docs/guides/process/ipc.md +66 -0
  144. package/docs/guides/process/nanoseconds.md +13 -0
  145. package/docs/guides/process/os-signals.md +39 -0
  146. package/docs/guides/process/spawn-stderr.md +31 -0
  147. package/docs/guides/process/spawn-stdout.md +26 -0
  148. package/docs/guides/process/spawn.md +41 -0
  149. package/docs/guides/process/stdin.md +54 -0
  150. package/docs/guides/read-file/arraybuffer.md +28 -0
  151. package/docs/guides/read-file/buffer.md +19 -0
  152. package/docs/guides/read-file/exists.md +16 -0
  153. package/docs/guides/read-file/json.md +17 -0
  154. package/docs/guides/read-file/mime.md +20 -0
  155. package/docs/guides/read-file/stream.md +26 -0
  156. package/docs/guides/read-file/string.md +22 -0
  157. package/docs/guides/read-file/uint8array.md +21 -0
  158. package/docs/guides/read-file/watch.md +68 -0
  159. package/docs/guides/runtime/cicd.md +43 -0
  160. package/docs/guides/runtime/define-constant.md +145 -0
  161. package/docs/guides/runtime/import-html.md +15 -0
  162. package/docs/guides/runtime/import-json.md +44 -0
  163. package/docs/guides/runtime/import-toml.md +30 -0
  164. package/docs/guides/runtime/read-env.md +32 -0
  165. package/docs/guides/runtime/set-env.md +37 -0
  166. package/docs/guides/runtime/shell.md +40 -0
  167. package/docs/guides/runtime/timezone.md +35 -0
  168. package/docs/guides/runtime/tsconfig-paths.md +29 -0
  169. package/docs/guides/runtime/typescript.md +47 -0
  170. package/docs/guides/runtime/vscode-debugger.md +47 -0
  171. package/docs/guides/runtime/web-debugger.md +82 -0
  172. package/docs/guides/streams/node-readable-to-arraybuffer.md +11 -0
  173. package/docs/guides/streams/node-readable-to-blob.md +11 -0
  174. package/docs/guides/streams/node-readable-to-json.md +12 -0
  175. package/docs/guides/streams/node-readable-to-string.md +12 -0
  176. package/docs/guides/streams/node-readable-to-uint8array.md +11 -0
  177. package/docs/guides/streams/to-array.md +14 -0
  178. package/docs/guides/streams/to-arraybuffer.md +14 -0
  179. package/docs/guides/streams/to-blob.md +14 -0
  180. package/docs/guides/streams/to-buffer.md +15 -0
  181. package/docs/guides/streams/to-json.md +14 -0
  182. package/docs/guides/streams/to-string.md +14 -0
  183. package/docs/guides/streams/to-typedarray.md +22 -0
  184. package/docs/guides/test/bail.md +22 -0
  185. package/docs/guides/test/coverage-threshold.md +60 -0
  186. package/docs/guides/test/coverage.md +44 -0
  187. package/docs/guides/test/happy-dom.md +68 -0
  188. package/docs/guides/test/migrate-from-jest.md +110 -0
  189. package/docs/guides/test/mock-clock.md +48 -0
  190. package/docs/guides/test/mock-functions.md +68 -0
  191. package/docs/guides/test/rerun-each.md +14 -0
  192. package/docs/guides/test/run-tests.md +111 -0
  193. package/docs/guides/test/skip-tests.md +39 -0
  194. package/docs/guides/test/snapshot.md +99 -0
  195. package/docs/guides/test/spy-on.md +46 -0
  196. package/docs/guides/test/testing-library.md +87 -0
  197. package/docs/guides/test/timeout.md +15 -0
  198. package/docs/guides/test/todo-tests.md +67 -0
  199. package/docs/guides/test/update-snapshots.md +50 -0
  200. package/docs/guides/test/watch-mode.md +19 -0
  201. package/docs/guides/util/base64.md +15 -0
  202. package/docs/guides/util/deep-equals.md +39 -0
  203. package/docs/guides/util/deflate.md +18 -0
  204. package/docs/guides/util/detect-bun.md +23 -0
  205. package/docs/guides/util/entrypoint.md +17 -0
  206. package/docs/guides/util/escape-html.md +22 -0
  207. package/docs/guides/util/file-url-to-path.md +14 -0
  208. package/docs/guides/util/gzip.md +18 -0
  209. package/docs/guides/util/hash-a-password.md +54 -0
  210. package/docs/guides/util/import-meta-dir.md +13 -0
  211. package/docs/guides/util/import-meta-file.md +13 -0
  212. package/docs/guides/util/import-meta-path.md +13 -0
  213. package/docs/guides/util/main.md +32 -0
  214. package/docs/guides/util/path-to-file-url.md +14 -0
  215. package/docs/guides/util/sleep.md +22 -0
  216. package/docs/guides/util/version.md +21 -0
  217. package/docs/guides/util/which-path-to-executable-bin.md +15 -0
  218. package/docs/guides/websocket/compression.md +31 -0
  219. package/docs/guides/websocket/context.md +72 -0
  220. package/docs/guides/websocket/pubsub.md +38 -0
  221. package/docs/guides/websocket/simple.md +33 -0
  222. package/docs/guides/write-file/append.md +52 -0
  223. package/docs/guides/write-file/basic.md +44 -0
  224. package/docs/guides/write-file/blob.md +28 -0
  225. package/docs/guides/write-file/cat.md +17 -0
  226. package/docs/guides/write-file/file-cp.md +16 -0
  227. package/docs/guides/write-file/filesink.md +52 -0
  228. package/docs/guides/write-file/response.md +17 -0
  229. package/docs/guides/write-file/stdout.md +21 -0
  230. package/docs/guides/write-file/stream.md +17 -0
  231. package/docs/guides/write-file/unlink.md +23 -0
  232. package/docs/index.md +77 -0
  233. package/docs/install/cache.md +57 -0
  234. package/docs/install/index.md +202 -0
  235. package/docs/install/lifecycle.md +46 -0
  236. package/docs/install/lockfile.md +90 -0
  237. package/docs/install/npmrc.md +75 -0
  238. package/docs/install/overrides.md +73 -0
  239. package/docs/install/patch.md +57 -0
  240. package/docs/install/registries.md +30 -0
  241. package/docs/install/workspaces.md +70 -0
  242. package/docs/installation.md +289 -0
  243. package/docs/project/benchmarking.md +203 -0
  244. package/docs/project/building-windows.md +162 -0
  245. package/docs/project/internals/build-process-for-ci.md +75 -0
  246. package/docs/project/roadmap.md +87 -0
  247. package/docs/quickstart.md +144 -0
  248. package/docs/runtime/autoimport.md +94 -0
  249. package/docs/runtime/bun-apis.md +129 -0
  250. package/docs/runtime/bunfig.md +532 -0
  251. package/docs/runtime/debugger.md +325 -0
  252. package/docs/runtime/env.md +214 -0
  253. package/docs/runtime/hot.md +139 -0
  254. package/docs/runtime/index.md +309 -0
  255. package/docs/runtime/jsx.md +326 -0
  256. package/docs/runtime/loaders.md +127 -0
  257. package/docs/runtime/modules.md +298 -0
  258. package/docs/runtime/nodejs-apis.md +456 -0
  259. package/docs/runtime/plugins.md +605 -0
  260. package/docs/runtime/shell.md +537 -0
  261. package/docs/runtime/typescript.md +60 -0
  262. package/docs/runtime/web-apis.md +128 -0
  263. package/docs/test/coverage.md +91 -0
  264. package/docs/test/dom.md +75 -0
  265. package/docs/test/hot.md +15 -0
  266. package/docs/test/lifecycle.md +81 -0
  267. package/docs/test/mocks.md +236 -0
  268. package/docs/test/snapshots.md +15 -0
  269. package/docs/test/time.md +106 -0
  270. package/docs/test/writing.md +547 -0
  271. package/docs/typescript.md +51 -0
  272. package/package.json +5 -4
@@ -0,0 +1,1543 @@
1
+ Bun's fast native bundler is now in beta. It 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
+
30
+ Let's jump into the bundler API.
31
+
32
+ {% callout %}
33
+ Note that the Bun bundler is not intended to replace `tsc` for typechecking or generating type declarations.
34
+ {% /callout %}
35
+
36
+ ## Basic example
37
+
38
+ Let's build our first bundle. You have the following two files, which implement a simple client-side rendered React app.
39
+
40
+ {% codetabs %}
41
+
42
+ ```tsx#./index.tsx
43
+ import * as ReactDOM from 'react-dom/client';
44
+ import {Component} from "./Component"
45
+
46
+ const root = ReactDOM.createRoot(document.getElementById('root')!);
47
+ root.render(<Component message="Sup!" />)
48
+ ```
49
+
50
+ ```tsx#./Component.tsx
51
+ export function Component(props: {message: string}) {
52
+ return <p>{props.message}</p>
53
+ }
54
+ ```
55
+
56
+ {% /codetabs %}
57
+
58
+ 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.
59
+
60
+ To create our bundle:
61
+
62
+ {% codetabs group="a" %}
63
+
64
+ ```ts#JavaScript
65
+ await Bun.build({
66
+ entrypoints: ['./index.tsx'],
67
+ outdir: './out',
68
+ })
69
+ ```
70
+
71
+ ```bash#CLI
72
+ $ bun build ./index.tsx --outdir ./out
73
+ ```
74
+
75
+ {% /codetabs %}
76
+
77
+ 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:
78
+
79
+ ```ts
80
+ .
81
+ ├── index.tsx
82
+ ├── Component.tsx
83
+ └── out
84
+ └── index.js
85
+ ```
86
+
87
+ The contents of `out/index.js` will look something like this:
88
+
89
+ ```js#out/index.js
90
+ // ...
91
+ // ~20k lines of code
92
+ // including the contents of `react-dom/client` and all its dependencies
93
+ // this is where the $jsxDEV and $createRoot functions are defined
94
+
95
+
96
+ // Component.tsx
97
+ function Component(props) {
98
+ return $jsxDEV("p", {
99
+ children: props.message
100
+ }, undefined, false, undefined, this);
101
+ }
102
+
103
+ // index.tsx
104
+ var rootNode = document.getElementById("root");
105
+ var root = $createRoot(rootNode);
106
+ root.render($jsxDEV(Component, {
107
+ message: "Sup!"
108
+ }, undefined, false, undefined, this));
109
+ ```
110
+
111
+ {% details summary="Tutorial: Run this file in your browser" %}
112
+ We can load this file in the browser to see our app in action. Create an `index.html` file in the `out` directory:
113
+
114
+ ```bash
115
+ $ touch out/index.html
116
+ ```
117
+
118
+ Then paste the following contents into it:
119
+
120
+ ```html
121
+ <html>
122
+ <body>
123
+ <div id="root"></div>
124
+ <script type="module" src="/index.js"></script>
125
+ </body>
126
+ </html>
127
+ ```
128
+
129
+ Then spin up a static file server serving the `out` directory:
130
+
131
+ ```bash
132
+ $ bunx serve out
133
+ ```
134
+
135
+ Visit `http://localhost:5000` to see your bundled app in action.
136
+
137
+ {% /details %}
138
+
139
+ ## Watch mode
140
+
141
+ Like the runtime and test runner, the bundler supports watch mode natively.
142
+
143
+ ```sh
144
+ $ bun build ./index.tsx --outdir ./out --watch
145
+ ```
146
+
147
+ ## Content types
148
+
149
+ 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.sh/docs/runtime/loaders) for full documentation.
150
+
151
+ {% table %}
152
+
153
+ - Extensions
154
+ - Details
155
+
156
+ ---
157
+
158
+ - `.js` `.jsx`, `.cjs` `.mjs` `.mts` `.cts` `.ts` `.tsx`
159
+ - 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.
160
+
161
+ ---
162
+
163
+ - `.json`
164
+ - JSON files are parsed and inlined into the bundle as a JavaScript object.
165
+
166
+ ```ts
167
+ import pkg from "./package.json";
168
+ pkg.name; // => "my-package"
169
+ ```
170
+
171
+ ---
172
+
173
+ - `.toml`
174
+ - TOML files are parsed and inlined into the bundle as a JavaScript object.
175
+
176
+ ```ts
177
+ import config from "./bunfig.toml";
178
+ config.logLevel; // => "debug"
179
+ ```
180
+
181
+ ---
182
+
183
+ - `.txt`
184
+ - The contents of the text file are read and inlined into the bundle as a string.
185
+
186
+ ```ts
187
+ import contents from "./file.txt";
188
+ console.log(contents); // => "Hello, world!"
189
+ ```
190
+
191
+ ---
192
+
193
+ - `.node` `.wasm`
194
+ - These files are supported by the Bun runtime, but during bundling they are treated as [assets](#assets).
195
+
196
+ {% /table %}
197
+
198
+ ### Assets
199
+
200
+ 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.
201
+
202
+ {% codetabs %}
203
+
204
+ ```ts#Input
205
+ // bundle entrypoint
206
+ import logo from "./logo.svg";
207
+ console.log(logo);
208
+ ```
209
+
210
+ ```ts#Output
211
+ // bundled output
212
+ var logo = "./logo-ab237dfe.svg";
213
+ console.log(logo);
214
+ ```
215
+
216
+ {% /codetabs %}
217
+
218
+ {% callout %}
219
+ The exact behavior of the file loader is also impacted by [`naming`](#naming) and [`publicPath`](#publicpath).
220
+ {% /callout %}
221
+
222
+ Refer to the [Bundler > Loaders](https://bun.sh/docs/bundler/loaders#file) page for more complete documentation on the file loader.
223
+
224
+ ### Plugins
225
+
226
+ The behavior described in this table can be overridden or extended with [plugins](https://bun.sh/docs/bundler/plugins). Refer to the [Bundler > Loaders](https://bun.sh/docs/bundler/plugins) page for complete documentation.
227
+
228
+ ## API
229
+
230
+ ### `entrypoints`
231
+
232
+ **Required.** An array of paths corresponding to the entrypoints of our application. One bundle will be generated for each entrypoint.
233
+
234
+ {% codetabs group="a" %}
235
+
236
+ ```ts#JavaScript
237
+ const result = await Bun.build({
238
+ entrypoints: ["./index.ts"],
239
+ });
240
+ // => { success: boolean, outputs: BuildArtifact[], logs: BuildMessage[] }
241
+ ```
242
+
243
+ ```bash#CLI
244
+ $ bun build --entrypoints ./index.ts
245
+ # the bundle will be printed to stdout
246
+ # <bundled code>
247
+ ```
248
+
249
+ {% /codetabs %}
250
+
251
+ ### `outdir`
252
+
253
+ The directory where output files will be written.
254
+
255
+ {% codetabs group="a" %}
256
+
257
+ ```ts#JavaScript
258
+ const result = await Bun.build({
259
+ entrypoints: ['./index.ts'],
260
+ outdir: './out'
261
+ });
262
+ // => { success: boolean, outputs: BuildArtifact[], logs: BuildMessage[] }
263
+ ```
264
+
265
+ ```bash#CLI
266
+ $ bun build --entrypoints ./index.ts --outdir ./out
267
+ # a summary of bundled files will be printed to stdout
268
+ ```
269
+
270
+ {% /codetabs %}
271
+
272
+ 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.
273
+
274
+ ```ts
275
+ const result = await Bun.build({
276
+ entrypoints: ["./index.ts"],
277
+ });
278
+
279
+ for (const res of result.outputs) {
280
+ // Can be consumed as blobs
281
+ await res.text();
282
+
283
+ // Bun will set Content-Type and Etag headers
284
+ new Response(res);
285
+
286
+ // Can be written manually, but you should use `outdir` in this case.
287
+ Bun.write(path.join("out", res.path), res);
288
+ }
289
+ ```
290
+
291
+ When `outdir` is set, the `path` property on a `BuildArtifact` will be the absolute path to where it was written to.
292
+
293
+ ### `target`
294
+
295
+ The intended execution environment for the bundle.
296
+
297
+ {% codetabs group="a" %}
298
+
299
+ ```ts#JavaScript
300
+ await Bun.build({
301
+ entrypoints: ['./index.ts'],
302
+ outdir: './out',
303
+ target: 'browser', // default
304
+ })
305
+ ```
306
+
307
+ ```bash#CLI
308
+ $ bun build --entrypoints ./index.ts --outdir ./out --target browser
309
+ ```
310
+
311
+ {% /codetabs %}
312
+
313
+ Depending on the target, Bun will apply different module resolution rules and optimizations.
314
+
315
+ <!-- - 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`. -->
316
+
317
+ {% table %}
318
+
319
+ ---
320
+
321
+ - `browser`
322
+ - _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.
323
+
324
+ ---
325
+
326
+ - `bun`
327
+ - 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.
328
+
329
+ 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.
330
+
331
+ If any entrypoints contains a Bun shebang (`#!/usr/bin/env bun`) the bundler will default to `target: "bun"` instead of `"browser"`.
332
+
333
+ 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.
334
+
335
+ ---
336
+
337
+ - `node`
338
+ - 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.
339
+
340
+ {% /table %}
341
+
342
+ ### `format`
343
+
344
+ Specifies the module format to be used in the generated bundles.
345
+
346
+ Bun defaults to `"esm"`, and provides experimental support for `"cjs"` and `"iife"`.
347
+
348
+ #### `format: "esm"` - ES Module
349
+
350
+ This is the default format, which supports ES Module syntax including top-level `await`, import.meta, and more.
351
+
352
+ {% codetabs %}
353
+
354
+ ```ts#JavaScript
355
+ await Bun.build({
356
+ entrypoints: ['./index.tsx'],
357
+ outdir: './out',
358
+ format: "esm",
359
+ })
360
+ ```
361
+
362
+ ```bash#CLI
363
+ $ bun build ./index.tsx --outdir ./out --format esm
364
+ ```
365
+
366
+ {% /codetabs %}
367
+
368
+ To use ES Module syntax in browsers, set `format` to `"esm"` and make sure your `<script type="module">` tag has `type="module"` set.
369
+
370
+ #### `format: "cjs"` - CommonJS
371
+
372
+ 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).
373
+
374
+ {% codetabs %}
375
+
376
+ ```ts#JavaScript
377
+ await Bun.build({
378
+ entrypoints: ['./index.tsx'],
379
+ outdir: './out',
380
+ format: "cjs",
381
+ })
382
+ ```
383
+
384
+ ```bash#CLI
385
+ $ bun build ./index.tsx --outdir ./out --format cjs
386
+ ```
387
+
388
+ {% /codetabs %}
389
+
390
+ #### `format: "iife"` - IIFE
391
+
392
+ TODO: document IIFE once we support globalNames.
393
+
394
+ ### `splitting`
395
+
396
+ Whether to enable code splitting.
397
+
398
+ {% codetabs group="a" %}
399
+
400
+ ```ts#JavaScript
401
+ await Bun.build({
402
+ entrypoints: ['./index.tsx'],
403
+ outdir: './out',
404
+ splitting: false, // default
405
+ })
406
+ ```
407
+
408
+ ```bash#CLI
409
+ $ bun build ./index.tsx --outdir ./out --splitting
410
+ ```
411
+
412
+ {% /codetabs %}
413
+
414
+ 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:
415
+
416
+ {% codetabs %}
417
+
418
+ ```ts#entry-a.ts
419
+ import { shared } from './shared.ts';
420
+ ```
421
+
422
+ ```ts#entry-b.ts
423
+ import { shared } from './shared.ts';
424
+ ```
425
+
426
+ ```ts#shared.ts
427
+ export const shared = 'shared';
428
+ ```
429
+
430
+ {% /codetabs %}
431
+
432
+ To bundle `entry-a.ts` and `entry-b.ts` with code-splitting enabled:
433
+
434
+ {% codetabs group="a" %}
435
+
436
+ ```ts#JavaScript
437
+ await Bun.build({
438
+ entrypoints: ['./entry-a.ts', './entry-b.ts'],
439
+ outdir: './out',
440
+ splitting: true,
441
+ })
442
+ ```
443
+
444
+ ```bash#CLI
445
+ $ bun build ./entry-a.ts ./entry-b.ts --outdir ./out --splitting
446
+ ```
447
+
448
+ {% /codetabs %}
449
+
450
+ Running this build will result in the following files:
451
+
452
+ ```txt
453
+ .
454
+ ├── entry-a.tsx
455
+ ├── entry-b.tsx
456
+ ├── shared.tsx
457
+ └── out
458
+ ├── entry-a.js
459
+ ├── entry-b.js
460
+ └── chunk-2fce6291bf86559d.js
461
+
462
+ ```
463
+
464
+ 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).
465
+
466
+ ### `plugins`
467
+
468
+ A list of plugins to use during bundling.
469
+
470
+ {% codetabs group="a" %}
471
+
472
+ ```ts#JavaScript
473
+ await Bun.build({
474
+ entrypoints: ['./index.tsx'],
475
+ outdir: './out',
476
+ plugins: [/* ... */],
477
+ })
478
+ ```
479
+
480
+ ```bash#CLI
481
+ n/a
482
+ ```
483
+
484
+ {% /codetabs %}
485
+
486
+ Bun implements a universal plugin system for both Bun's runtime and bundler. Refer to the [plugin documentation](https://bun.sh/docs/bundler/plugins) for complete documentation.
487
+
488
+ <!-- ### `manifest`
489
+
490
+ Whether to return a build manifest in the result of `Bun.build`.
491
+
492
+ ```ts
493
+ const result = await Bun.build({
494
+ entrypoints: ["./index.tsx"],
495
+ outdir: "./out",
496
+ manifest: true, // default is true
497
+ });
498
+
499
+ console.log(result.manifest);
500
+ ```
501
+
502
+ {% details summary="Manifest structure" %}
503
+
504
+ The manifest has the following form:
505
+
506
+ ```ts
507
+ export type BuildManifest = {
508
+ inputs: {
509
+ [path: string]: {
510
+ output: {
511
+ path: string;
512
+ };
513
+ imports: {
514
+ path: string;
515
+ kind: ImportKind;
516
+ external?: boolean;
517
+ }[];
518
+ };
519
+ };
520
+ outputs: {
521
+ [path: string]: {
522
+ type: "chunk" | "entry-point" | "asset";
523
+ inputs: { path: string }[];
524
+ imports: {
525
+ path: string;
526
+ kind: ImportKind;
527
+ external?: boolean;
528
+ asset?: boolean;
529
+ }[];
530
+ exports: string[];
531
+ };
532
+ };
533
+ };
534
+
535
+ export type ImportKind =
536
+ | "entry-point"
537
+ | "import-statement"
538
+ | "require-call"
539
+ | "dynamic-import"
540
+ | "require-resolve"
541
+ | "import-rule"
542
+ | "url-token";
543
+ ```
544
+
545
+ {% /details %}
546
+
547
+ 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. -->
548
+
549
+ ### `sourcemap`
550
+
551
+ Specifies the type of sourcemap to generate.
552
+
553
+ {% codetabs group="a" %}
554
+
555
+ ```ts#JavaScript
556
+ await Bun.build({
557
+ entrypoints: ['./index.tsx'],
558
+ outdir: './out',
559
+ sourcemap: 'linked', // default 'none'
560
+ })
561
+ ```
562
+
563
+ ```bash#CLI
564
+ $ bun build ./index.tsx --outdir ./out --sourcemap=linked
565
+ ```
566
+
567
+ {% /codetabs %}
568
+
569
+ {% table %}
570
+
571
+ ---
572
+
573
+ - `"none"`
574
+ - _Default._ No sourcemap is generated.
575
+
576
+ ---
577
+
578
+ - `"linked"`
579
+ - 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`.
580
+
581
+ ```ts
582
+ // <bundled code here>
583
+
584
+ //# sourceMappingURL=bundle.js.map
585
+ ```
586
+
587
+ ---
588
+
589
+ - `"external"`
590
+ - A separate `*.js.map` file is created alongside each `*.js` bundle without inserting a `//# sourceMappingURL` comment.
591
+
592
+ {% /table %}
593
+
594
+ {% callout %}
595
+
596
+ 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.
597
+
598
+ ```ts
599
+ // <generated bundle code>
600
+
601
+ //# debugId=<DEBUG ID>
602
+ ```
603
+
604
+ ---
605
+
606
+ - `"inline"`
607
+ - A sourcemap is generated and appended to the end of the generated bundle as a base64 payload.
608
+
609
+ ```ts
610
+ // <bundled code here>
611
+
612
+ //# sourceMappingURL=data:application/json;base64,<encoded sourcemap here>
613
+ ```
614
+
615
+ The associated `*.js.map` sourcemap will be a JSON file containing an equivalent `debugId` property.
616
+
617
+ {% /callout %}
618
+
619
+ ### `minify`
620
+
621
+ Whether to enable minification. Default `false`.
622
+
623
+ {% callout %}
624
+ When targeting `bun`, identifiers will be minified by default.
625
+ {% /callout %}
626
+
627
+ To enable all minification options:
628
+
629
+ {% codetabs group="a" %}
630
+
631
+ ```ts#JavaScript
632
+ await Bun.build({
633
+ entrypoints: ['./index.tsx'],
634
+ outdir: './out',
635
+ minify: true, // default false
636
+ })
637
+ ```
638
+
639
+ ```bash#CLI
640
+ $ bun build ./index.tsx --outdir ./out --minify
641
+ ```
642
+
643
+ {% /codetabs %}
644
+
645
+ To granularly enable certain minifications:
646
+
647
+ {% codetabs group="a" %}
648
+
649
+ ```ts#JavaScript
650
+ await Bun.build({
651
+ entrypoints: ['./index.tsx'],
652
+ outdir: './out',
653
+ minify: {
654
+ whitespace: true,
655
+ identifiers: true,
656
+ syntax: true,
657
+ },
658
+ })
659
+ ```
660
+
661
+ ```bash#CLI
662
+ $ bun build ./index.tsx --outdir ./out --minify-whitespace --minify-identifiers --minify-syntax
663
+ ```
664
+
665
+ {% /codetabs %}
666
+
667
+ <!-- ### `treeshaking`
668
+
669
+ boolean; -->
670
+
671
+ ### `external`
672
+
673
+ A list of import paths to consider _external_. Defaults to `[]`.
674
+
675
+ {% codetabs group="a" %}
676
+
677
+ ```ts#JavaScript
678
+ await Bun.build({
679
+ entrypoints: ['./index.tsx'],
680
+ outdir: './out',
681
+ external: ["lodash", "react"], // default: []
682
+ })
683
+ ```
684
+
685
+ ```bash#CLI
686
+ $ bun build ./index.tsx --outdir ./out --external lodash --external react
687
+ ```
688
+
689
+ {% /codetabs %}
690
+
691
+ 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.
692
+
693
+ For instance, consider the following entrypoint file:
694
+
695
+ ```ts#index.tsx
696
+ import _ from "lodash";
697
+ import {z} from "zod";
698
+
699
+ const value = z.string().parse("Hello world!")
700
+ console.log(_.upperCase(value));
701
+ ```
702
+
703
+ 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:
704
+
705
+ {% codetabs group="a" %}
706
+
707
+ ```ts#JavaScript
708
+ await Bun.build({
709
+ entrypoints: ['./index.tsx'],
710
+ outdir: './out',
711
+ external: ['zod'],
712
+ })
713
+ ```
714
+
715
+ ```bash#CLI
716
+ $ bun build ./index.tsx --outdir ./out --external zod
717
+ ```
718
+
719
+ {% /codetabs %}
720
+
721
+ The generated bundle will look something like this:
722
+
723
+ ```js#out/index.js
724
+ import {z} from "zod";
725
+
726
+ // ...
727
+ // the contents of the "lodash" package
728
+ // including the `_.upperCase` function
729
+
730
+ var value = z.string().parse("Hello world!")
731
+ console.log(_.upperCase(value));
732
+ ```
733
+
734
+ To mark all imports as external, use the wildcard `*`:
735
+
736
+ {% codetabs %}
737
+
738
+ ```ts#JavaScript
739
+ await Bun.build({
740
+ entrypoints: ['./index.tsx'],
741
+ outdir: './out',
742
+ external: ['*'],
743
+ })
744
+ ```
745
+
746
+ ```bash#CLI
747
+ $ bun build ./index.tsx --outdir ./out --external '*'
748
+ ```
749
+
750
+ {% /codetabs %}
751
+
752
+ ### `packages`
753
+
754
+ 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.
755
+
756
+ {% codetabs group="a" %}
757
+
758
+ ```ts#JavaScript
759
+ await Bun.build({
760
+ entrypoints: ['./index.ts'],
761
+ packages: 'external',
762
+ })
763
+ ```
764
+
765
+ ```bash#CLI
766
+ $ bun build ./index.ts --packages external
767
+ ```
768
+
769
+ {% /codetabs %}
770
+
771
+ ### `naming`
772
+
773
+ Customizes the generated file names. Defaults to `./[dir]/[name].[ext]`.
774
+
775
+ {% codetabs group="a" %}
776
+
777
+ ```ts#JavaScript
778
+ await Bun.build({
779
+ entrypoints: ['./index.tsx'],
780
+ outdir: './out',
781
+ naming: "[dir]/[name].[ext]", // default
782
+ })
783
+ ```
784
+
785
+ ```bash#CLI
786
+ $ bun build ./index.tsx --outdir ./out --entry-naming [dir]/[name].[ext]
787
+ ```
788
+
789
+ {% /codetabs %}
790
+
791
+ By default, the names of the generated bundles are based on the name of the associated entrypoint.
792
+
793
+ ```txt
794
+ .
795
+ ├── index.tsx
796
+ └── out
797
+ └── index.js
798
+ ```
799
+
800
+ With multiple entrypoints, the generated file hierarchy will reflect the directory structure of the entrypoints.
801
+
802
+ ```txt
803
+ .
804
+ ├── index.tsx
805
+ └── nested
806
+ └── index.tsx
807
+ └── out
808
+ ├── index.js
809
+ └── nested
810
+ └── index.js
811
+ ```
812
+
813
+ 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:
814
+
815
+ - `[name]` - The name of the entrypoint file, without the extension.
816
+ - `[ext]` - The extension of the generated bundle.
817
+ - `[hash]` - A hash of the bundle contents.
818
+ - `[dir]` - The relative path from the project root to the parent directory of the source file.
819
+
820
+ For example:
821
+
822
+ {% table %}
823
+
824
+ - Token
825
+ - `[name]`
826
+ - `[ext]`
827
+ - `[hash]`
828
+ - `[dir]`
829
+
830
+ ---
831
+
832
+ - `./index.tsx`
833
+ - `index`
834
+ - `js`
835
+ - `a1b2c3d4`
836
+ - `""` (empty string)
837
+
838
+ ---
839
+
840
+ - `./nested/entry.ts`
841
+ - `entry`
842
+ - `js`
843
+ - `c3d4e5f6`
844
+ - `"nested"`
845
+
846
+ {% /table %}
847
+
848
+ We can combine these tokens to create a template string. For instance, to include the hash in the generated bundle names:
849
+
850
+ {% codetabs group="a" %}
851
+
852
+ ```ts#JavaScript
853
+ await Bun.build({
854
+ entrypoints: ['./index.tsx'],
855
+ outdir: './out',
856
+ naming: 'files/[dir]/[name]-[hash].[ext]',
857
+ })
858
+ ```
859
+
860
+ ```bash#CLI
861
+ $ bun build ./index.tsx --outdir ./out --entry-naming [name]-[hash].[ext]
862
+ ```
863
+
864
+ {% /codetabs %}
865
+
866
+ This build would result in the following file structure:
867
+
868
+ ```txt
869
+ .
870
+ ├── index.tsx
871
+ └── out
872
+ └── files
873
+ └── index-a1b2c3d4.js
874
+ ```
875
+
876
+ 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.
877
+
878
+ {% codetabs group="a" %}
879
+
880
+ ```ts#JavaScript
881
+ await Bun.build({
882
+ entrypoints: ['./index.tsx'],
883
+ outdir: './out',
884
+ naming: {
885
+ // default values
886
+ entry: '[dir]/[name].[ext]',
887
+ chunk: '[name]-[hash].[ext]',
888
+ asset: '[name]-[hash].[ext]',
889
+ },
890
+ })
891
+ ```
892
+
893
+ ```bash#CLI
894
+ $ bun build ./index.tsx --outdir ./out --entry-naming "[dir]/[name].[ext]" --chunk-naming "[name]-[hash].[ext]" --asset-naming "[name]-[hash].[ext]"
895
+ ```
896
+
897
+ {% /codetabs %}
898
+
899
+ ### `root`
900
+
901
+ The root directory of the project.
902
+
903
+ {% codetabs group="a" %}
904
+
905
+ ```ts#JavaScript
906
+ await Bun.build({
907
+ entrypoints: ['./pages/a.tsx', './pages/b.tsx'],
908
+ outdir: './out',
909
+ root: '.',
910
+ })
911
+ ```
912
+
913
+ ```bash#CLI
914
+ n/a
915
+ ```
916
+
917
+ {% /codetabs %}
918
+
919
+ If unspecified, it is computed to be the first common ancestor of all entrypoint files. Consider the following file structure:
920
+
921
+ ```txt
922
+ .
923
+ └── pages
924
+ └── index.tsx
925
+ └── settings.tsx
926
+ ```
927
+
928
+ We can build both entrypoints in the `pages` directory:
929
+
930
+ {% codetabs group="a" %}
931
+
932
+ ```ts#JavaScript
933
+ await Bun.build({
934
+ entrypoints: ['./pages/index.tsx', './pages/settings.tsx'],
935
+ outdir: './out',
936
+ })
937
+ ```
938
+
939
+ ```bash#CLI
940
+ $ bun build ./pages/index.tsx ./pages/settings.tsx --outdir ./out
941
+ ```
942
+
943
+ {% /codetabs %}
944
+
945
+ This would result in a file structure like this:
946
+
947
+ ```txt
948
+ .
949
+ └── pages
950
+ └── index.tsx
951
+ └── settings.tsx
952
+ └── out
953
+ └── index.js
954
+ └── settings.js
955
+ ```
956
+
957
+ 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.
958
+
959
+ This behavior can be overridden by specifying the `root` option:
960
+
961
+ {% codetabs group="a" %}
962
+
963
+ ```ts#JavaScript
964
+ await Bun.build({
965
+ entrypoints: ['./pages/index.tsx', './pages/settings.tsx'],
966
+ outdir: './out',
967
+ root: '.',
968
+ })
969
+ ```
970
+
971
+ ```bash#CLI
972
+ $ bun build ./pages/index.tsx ./pages/settings.tsx --outdir ./out --root .
973
+ ```
974
+
975
+ {% /codetabs %}
976
+
977
+ By specifying `.` as `root`, the generated file structure will look like this:
978
+
979
+ ```txt
980
+ .
981
+ └── pages
982
+ └── index.tsx
983
+ └── settings.tsx
984
+ └── out
985
+ └── pages
986
+ └── index.js
987
+ └── settings.js
988
+ ```
989
+
990
+ ### `publicPath`
991
+
992
+ A prefix to be appended to any import paths in bundled code.
993
+
994
+ 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.
995
+
996
+ - **Asset imports** — When importing an unrecognized file type like `*.svg`, the bundler defers to the [`file` loader](https://bun.sh/docs/bundler/loaders#file), which copies the file into `outdir` as is. The import is converted into a variable
997
+ - **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.
998
+ - **Chunking**. When [`splitting`](#splitting) is enabled, the bundler may generate separate "chunk" files that represent code that is shared among multiple entrypoints.
999
+
1000
+ 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:
1001
+
1002
+ {% codetabs %}
1003
+
1004
+ ```ts#Input
1005
+ import logo from './logo.svg';
1006
+ console.log(logo);
1007
+ ```
1008
+
1009
+ ```ts#Output
1010
+ // logo.svg is copied into <outdir>
1011
+ // and hash is added to the filename to prevent collisions
1012
+ var logo = './logo-a7305bdef.svg';
1013
+ console.log(logo);
1014
+ ```
1015
+
1016
+ {% /codetabs %}
1017
+
1018
+ Setting `publicPath` will prefix all file paths with the specified value.
1019
+
1020
+ {% codetabs group="a" %}
1021
+
1022
+ ```ts#JavaScript
1023
+ await Bun.build({
1024
+ entrypoints: ['./index.tsx'],
1025
+ outdir: './out',
1026
+ publicPath: 'https://cdn.example.com/', // default is undefined
1027
+ })
1028
+ ```
1029
+
1030
+ ```bash#CLI
1031
+ $ bun build ./index.tsx --outdir ./out --public-path https://cdn.example.com/
1032
+ ```
1033
+
1034
+ {% /codetabs %}
1035
+
1036
+ The output file would now look something like this.
1037
+
1038
+ ```ts-diff#Output
1039
+ - var logo = './logo-a7305bdef.svg';
1040
+ + var logo = 'https://cdn.example.com/logo-a7305bdef.svg';
1041
+ ```
1042
+
1043
+ ### `define`
1044
+
1045
+ 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.
1046
+
1047
+ {% callout }
1048
+ This is not needed to inline `process.env.NODE_ENV`, as Bun does this automatically.
1049
+ {% /callout %}
1050
+
1051
+ {% codetabs %}
1052
+
1053
+ ```ts#JavaScript
1054
+ await Bun.build({
1055
+ entrypoints: ['./index.tsx'],
1056
+ outdir: './out',
1057
+ define: {
1058
+ STRING: JSON.stringify("value"),
1059
+ "nested.boolean": "true",
1060
+ },
1061
+ })
1062
+ ```
1063
+
1064
+ ```bash#CLI
1065
+ $ bun build ./index.tsx --outdir ./out --define 'STRING="value"' --define "nested.boolean=true"
1066
+ ```
1067
+
1068
+ {% /codetabs %}
1069
+
1070
+ ### `loader`
1071
+
1072
+ A map of file extensions to [built-in loader names](https://bun.sh/docs/bundler/loaders#built-in-loaders). This can be used to quickly customize how certain files are loaded.
1073
+
1074
+ {% codetabs %}
1075
+
1076
+ ```ts#JavaScript
1077
+ await Bun.build({
1078
+ entrypoints: ['./index.tsx'],
1079
+ outdir: './out',
1080
+ loader: {
1081
+ ".png": "dataurl",
1082
+ ".txt": "file",
1083
+ },
1084
+ })
1085
+ ```
1086
+
1087
+ ```bash#CLI
1088
+ $ bun build ./index.tsx --outdir ./out --loader .png:dataurl --loader .txt:file
1089
+ ```
1090
+
1091
+ {% /codetabs %}
1092
+
1093
+ ### `banner`
1094
+
1095
+ 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.
1096
+
1097
+ {% codetabs %}
1098
+
1099
+ ```ts#JavaScript
1100
+ await Bun.build({
1101
+ entrypoints: ['./index.tsx'],
1102
+ outdir: './out',
1103
+ banner: '"use client";'
1104
+ })
1105
+ ```
1106
+
1107
+ ```bash#CLI
1108
+ $ bun build ./index.tsx --outdir ./out --banner "\"use client\";"
1109
+ ```
1110
+
1111
+ {% /codetabs %}
1112
+
1113
+ ### `footer`
1114
+
1115
+ 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.
1116
+
1117
+ {% codetabs %}
1118
+
1119
+ ```ts#JavaScript
1120
+ await Bun.build({
1121
+ entrypoints: ['./index.tsx'],
1122
+ outdir: './out',
1123
+ footer: '// built with love in SF'
1124
+ })
1125
+ ```
1126
+
1127
+ ```bash#CLI
1128
+ $ bun build ./index.tsx --outdir ./out --footer="// built with love in SF"
1129
+ ```
1130
+
1131
+ {% /codetabs %}
1132
+
1133
+ ### `drop`
1134
+
1135
+ 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.
1136
+
1137
+ {% codetabs %}
1138
+
1139
+ ```ts#JavaScript
1140
+ await Bun.build({
1141
+ entrypoints: ['./index.tsx'],
1142
+ outdir: './out',
1143
+ drop: ["console", "debugger", "anyIdentifier.or.propertyAccess"],
1144
+ })
1145
+ ```
1146
+
1147
+ ```bash#CLI
1148
+ $ bun build ./index.tsx --outdir ./out --drop=console --drop=debugger --drop=anyIdentifier.or.propertyAccess
1149
+ ```
1150
+
1151
+ {% /codetabs %}
1152
+
1153
+ ### `experimentalCss`
1154
+
1155
+ Whether to enable _experimental_ support for bundling CSS files. Defaults to `false`.
1156
+
1157
+ This supports bundling CSS files imported from JS, as well as CSS entrypoints.
1158
+
1159
+ {% codetabs group="a" %}
1160
+
1161
+ ```ts#JavaScript
1162
+ const result = await Bun.build({
1163
+ entrypoints: ["./index.ts"],
1164
+ experimentalCss: true,
1165
+ });
1166
+ // => { success: boolean, outputs: BuildArtifact[], logs: BuildMessage[] }
1167
+ ```
1168
+
1169
+ {% /codetabs %}
1170
+
1171
+ ## Outputs
1172
+
1173
+ The `Bun.build` function returns a `Promise<BuildOutput>`, defined as:
1174
+
1175
+ ```ts
1176
+ interface BuildOutput {
1177
+ outputs: BuildArtifact[];
1178
+ success: boolean;
1179
+ logs: Array<object>; // see docs for details
1180
+ }
1181
+
1182
+ interface BuildArtifact extends Blob {
1183
+ kind: "entry-point" | "chunk" | "asset" | "sourcemap";
1184
+ path: string;
1185
+ loader: Loader;
1186
+ hash: string | null;
1187
+ sourcemap: BuildArtifact | null;
1188
+ }
1189
+ ```
1190
+
1191
+ The `outputs` array contains all the files that were generated by the build. Each artifact implements the `Blob` interface.
1192
+
1193
+ ```ts
1194
+ const build = await Bun.build({
1195
+ /* */
1196
+ });
1197
+
1198
+ for (const output of build.outputs) {
1199
+ await output.arrayBuffer(); // => ArrayBuffer
1200
+ await output.bytes(); // => Uint8Array
1201
+ await output.text(); // string
1202
+ }
1203
+ ```
1204
+
1205
+ Each artifact also contains the following properties:
1206
+
1207
+ {% table %}
1208
+
1209
+ ---
1210
+
1211
+ - `kind`
1212
+ - What kind of build output this file is. A build generates bundled entrypoints, code-split "chunks", sourcemaps, bytecode, and copied assets (like images).
1213
+
1214
+ ---
1215
+
1216
+ - `path`
1217
+ - Absolute path to the file on disk
1218
+
1219
+ ---
1220
+
1221
+ - `loader`
1222
+ - The loader was used to interpret the file. See [Bundler > Loaders](https://bun.sh/docs/bundler/loaders) to see how Bun maps file extensions to the appropriate built-in loader.
1223
+
1224
+ ---
1225
+
1226
+ - `hash`
1227
+ - The hash of the file contents. Always defined for assets.
1228
+
1229
+ ---
1230
+
1231
+ - `sourcemap`
1232
+ - The sourcemap file corresponding to this file, if generated. Only defined for entrypoints and chunks.
1233
+
1234
+ {% /table %}
1235
+
1236
+ Similar to `BunFile`, `BuildArtifact` objects can be passed directly into `new Response()`.
1237
+
1238
+ ```ts
1239
+ const build = await Bun.build({
1240
+ /* */
1241
+ });
1242
+
1243
+ const artifact = build.outputs[0];
1244
+
1245
+ // Content-Type header is automatically set
1246
+ return new Response(artifact);
1247
+ ```
1248
+
1249
+ The Bun runtime implements special pretty-printing of `BuildArtifact` object to make debugging easier.
1250
+
1251
+ {% codetabs %}
1252
+
1253
+ ```ts#Build_script
1254
+ // build.ts
1255
+ const build = await Bun.build({/* */});
1256
+
1257
+ const artifact = build.outputs[0];
1258
+ console.log(artifact);
1259
+ ```
1260
+
1261
+ ```sh#Shell_output
1262
+ $ bun run build.ts
1263
+ BuildArtifact (entry-point) {
1264
+ path: "./index.js",
1265
+ loader: "tsx",
1266
+ kind: "entry-point",
1267
+ hash: "824a039620219640",
1268
+ Blob (114 bytes) {
1269
+ type: "text/javascript;charset=utf-8"
1270
+ },
1271
+ sourcemap: null
1272
+ }
1273
+ ```
1274
+
1275
+ {% /codetabs %}
1276
+
1277
+ ### Bytecode
1278
+
1279
+ 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.
1280
+
1281
+ {% codetabs %}
1282
+
1283
+ ```ts#JavaScript
1284
+ await Bun.build({
1285
+ entrypoints: ["./index.tsx"],
1286
+ outdir: "./out",
1287
+ bytecode: true,
1288
+ })
1289
+ ```
1290
+
1291
+ ```bash#CLI
1292
+ $ bun build ./index.tsx --outdir ./out --bytecode
1293
+ ```
1294
+
1295
+ {% /codetabs %}
1296
+
1297
+ ### Executables
1298
+
1299
+ Bun supports "compiling" a JavaScript/TypeScript entrypoint into a standalone executable. This executable contains a copy of the Bun binary.
1300
+
1301
+ ```sh
1302
+ $ bun build ./cli.tsx --outfile mycli --compile
1303
+ $ ./mycli
1304
+ ```
1305
+
1306
+ Refer to [Bundler > Executables](https://bun.sh/docs/bundler/executables) for complete documentation.
1307
+
1308
+ ## Logs and errors
1309
+
1310
+ `Bun.build` only throws if invalid options are provided. Read the `success` property to determine if the build was successful; the `logs` property will contain additional details.
1311
+
1312
+ ```ts
1313
+ const result = await Bun.build({
1314
+ entrypoints: ["./index.tsx"],
1315
+ outdir: "./out",
1316
+ });
1317
+
1318
+ if (!result.success) {
1319
+ console.error("Build failed");
1320
+ for (const message of result.logs) {
1321
+ // Bun will pretty print the message object
1322
+ console.error(message);
1323
+ }
1324
+ }
1325
+ ```
1326
+
1327
+ Each message is either a `BuildMessage` or `ResolveMessage` object, which can be used to trace what problems happened in the build.
1328
+
1329
+ ```ts
1330
+ class BuildMessage {
1331
+ name: string;
1332
+ position?: Position;
1333
+ message: string;
1334
+ level: "error" | "warning" | "info" | "debug" | "verbose";
1335
+ }
1336
+
1337
+ class ResolveMessage extends BuildMessage {
1338
+ code: string;
1339
+ referrer: string;
1340
+ specifier: string;
1341
+ importKind: ImportKind;
1342
+ }
1343
+ ```
1344
+
1345
+ If you want to throw an error from a failed build, consider passing the logs to an `AggregateError`. If uncaught, Bun will pretty-print the contained messages nicely.
1346
+
1347
+ ```ts
1348
+ if (!result.success) {
1349
+ throw new AggregateError(result.logs, "Build failed");
1350
+ }
1351
+ ```
1352
+
1353
+ ## Reference
1354
+
1355
+ ```ts
1356
+ interface Bun {
1357
+ build(options: BuildOptions): Promise<BuildOutput>;
1358
+ }
1359
+
1360
+ interface BuildConfig {
1361
+ entrypoints: string[]; // list of file path
1362
+ outdir?: string; // output directory
1363
+ target?: Target; // default: "browser"
1364
+ /**
1365
+ * Output module format. Top-level await is only supported for `"esm"`.
1366
+ *
1367
+ * Can be:
1368
+ * - `"esm"`
1369
+ * - `"cjs"` (**experimental**)
1370
+ * - `"iife"` (**experimental**)
1371
+ *
1372
+ * @default "esm"
1373
+ */
1374
+ format?: /**
1375
+
1376
+ * ECMAScript Module format
1377
+ */
1378
+ | "esm"
1379
+ /**
1380
+ * CommonJS format
1381
+ * **Experimental**
1382
+ */
1383
+ | "cjs"
1384
+ /**
1385
+ * IIFE format
1386
+ * **Experimental**
1387
+ */
1388
+ | "iife";
1389
+ naming?:
1390
+ | string
1391
+ | {
1392
+ chunk?: string;
1393
+ entry?: string;
1394
+ asset?: string;
1395
+ }; // | string;
1396
+ root?: string; // project root
1397
+ splitting?: boolean; // default true, enable code splitting
1398
+ plugins?: BunPlugin[];
1399
+ // manifest?: boolean; // whether to return manifest
1400
+ external?: string[];
1401
+ packages?: "bundle" | "external";
1402
+ publicPath?: string;
1403
+ define?: Record<string, string>;
1404
+ // origin?: string; // e.g. http://mydomain.com
1405
+ loader?: { [k in string]: Loader };
1406
+ sourcemap?: "none" | "linked" | "inline" | "external" | "linked"; // default: "none", true -> "inline"
1407
+ /**
1408
+ * package.json `exports` conditions used when resolving imports
1409
+ *
1410
+ * Equivalent to `--conditions` in `bun build` or `bun run`.
1411
+ *
1412
+ * https://nodejs.org/api/packages.html#exports
1413
+ */
1414
+ conditions?: Array<string> | string;
1415
+ minify?:
1416
+ | boolean
1417
+ | {
1418
+ whitespace?: boolean;
1419
+ syntax?: boolean;
1420
+ identifiers?: boolean;
1421
+ };
1422
+ /**
1423
+ * Ignore dead code elimination/tree-shaking annotations such as @__PURE__ and package.json
1424
+ * "sideEffects" fields. This should only be used as a temporary workaround for incorrect
1425
+ * annotations in libraries.
1426
+ */
1427
+ ignoreDCEAnnotations?: boolean;
1428
+ /**
1429
+ * Force emitting @__PURE__ annotations even if minify.whitespace is true.
1430
+ */
1431
+ emitDCEAnnotations?: boolean;
1432
+ // treeshaking?: boolean;
1433
+
1434
+ // jsx?:
1435
+ // | "automatic"
1436
+ // | "classic"
1437
+ // | /* later: "preserve" */ {
1438
+ // runtime?: "automatic" | "classic"; // later: "preserve"
1439
+ // /** Only works when runtime=classic */
1440
+ // factory?: string; // default: "React.createElement"
1441
+ // /** Only works when runtime=classic */
1442
+ // fragment?: string; // default: "React.Fragment"
1443
+ // /** Only works when runtime=automatic */
1444
+ // importSource?: string; // default: "react"
1445
+ // };
1446
+
1447
+ /**
1448
+ * Generate bytecode for the output. This can dramatically improve cold
1449
+ * start times, but will make the final output larger and slightly increase
1450
+ * memory usage.
1451
+ *
1452
+ * Bytecode is currently only supported for CommonJS (`format: "cjs"`).
1453
+ *
1454
+ * Must be `target: "bun"`
1455
+ * @default false
1456
+ */
1457
+ bytecode?: boolean;
1458
+ }
1459
+
1460
+ interface BuildOutput {
1461
+ outputs: BuildArtifact[];
1462
+ success: boolean;
1463
+ logs: Array<BuildMessage | ResolveMessage>;
1464
+ }
1465
+
1466
+ interface BuildArtifact extends Blob {
1467
+ path: string;
1468
+ loader: Loader;
1469
+ hash: string | null;
1470
+ kind: "entry-point" | "chunk" | "asset" | "sourcemap" | "bytecode";
1471
+ sourcemap: BuildArtifact | null;
1472
+ }
1473
+
1474
+ type Loader =
1475
+ | "js"
1476
+ | "jsx"
1477
+ | "ts"
1478
+ | "tsx"
1479
+ | "json"
1480
+ | "toml"
1481
+ | "file"
1482
+ | "napi"
1483
+ | "wasm"
1484
+ | "text";
1485
+
1486
+ interface BuildOutput {
1487
+ outputs: BuildArtifact[];
1488
+ success: boolean;
1489
+ logs: Array<BuildMessage | ResolveMessage>;
1490
+ }
1491
+
1492
+ declare class ResolveMessage {
1493
+ readonly name: "ResolveMessage";
1494
+ readonly position: Position | null;
1495
+ readonly code: string;
1496
+ readonly message: string;
1497
+ readonly referrer: string;
1498
+ readonly specifier: string;
1499
+ readonly importKind:
1500
+ | "entry_point"
1501
+ | "stmt"
1502
+ | "require"
1503
+ | "import"
1504
+ | "dynamic"
1505
+ | "require_resolve"
1506
+ | "at"
1507
+ | "at_conditional"
1508
+ | "url"
1509
+ | "internal";
1510
+ readonly level: "error" | "warning" | "info" | "debug" | "verbose";
1511
+
1512
+ toString(): string;
1513
+ }
1514
+ ```
1515
+
1516
+ <!--
1517
+ interface BuildManifest {
1518
+ inputs: {
1519
+ [path: string]: {
1520
+ output: {
1521
+ path: string;
1522
+ };
1523
+ imports: {
1524
+ path: string;
1525
+ kind: ImportKind;
1526
+ external?: boolean;
1527
+ asset?: boolean; // whether the import defaulted to "file" loader
1528
+ }[];
1529
+ };
1530
+ };
1531
+ outputs: {
1532
+ [path: string]: {
1533
+ type: "chunk" | "entrypoint" | "asset";
1534
+ inputs: { path: string }[];
1535
+ imports: {
1536
+ path: string;
1537
+ kind: ImportKind;
1538
+ external?: boolean;
1539
+ }[];
1540
+ exports: string[];
1541
+ };
1542
+ };
1543
+ } -->