Skip to content
Open
91 changes: 56 additions & 35 deletions docs/bundler/loaders.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ const pkg = {
pkg.name;
```

If you pass a `.json` file as an entrypoint to the bundler, Bun converts it to a `.js` module that `export default`s the parsed object.
If you pass a `.json` file as an entrypoint to the bundler, Bun converts it to a `.js` module. The default export is the parsed object. Each top-level key that is a valid identifier is also a named export.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

<CodeGroup>

```json Input
```json data.json
{
"name": "John Doe",
"age": 35,
Expand All @@ -94,11 +94,16 @@ If you pass a `.json` file as an entrypoint to the bundler, Bun converts it to a
```

```js Output
export default {
name: "John Doe",
age: 35,
email: "johndoe@example.com",
// data.json
var name = "John Doe";
var age = 35;
var email = "johndoe@example.com";
var data_default = {
name,
age,
email,
};
export { age, data_default as default, email, name };
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
```

</CodeGroup>
Expand Down Expand Up @@ -154,22 +159,27 @@ var config = {
config.logLevel;
```

If you pass a `.toml` file as an entrypoint, Bun converts it to a `.js` module that `export default`s the parsed object.
If you pass a `.toml` file as an entrypoint, Bun converts it to a `.js` module. The default export is the parsed object. Each top-level key that is a valid identifier is also a named export.

<CodeGroup>

```toml Input
```toml data.toml
name = "John Doe"
age = 35
email = "johndoe@example.com"
```

```js Output
export default {
name: "John Doe",
age: 35,
email: "johndoe@example.com",
// data.toml
var name = "John Doe";
var age = 35;
var email = "johndoe@example.com";
var data_default = {
name,
age,
email,
};
export { age, data_default as default, email, name };
```

</CodeGroup>
Expand Down Expand Up @@ -200,22 +210,27 @@ var config = {
};
```

If you pass a `.yaml` or `.yml` file as an entrypoint, Bun converts it to a `.js` module that `export default`s the parsed object.
If you pass a `.yaml` or `.yml` file as an entrypoint, Bun converts it to a `.js` module. The default export is the parsed object. Each top-level key that is a valid identifier is also a named export.

<CodeGroup>

```yaml Input
```yaml data.yaml
name: John Doe
age: 35
email: johndoe@example.com
```

```js Output
export default {
name: "John Doe",
age: 35,
email: "johndoe@example.com",
// data.yaml
var name = "John Doe";
var age = 35;
var email = "johndoe@example.com";
var data_default = {
name,
age,
email,
};
export { age, data_default as default, email, name };
```

</CodeGroup>
Expand Down Expand Up @@ -253,11 +268,11 @@ var doc = {
};
```

If you pass a `.xml` file as an entrypoint, Bun converts it to a `.js` module that `export default`s the parsed object.
If you pass a `.xml` file as an entrypoint, Bun converts it to a `.js` module. The default export is the parsed object. The root element's name is also a named export when it is a valid identifier.

<CodeGroup>

```xml Input
```xml data.xml
<user id="1">
<name>John Doe</name>
<email>johndoe@example.com</email>
Expand All @@ -267,14 +282,17 @@ If you pass a `.xml` file as an entrypoint, Bun converts it to a `.js` module th
```

```ts Output
export default {
user: {
"@id": "1",
name: "John Doe",
email: "johndoe@example.com",
role: ["admin", "editor"],
},
// data.xml
var user = {
"@id": "1",
name: "John Doe",
email: "johndoe@example.com",
role: ["admin", "editor"],
};
var data_default = {
user,
};
export { data_default as default, user };
```

</CodeGroup>
Expand Down Expand Up @@ -490,20 +508,23 @@ bun run logo.ts
# Output: /path/to/project/logo.svg
```

In the bundler, Bun copies the file into `outdir` as-is, and the import resolves to a relative path pointing to the copied file.
In the bundler, Bun copies the file into `outdir` and adds a hash of the file's contents to the name of the copy. The import resolves to a relative path pointing to the copied file.

```js
// Output
var logo = "./logo.svg";
var logo = "./logo-a7305bdef.svg";
console.log(logo);
```

If `publicPath` is set, the import uses its value as a prefix to construct an absolute path/URL.

| Public path | Resolved import |
| ---------------------------- | ---------------------------------- |
| `""` (default) | `./logo.svg` |
| `"/assets/"` | `/assets/logo.svg` |
| `"https://cdn.example.com/"` | `https://cdn.example.com/logo.svg` |
| Public path | Resolved import |
| ---------------------------- | -------------------------------------------- |
| `""` (default) | `./logo-a7305bdef.svg` |
| `"/assets/"` | `/assets/logo-a7305bdef.svg` |
| `"https://cdn.example.com/"` | `https://cdn.example.com/logo-a7305bdef.svg` |

<Note>The value of `naming.asset` determines the location and file name of the copied file.</Note>
<Note>
Comment thread
robobun marked this conversation as resolved.
Resolved
The value of `naming.asset` determines the location and file name of the copied file. The default is
`[name]-[hash].[ext]`. Set it to `[name].[ext]` to keep the original file name.
Comment thread
robobun marked this conversation as resolved.
Resolved
</Note>
19 changes: 14 additions & 5 deletions docs/runtime/child-process.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ When passing an existing `Terminal` object:

## Blocking API (`Bun.spawnSync()`)

`Bun.spawnSync` is the blocking equivalent of `Bun.spawn`. It supports the same inputs and parameters and returns a `SyncSubprocess` object, which differs from `Subprocess` in a few ways.
`Bun.spawnSync` is the blocking equivalent of `Bun.spawn`. It takes the same command and most of the same options, and returns a `SyncSubprocess` object, which differs from `Subprocess` in a few ways.

1. The returned object contains a `success` property that indicates whether the process exited with a zero exit code.
2. The `stdout` and `stderr` properties are instances of `Buffer` instead of `ReadableStream`.
Expand All @@ -467,6 +467,15 @@ console.log(proc.stdout.toString());
// => "hello\n"
```

`Bun.spawnSync` blocks until the process exits. The options that depend on callbacks or streams while the process runs work only with `Bun.spawn`:

| Option | With `Bun.spawnSync` |
| ----------------------------- | ------------------------------------------------------------------------------ |
| `onExit` | Bun never calls it. Read `exitCode` and `signalCode` from the returned object. |
| `ipc` | Bun never calls it, and the subprocess gets no IPC channel. |
| A `ReadableStream` as `stdin` | `Bun.spawnSync` throws a `TypeError`. |
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
| `terminal` | `Bun.spawnSync` throws a `TypeError`. |

As a rule of thumb, the asynchronous `Bun.spawn` API is better for HTTP servers and apps, and `Bun.spawnSync` is better for building command-line tools.

---
Expand Down Expand Up @@ -533,8 +542,8 @@ namespace SpawnOptions {
exitCode: number | null,
signalCode: NodeJS.Signals | number | null,
error?: ErrorLike,
): void | Promise<void>;
ipc?(message: any, subprocess: Subprocess): void;
): void | Promise<void>; // Bun.spawn only (spawnSync never calls it)
ipc?(message: any, subprocess: Subprocess): void; // Bun.spawn only (spawnSync never calls it)
serialization?: "json" | "advanced";
windowsHide?: boolean;
windowsVerbatimArguments?: boolean;
Expand All @@ -554,7 +563,7 @@ namespace SpawnOptions {
| null // equivalent to "ignore"
| undefined // to use default
| BunFile
| ArrayBufferView
| ArrayBufferView // not implemented: throws a TypeError unless the view is empty
| number;

type Writable =
Expand All @@ -566,7 +575,7 @@ namespace SpawnOptions {
| BunFile
| ArrayBufferView
| number
| ReadableStream
| ReadableStream // Bun.spawn only (spawnSync throws)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
| Blob
| Response
| Request;
Expand Down
Loading
Loading