new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Bun · Bundler · all subjects

js api

31 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Bun.build() absWorkingDir always set to process.cwd()

In Bun.build(), `absWorkingDir` is always set to `process.cwd()`. This esbuild option is not configurable.

Bun.build() allowOverwrite always false

In Bun.build(), `allowOverwrite` is always false. Overwriting is never allowed in Bun.

Bun.build() entrypoints capitalization difference

In Bun.build(), the parameter is called `entrypoints` (lowercase 'p'), whereas in esbuild.build() it is `entryPoints` (camelCase).

Bun.build() naming.asset replaces assetNames

In Bun.build(), asset naming uses `naming.asset` instead of esbuild's `assetNames`. Uses the same templating syntax as esbuild, but `[ext]` must be included explicitly. Example: `Bun.build({ entrypoints: ["./index.tsx"], naming: { asset: "[name].[ext]" } })`.

Bun.build() naming.chunk replaces chunkNames

In Bun.build(), chunk naming uses `naming.chunk` instead of esbuild's `chunkNames`. Uses the same templating syntax as esbuild, but `[ext]` must be included explicitly. Example: `Bun.build({ entrypoints: ["./index.tsx"], naming: { chunk: "[name].[ext]" } })`.

Bun.build() bundle always true

In Bun.build(), `bundle` is always true. To transpile without bundling, use `Bun.Transpiler` instead.

Bun.build() logs returned in build result

In Bun.build(), logs are returned in the `logs` property of the build result, rather than being output directly. The esbuild `color` option is not applicable.

Bun.build() naming object structure

In Bun.build(), `naming` can be either a string (equivalent to entryNames) or an object with granular options: `{ entry: "[name].[ext]", asset: "[name].[ext]", chunk: "[name].[ext]" }`.

Bun.build() jsx.runtime, jsx.factory, jsx.fragment, jsx.importSource, jsx.development

In Bun.build(), JSX options are nested under a `jsx` object: `jsx.runtime`, `jsx.factory`, `jsx.fragment`, `jsx.importSource`, and `jsx.development`, whereas esbuild uses top-level `jsx`, `jsxFactory`, `jsxFragment`, `jsxImportSource`, and `jsxDev`.

Bun.build() minify boolean or object

In Bun.build(), `minify` can be a boolean or an object with granular options. Example: `minify: true` or `minify: { identifiers: true, syntax: true, whitespace: true }`.

Bun.build() minify.keepNames replaces keepNames

In Bun.build(), the `keepNames` option is nested as `minify.keepNames`.

Bun.build() root replaces outbase

In Bun.build(), `root` is used instead of esbuild's `outbase`.

Bun.build() target replaces platform

In Bun.build(), `target` is used instead of esbuild's `platform`. Supports `"bun"`, `"node"` and `"browser"` (the default). Does not support `"neutral"`.

Bun.build() plugins subset of esbuild

In Bun.build(), the `plugins` API is a subset of esbuild's. Some esbuild plugins work with Bun without modification.

Bun.build() sourcemap values

In Bun.build(), `sourcemap` supports values: `"none"`, `"linked"`, `"inline"`, and `"external"`.

Bun.build() conditions, define, drop, external, format, metafile, publicPath, splitting supported

In Bun.build(), these options are supported with no differences from esbuild: `conditions`, `define`, `drop`, `external`, `format`, `metafile`, `publicPath`, and `splitting`.

Environment variable inlining in production build API

When using the Bun.build API, set the `env` option to inline environment variables: `env: "inline"` to inline all environment variables, or `env: "PUBLIC_*"` to only inline vars with a specific prefix (recommended).

Production build with Bun.build API

Use the Bun.build API for production builds: `await Bun.build({ entrypoints: ["./index.html"], outdir: "./dist", minify: true });`

Bun.build() for standalone HTML

Use the JavaScript API `await Bun.build({ entrypoints: ["./index.html"], compile: true, target: "browser", outdir: "./dist", minify: true })` to produce standalone HTML programmatically. When outdir is omitted, output is available as BuildArtifact in result.outputs and can be accessed via result.outputs[0].text().

Bun.serve() routes option with HTML imports

To get started with fullstack dev, import HTML files and pass them to the `routes` option in `Bun.serve()`. When you make a request to a route mapped to an HTML file, Bun automatically bundles the `<script>` and `<link>` tags in the HTML files, exposes them as static routes, and serves the result.

HTML import syntax for routes

Import HTML files into your JavaScript/TypeScript/TSX/JSX files using standard ES module syntax: `import dashboard from "./dashboard.html"; import homepage from "./index.html";`. Then pass the imported HTML objects as routes to `Bun.serve()`.

API endpoints in Bun.serve() routes

Define API endpoints with HTTP method handlers in the routes object. Each route can have GET, POST, PUT, DELETE methods as async functions. API routes require Bun v1.2.3+.

Dynamic routes with URL parameters

Use URL parameters in routes with the `:parameter` syntax. Single parameter example: `/api/users/:id`. Multiple parameters: `/api/users/:userId/posts/:postId`. Wildcard routes: `/api/files/*`. Access parameters via `req.params`.

Bun.serve() development option features

When `development: true`, Bun includes SourceMap header in responses, disables minification, re-bundles assets on each request to an .html file, and enables hot module reloading (unless `hmr: false` is set).

Bun.serve() development as object with console logging

The `development` option can be an object with `hmr: true` to enable Hot Module Reloading and `console: true` to echo console logs from the browser to the terminal. Logs are sent over the existing HMR WebSocket connection.

Request object properties in API routes

In API route handlers, access: `req.json()` for parsed JSON body, `req.headers.get()` for headers, `req.params` for URL parameters, `new URL(req.url).searchParams` for query parameters. Returns are Response objects using `Response.json()`.

React integration in Bun fullstack apps

To use React, import `react-dom/client` in frontend code and render your app: `import { createRoot } from "react-dom/client"; const root = createRoot(container); root.render(<App />);`. Reference the React component file in an HTML `<script type="module">` tag.

Bun.serve() fetch fallback option

Pass a `fetch` function to `Bun.serve()` as a fallback handler for unmatched routes that are not defined in the `routes` object. This allows handling API requests and other dynamic behavior.

HTTP method handlers in API routes support all standard methods

API routes in `Bun.serve()` support GET, POST, PUT, and DELETE HTTP methods as async functions. Each method receives the request object and must return a Response.

API route object syntax with method handlers

Routes can map to an object with method handlers: `"/api/users": { async GET(req) { ... }, async POST(req) { ... } }`. Alternatively, routes can be async functions: `"/api/users/:id": async req => { ... }`.

Bun.serve() return value

Bun.serve() returns a server object with a `url` property that contains the server's address and port.

Give your agent this brain