bun-types 1.3.3-canary.20251114T140703 → 1.3.3-canary.20251116T140533
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.
- package/bun.d.ts +1 -1
- package/docs/bundler/executables.mdx +37 -3
- package/docs/bundler/index.mdx +1 -1
- package/docs/guides/ecosystem/nextjs.mdx +81 -35
- package/docs/guides/ecosystem/tanstack-start.mdx +726 -88
- package/docs/guides/html-rewriter/extract-links.mdx +5 -6
- package/docs/guides/http/file-uploads.mdx +2 -2
- package/docs/guides/install/add-peer.mdx +2 -2
- package/docs/guides/read-file/watch.mdx +2 -2
- package/docs/guides/runtime/cicd.mdx +6 -6
- package/docs/guides/runtime/define-constant.mdx +4 -4
- package/docs/guides/test/concurrent-test-glob.mdx +143 -0
- package/docs/guides/test/spy-on.mdx +2 -2
- package/docs/guides/websocket/context.mdx +8 -3
- package/docs/guides/websocket/pubsub.mdx +4 -1
- package/docs/guides/websocket/simple.mdx +4 -1
- package/docs/pm/cli/info.mdx +70 -0
- package/docs/pm/cli/install.mdx +123 -4
- package/docs/pm/cli/link.mdx +13 -0
- package/docs/pm/cli/publish.mdx +8 -0
- package/docs/pm/overrides.mdx +6 -6
- package/docs/pm/workspaces.mdx +13 -5
- package/docs/project/contributing.mdx +60 -15
- package/docs/quickstart.mdx +8 -3
- package/docs/runtime/bunfig.mdx +53 -1
- package/docs/runtime/html-rewriter.mdx +13 -20
- package/docs/runtime/http/routing.mdx +1 -1
- package/docs/runtime/http/websockets.mdx +8 -2
- package/docs/runtime/module-resolution.mdx +23 -0
- package/docs/runtime/redis.mdx +1 -0
- package/docs/runtime/utils.mdx +2 -2
- package/docs/test/configuration.mdx +47 -0
- package/docs/test/reporters.mdx +9 -0
- package/package.json +1 -1
|
@@ -37,6 +37,7 @@ await extractLinks("https://bun.com");
|
|
|
37
37
|
|
|
38
38
|
When scraping websites, you often want to convert relative URLs (like `/docs`) to absolute URLs. Here's how to handle URL resolution:
|
|
39
39
|
|
|
40
|
+
{/* prettier-ignore */}
|
|
40
41
|
```ts extract-links.ts icon="/icons/typescript.svg"
|
|
41
42
|
async function extractLinksFromURL(url: string) {
|
|
42
43
|
const response = await fetch(url);
|
|
@@ -47,13 +48,11 @@ async function extractLinksFromURL(url: string) {
|
|
|
47
48
|
const href = el.getAttribute("href");
|
|
48
49
|
if (href) {
|
|
49
50
|
// Convert relative URLs to absolute // [!code ++]
|
|
50
|
-
try {
|
|
51
|
-
// [!code ++]
|
|
51
|
+
try { // [!code ++]
|
|
52
52
|
const absoluteURL = new URL(href, url).href; // [!code ++]
|
|
53
|
-
links.add(absoluteURL);
|
|
54
|
-
} catch {
|
|
55
|
-
// [!code ++]
|
|
56
|
-
links.add(href);
|
|
53
|
+
links.add(absoluteURL);
|
|
54
|
+
} catch { // [!code ++]
|
|
55
|
+
links.add(href); // [!code ++]
|
|
57
56
|
} // [!code ++]
|
|
58
57
|
}
|
|
59
58
|
},
|
|
@@ -65,6 +65,7 @@ First we use the [`.formData()`](https://developer.mozilla.org/en-US/docs/Web/AP
|
|
|
65
65
|
|
|
66
66
|
Finally, we write the `Blob` to disk using [`Bun.write()`](https://bun.com/docs/api/file-io#writing-files-bun-write).
|
|
67
67
|
|
|
68
|
+
{/* prettier-ignore */}
|
|
68
69
|
```ts index.ts icon="/icons/typescript.svg"
|
|
69
70
|
const server = Bun.serve({
|
|
70
71
|
port: 4000,
|
|
@@ -80,8 +81,7 @@ const server = Bun.serve({
|
|
|
80
81
|
});
|
|
81
82
|
|
|
82
83
|
// parse formdata at /action // [!code ++]
|
|
83
|
-
if (url.pathname === "/action") {
|
|
84
|
-
// [!code ++]
|
|
84
|
+
if (url.pathname === "/action") { // [!code ++]
|
|
85
85
|
const formdata = await req.formData(); // [!code ++]
|
|
86
86
|
const name = formdata.get("name"); // [!code ++]
|
|
87
87
|
const profilePicture = formdata.get("profilePicture"); // [!code ++]
|
|
@@ -26,14 +26,14 @@ This will add the package to `peerDependencies` in `package.json`.
|
|
|
26
26
|
|
|
27
27
|
Running `bun install` will install peer dependencies by default, unless marked optional in `peerDependenciesMeta`.
|
|
28
28
|
|
|
29
|
+
{/* prettier-ignore */}
|
|
29
30
|
```json package.json icon="file-json"
|
|
30
31
|
{
|
|
31
32
|
"peerDependencies": {
|
|
32
33
|
"@types/bun": "^1.3.2"
|
|
33
34
|
},
|
|
34
35
|
"peerDependenciesMeta": {
|
|
35
|
-
"@types/bun": {
|
|
36
|
-
// [!code ++]
|
|
36
|
+
"@types/bun": { // [!code ++]
|
|
37
37
|
"optional": true // [!code ++]
|
|
38
38
|
} // [!code ++]
|
|
39
39
|
}
|
|
@@ -23,8 +23,8 @@ To listen to changes in subdirectories, pass the `recursive: true` option to `fs
|
|
|
23
23
|
```ts
|
|
24
24
|
import { watch } from "fs";
|
|
25
25
|
|
|
26
|
-
const watcher = watch(import.meta.dir, { recursive: true }, (event,
|
|
27
|
-
console.log(`Detected ${event} in ${
|
|
26
|
+
const watcher = watch(import.meta.dir, { recursive: true }, (event, relativePath) => {
|
|
27
|
+
console.log(`Detected ${event} in ${relativePath}`);
|
|
28
28
|
});
|
|
29
29
|
```
|
|
30
30
|
|
|
@@ -15,12 +15,12 @@ jobs:
|
|
|
15
15
|
steps:
|
|
16
16
|
# ...
|
|
17
17
|
- uses: actions/checkout@v4
|
|
18
|
-
- uses: oven-sh/setup-bun@v2
|
|
18
|
+
- uses: oven-sh/setup-bun@v2 # [!code ++]
|
|
19
19
|
|
|
20
20
|
# run any `bun` or `bunx` command
|
|
21
|
-
- run: bun install
|
|
22
|
-
- run: bun index.ts
|
|
23
|
-
- run: bun run build
|
|
21
|
+
- run: bun install # [!code ++]
|
|
22
|
+
- run: bun index.ts # [!code ++]
|
|
23
|
+
- run: bun run build # [!code ++]
|
|
24
24
|
```
|
|
25
25
|
|
|
26
26
|
---
|
|
@@ -36,8 +36,8 @@ jobs:
|
|
|
36
36
|
steps:
|
|
37
37
|
# ...
|
|
38
38
|
- uses: oven-sh/setup-bun@v2
|
|
39
|
-
with:
|
|
40
|
-
bun-version: 1.2.0 # or "latest", "canary", <sha>
|
|
39
|
+
with: # [!code ++]
|
|
40
|
+
bun-version: 1.2.0 # or "latest", "canary", <sha> # [!code ++]
|
|
41
41
|
```
|
|
42
42
|
|
|
43
43
|
---
|
|
@@ -27,9 +27,9 @@ if (process.env.NODE_ENV === "production") {
|
|
|
27
27
|
|
|
28
28
|
Before the code reaches the JavaScript engine, Bun replaces `process.env.NODE_ENV` with `"production"`.
|
|
29
29
|
|
|
30
|
+
{/* prettier-ignore */}
|
|
30
31
|
```ts
|
|
31
|
-
if ("production" === "production") {
|
|
32
|
-
// [!code ++]
|
|
32
|
+
if ("production" === "production") { // [!code ++]
|
|
33
33
|
console.log("Production mode");
|
|
34
34
|
} else {
|
|
35
35
|
console.log("Development mode");
|
|
@@ -42,9 +42,9 @@ It doesn't stop there. Bun's optimizing transpiler is smart enough to do some ba
|
|
|
42
42
|
|
|
43
43
|
Since `"production" === "production"` is always `true`, Bun replaces the entire expression with the `true` value.
|
|
44
44
|
|
|
45
|
+
{/* prettier-ignore */}
|
|
45
46
|
```ts
|
|
46
|
-
if (true) {
|
|
47
|
-
// [!code ++]
|
|
47
|
+
if (true) { // [!code ++]
|
|
48
48
|
console.log("Production mode");
|
|
49
49
|
} else {
|
|
50
50
|
console.log("Development mode");
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Selectively run tests concurrently with glob patterns
|
|
3
|
+
sidebarTitle: Concurrent test glob
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
This guide demonstrates how to use the `concurrentTestGlob` option to selectively run tests concurrently based on file naming patterns.
|
|
8
|
+
|
|
9
|
+
## Project Structure
|
|
10
|
+
|
|
11
|
+
```sh title="Project Structure" icon="folder-tree"
|
|
12
|
+
my-project/
|
|
13
|
+
├── bunfig.toml
|
|
14
|
+
├── tests/
|
|
15
|
+
│ ├── unit/
|
|
16
|
+
│ │ ├── math.test.ts # Sequential
|
|
17
|
+
│ │ └── utils.test.ts # Sequential
|
|
18
|
+
│ └── integration/
|
|
19
|
+
│ ├── concurrent-api.test.ts # Concurrent
|
|
20
|
+
│ └── concurrent-database.test.ts # Concurrent
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Configuration
|
|
24
|
+
|
|
25
|
+
Configure your `bunfig.toml` to run test files with "concurrent-" prefix concurrently:
|
|
26
|
+
|
|
27
|
+
```toml title="bunfig.toml" icon="settings"
|
|
28
|
+
[test]
|
|
29
|
+
# Run all test files with "concurrent-" prefix concurrently
|
|
30
|
+
concurrentTestGlob = "**/concurrent-*.test.ts"
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Test Files
|
|
34
|
+
|
|
35
|
+
### Unit Test (Sequential)
|
|
36
|
+
|
|
37
|
+
Sequential tests are good for tests that share state or have specific ordering requirements:
|
|
38
|
+
|
|
39
|
+
```ts title="tests/unit/math.test.ts" icon="/icons/typescript.svg"
|
|
40
|
+
import { test, expect } from "bun:test";
|
|
41
|
+
|
|
42
|
+
// These tests run sequentially by default
|
|
43
|
+
let sharedState = 0;
|
|
44
|
+
|
|
45
|
+
test("addition", () => {
|
|
46
|
+
sharedState = 5 + 3;
|
|
47
|
+
expect(sharedState).toBe(8);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("uses previous state", () => {
|
|
51
|
+
// This test depends on the previous test's state
|
|
52
|
+
expect(sharedState).toBe(8);
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Integration Test (Concurrent)
|
|
57
|
+
|
|
58
|
+
Tests in files matching the glob pattern automatically run concurrently:
|
|
59
|
+
|
|
60
|
+
```ts title="tests/integration/concurrent-api.test.ts" icon="/icons/typescript.svg"
|
|
61
|
+
import { test, expect } from "bun:test";
|
|
62
|
+
|
|
63
|
+
// These tests automatically run concurrently due to filename matching the glob pattern.
|
|
64
|
+
// Using test() is equivalent to test.concurrent() when the file matches concurrentTestGlob.
|
|
65
|
+
// Each test is independent and can run in parallel.
|
|
66
|
+
|
|
67
|
+
test("fetch user data", async () => {
|
|
68
|
+
const response = await fetch("/api/user/1");
|
|
69
|
+
expect(response.ok).toBe(true);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("fetch posts", async () => {
|
|
73
|
+
const response = await fetch("/api/posts");
|
|
74
|
+
expect(response.ok).toBe(true);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("fetch comments", async () => {
|
|
78
|
+
const response = await fetch("/api/comments");
|
|
79
|
+
expect(response.ok).toBe(true);
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Running Tests
|
|
84
|
+
|
|
85
|
+
```bash terminal icon="terminal"
|
|
86
|
+
# Run all tests - concurrent-*.test.ts files will run concurrently
|
|
87
|
+
bun test
|
|
88
|
+
|
|
89
|
+
# Override: Force ALL tests to run concurrently
|
|
90
|
+
# Note: This overrides bunfig.toml and runs all tests concurrently, regardless of glob
|
|
91
|
+
bun test --concurrent
|
|
92
|
+
|
|
93
|
+
# Run only unit tests (sequential)
|
|
94
|
+
bun test tests/unit
|
|
95
|
+
|
|
96
|
+
# Run only integration tests (concurrent due to glob pattern)
|
|
97
|
+
bun test tests/integration
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Benefits
|
|
101
|
+
|
|
102
|
+
1. **Gradual Migration**: Migrate to concurrent tests file by file by renaming them
|
|
103
|
+
2. **Clear Organization**: File naming convention indicates execution mode
|
|
104
|
+
3. **Performance**: Integration tests run faster in parallel
|
|
105
|
+
4. **Safety**: Unit tests remain sequential where needed
|
|
106
|
+
5. **Flexibility**: Easy to change execution mode by renaming files
|
|
107
|
+
|
|
108
|
+
## Migration Strategy
|
|
109
|
+
|
|
110
|
+
To migrate existing tests to concurrent execution:
|
|
111
|
+
|
|
112
|
+
1. **Start with independent integration tests** - These typically don't share state
|
|
113
|
+
2. **Rename files to match the glob pattern**: `mv api.test.ts concurrent-api.test.ts`
|
|
114
|
+
3. **Verify tests still pass** - Run `bun test` to ensure no race conditions
|
|
115
|
+
4. **Monitor for shared state issues** - Watch for flaky tests or unexpected failures
|
|
116
|
+
5. **Continue migrating stable tests incrementally** - Don't rush the migration
|
|
117
|
+
|
|
118
|
+
## Tips
|
|
119
|
+
|
|
120
|
+
- **Use descriptive prefixes**: `concurrent-`, `parallel-`, `async-`
|
|
121
|
+
- **Keep related sequential tests together** in the same directory
|
|
122
|
+
- **Document why certain tests must remain sequential** with comments
|
|
123
|
+
- **Use `test.concurrent()` for fine-grained control** in sequential files
|
|
124
|
+
(Note: In files matched by `concurrentTestGlob`, plain `test()` already runs concurrently)
|
|
125
|
+
|
|
126
|
+
## Multiple Patterns
|
|
127
|
+
|
|
128
|
+
You can specify multiple patterns for different test categories:
|
|
129
|
+
|
|
130
|
+
```toml title="bunfig.toml" icon="settings"
|
|
131
|
+
[test]
|
|
132
|
+
concurrentTestGlob = [
|
|
133
|
+
"**/integration/*.test.ts",
|
|
134
|
+
"**/e2e/*.test.ts",
|
|
135
|
+
"**/concurrent-*.test.ts"
|
|
136
|
+
]
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
This configuration will run tests concurrently if they match any of these patterns:
|
|
140
|
+
|
|
141
|
+
- All tests in `integration/` directories
|
|
142
|
+
- All tests in `e2e/` directories
|
|
143
|
+
- All tests with `concurrent-` prefix anywhere in the project
|
|
@@ -23,6 +23,7 @@ const spy = spyOn(leo, "sayHi");
|
|
|
23
23
|
|
|
24
24
|
Once the spy is created, it can be used to write `expect` assertions relating to method calls.
|
|
25
25
|
|
|
26
|
+
{/* prettier-ignore */}
|
|
26
27
|
```ts
|
|
27
28
|
import { test, expect, spyOn } from "bun:test";
|
|
28
29
|
|
|
@@ -35,8 +36,7 @@ const leo = {
|
|
|
35
36
|
|
|
36
37
|
const spy = spyOn(leo, "sayHi");
|
|
37
38
|
|
|
38
|
-
test("turtles", () => {
|
|
39
|
-
// [!code ++]
|
|
39
|
+
test("turtles", () => { // [!code ++]
|
|
40
40
|
expect(spy).toHaveBeenCalledTimes(0); // [!code ++]
|
|
41
41
|
leo.sayHi("pizza"); // [!code ++]
|
|
42
42
|
expect(spy).toHaveBeenCalledTimes(1); // [!code ++]
|
|
@@ -9,7 +9,7 @@ When building a WebSocket server, it's typically necessary to store some identif
|
|
|
9
9
|
With [Bun.serve()](https://bun.com/docs/api/websockets contextual-data), this "contextual data" is set when the connection is initially upgraded by passing a `data` parameter in the `server.upgrade()` call.
|
|
10
10
|
|
|
11
11
|
```ts server.ts icon="/icons/typescript.svg"
|
|
12
|
-
Bun.serve
|
|
12
|
+
Bun.serve({
|
|
13
13
|
fetch(req, server) {
|
|
14
14
|
const success = server.upgrade(req, {
|
|
15
15
|
data: {
|
|
@@ -22,6 +22,9 @@ Bun.serve<{ socketId: number }>({
|
|
|
22
22
|
// ...
|
|
23
23
|
},
|
|
24
24
|
websocket: {
|
|
25
|
+
// TypeScript: specify the type of ws.data like this
|
|
26
|
+
data: {} as { socketId: number },
|
|
27
|
+
|
|
25
28
|
// define websocket handlers
|
|
26
29
|
async message(ws, message) {
|
|
27
30
|
// the contextual data is available as the `data` property
|
|
@@ -43,8 +46,7 @@ type WebSocketData = {
|
|
|
43
46
|
userId: string;
|
|
44
47
|
};
|
|
45
48
|
|
|
46
|
-
|
|
47
|
-
Bun.serve<WebSocketData>({
|
|
49
|
+
Bun.serve({
|
|
48
50
|
async fetch(req, server) {
|
|
49
51
|
// use a library to parse cookies
|
|
50
52
|
const cookies = parseCookies(req.headers.get("Cookie"));
|
|
@@ -62,6 +64,9 @@ Bun.serve<WebSocketData>({
|
|
|
62
64
|
if (upgraded) return undefined;
|
|
63
65
|
},
|
|
64
66
|
websocket: {
|
|
67
|
+
// TypeScript: specify the type of ws.data like this
|
|
68
|
+
data: {} as WebSocketData,
|
|
69
|
+
|
|
65
70
|
async message(ws, message) {
|
|
66
71
|
// save the message to a database
|
|
67
72
|
await saveMessageToDatabase({
|
|
@@ -9,7 +9,7 @@ Bun's server-side `WebSocket` API provides a native pub-sub API. Sockets can be
|
|
|
9
9
|
This code snippet implements a simple single-channel chat server.
|
|
10
10
|
|
|
11
11
|
```ts server.ts icon="/icons/typescript.svg"
|
|
12
|
-
const server = Bun.serve
|
|
12
|
+
const server = Bun.serve({
|
|
13
13
|
fetch(req, server) {
|
|
14
14
|
const cookies = req.headers.get("cookie");
|
|
15
15
|
const username = getUsernameFromCookies(cookies);
|
|
@@ -19,6 +19,9 @@ const server = Bun.serve<{ username: string }>({
|
|
|
19
19
|
return new Response("Hello world");
|
|
20
20
|
},
|
|
21
21
|
websocket: {
|
|
22
|
+
// TypeScript: specify the type of ws.data like this
|
|
23
|
+
data: {} as { username: string },
|
|
24
|
+
|
|
22
25
|
open(ws) {
|
|
23
26
|
const msg = `${ws.data.username} has entered the chat`;
|
|
24
27
|
ws.subscribe("the-group-chat");
|
|
@@ -9,7 +9,7 @@ Start a simple WebSocket server using [`Bun.serve`](https://bun.com/docs/api/htt
|
|
|
9
9
|
Inside `fetch`, we attempt to upgrade incoming `ws:` or `wss:` requests to WebSocket connections.
|
|
10
10
|
|
|
11
11
|
```ts server.ts icon="/icons/typescript.svg"
|
|
12
|
-
const server = Bun.serve
|
|
12
|
+
const server = Bun.serve({
|
|
13
13
|
fetch(req, server) {
|
|
14
14
|
const success = server.upgrade(req);
|
|
15
15
|
if (success) {
|
|
@@ -22,6 +22,9 @@ const server = Bun.serve<{ authToken: string }>({
|
|
|
22
22
|
return new Response("Hello world!");
|
|
23
23
|
},
|
|
24
24
|
websocket: {
|
|
25
|
+
// TypeScript: specify the type of ws.data like this
|
|
26
|
+
data: {} as { authToken: string },
|
|
27
|
+
|
|
25
28
|
// this is called when a message is received
|
|
26
29
|
async message(ws, message) {
|
|
27
30
|
console.log(`Received ${message}`);
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "bun info"
|
|
3
|
+
description: "Display package metadata from the npm registry"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
`bun info` displays package metadata from the npm registry.
|
|
7
|
+
|
|
8
|
+
## Usage
|
|
9
|
+
|
|
10
|
+
```bash terminal icon="terminal"
|
|
11
|
+
bun info react
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
This will display information about the `react` package, including its latest version, description, homepage, dependencies, and more.
|
|
15
|
+
|
|
16
|
+
## Viewing specific versions
|
|
17
|
+
|
|
18
|
+
To view information about a specific version:
|
|
19
|
+
|
|
20
|
+
```bash terminal icon="terminal"
|
|
21
|
+
bun info react@18.0.0
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Viewing specific properties
|
|
25
|
+
|
|
26
|
+
You can also query specific properties from the package metadata:
|
|
27
|
+
|
|
28
|
+
```bash terminal icon="terminal"
|
|
29
|
+
bun info react version
|
|
30
|
+
bun info react dependencies
|
|
31
|
+
bun info react repository.url
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## JSON output
|
|
35
|
+
|
|
36
|
+
To get the output in JSON format, use the `--json` flag:
|
|
37
|
+
|
|
38
|
+
```bash terminal icon="terminal"
|
|
39
|
+
bun info react --json
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Alias
|
|
43
|
+
|
|
44
|
+
`bun pm view` is an alias for `bun info`:
|
|
45
|
+
|
|
46
|
+
```bash terminal icon="terminal"
|
|
47
|
+
bun pm view react # equivalent to: bun info react
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Examples
|
|
51
|
+
|
|
52
|
+
```bash terminal icon="terminal"
|
|
53
|
+
# View basic package information
|
|
54
|
+
bun info is-number
|
|
55
|
+
|
|
56
|
+
# View a specific version
|
|
57
|
+
bun info is-number@7.0.0
|
|
58
|
+
|
|
59
|
+
# View all available versions
|
|
60
|
+
bun info is-number versions
|
|
61
|
+
|
|
62
|
+
# View package dependencies
|
|
63
|
+
bun info express dependencies
|
|
64
|
+
|
|
65
|
+
# View package homepage
|
|
66
|
+
bun info lodash homepage
|
|
67
|
+
|
|
68
|
+
# Get JSON output
|
|
69
|
+
bun info react --json
|
|
70
|
+
```
|
package/docs/pm/cli/install.mdx
CHANGED
|
@@ -134,14 +134,14 @@ For more information on filtering with `bun install`, refer to [Package Manager
|
|
|
134
134
|
|
|
135
135
|
Bun supports npm's `"overrides"` and Yarn's `"resolutions"` in `package.json`. These are mechanisms for specifying a version range for _metadependencies_—the dependencies of your dependencies. Refer to [Package manager > Overrides and resolutions](/pm/overrides) for complete documentation.
|
|
136
136
|
|
|
137
|
+
{/* prettier-ignore */}
|
|
137
138
|
```json package.json file="file-json"
|
|
138
139
|
{
|
|
139
140
|
"name": "my-app",
|
|
140
141
|
"dependencies": {
|
|
141
142
|
"foo": "^2.0.0"
|
|
142
143
|
},
|
|
143
|
-
"overrides": {
|
|
144
|
-
// [!code ++]
|
|
144
|
+
"overrides": { // [!code ++]
|
|
145
145
|
"bar": "~4.4.0" // [!code ++]
|
|
146
146
|
} // [!code ++]
|
|
147
147
|
}
|
|
@@ -304,7 +304,16 @@ For more advanced security scanning, including integration with services & custo
|
|
|
304
304
|
|
|
305
305
|
## Configuration
|
|
306
306
|
|
|
307
|
-
|
|
307
|
+
### Configuring `bun install` with `bunfig.toml`
|
|
308
|
+
|
|
309
|
+
`bunfig.toml` is searched for in the following paths on `bun install`, `bun remove`, and `bun add`:
|
|
310
|
+
|
|
311
|
+
1. `$XDG_CONFIG_HOME/.bunfig.toml` or `$HOME/.bunfig.toml`
|
|
312
|
+
2. `./bunfig.toml`
|
|
313
|
+
|
|
314
|
+
If both are found, the results are merged together.
|
|
315
|
+
|
|
316
|
+
Configuring with `bunfig.toml` is optional. Bun tries to be zero configuration in general, but that's not always possible. The default behavior of `bun install` can be configured in `bunfig.toml`. The default values are shown below.
|
|
308
317
|
|
|
309
318
|
```toml bunfig.toml icon="settings"
|
|
310
319
|
[install]
|
|
@@ -345,7 +354,29 @@ minimumReleaseAge = 259200 # seconds
|
|
|
345
354
|
minimumReleaseAgeExcludes = ["@types/node", "typescript"]
|
|
346
355
|
```
|
|
347
356
|
|
|
348
|
-
|
|
357
|
+
### Configuring with environment variables
|
|
358
|
+
|
|
359
|
+
Environment variables have a higher priority than `bunfig.toml`.
|
|
360
|
+
|
|
361
|
+
| Name | Description |
|
|
362
|
+
| ---------------------------------- | ------------------------------------------------------------- |
|
|
363
|
+
| `BUN_CONFIG_REGISTRY` | Set an npm registry (default: https://registry.npmjs.org) |
|
|
364
|
+
| `BUN_CONFIG_TOKEN` | Set an auth token (currently does nothing) |
|
|
365
|
+
| `BUN_CONFIG_YARN_LOCKFILE` | Save a Yarn v1-style yarn.lock |
|
|
366
|
+
| `BUN_CONFIG_LINK_NATIVE_BINS` | Point `bin` in package.json to a platform-specific dependency |
|
|
367
|
+
| `BUN_CONFIG_SKIP_SAVE_LOCKFILE` | Don’t save a lockfile |
|
|
368
|
+
| `BUN_CONFIG_SKIP_LOAD_LOCKFILE` | Don’t load a lockfile |
|
|
369
|
+
| `BUN_CONFIG_SKIP_INSTALL_PACKAGES` | Don’t install any packages |
|
|
370
|
+
|
|
371
|
+
Bun always tries to use the fastest available installation method for the target platform. On macOS, that’s `clonefile` and on Linux, that’s `hardlink`. You can change which installation method is used with the `--backend` flag. When unavailable or on error, `clonefile` and `hardlink` fallsback to a platform-specific implementation of copying files.
|
|
372
|
+
|
|
373
|
+
Bun stores installed packages from npm in `~/.bun/install/cache/${name}@${version}`. Note that if the semver version has a `build` or a `pre` tag, it is replaced with a hash of that value instead. This is to reduce the chances of errors from long file paths, but unfortunately complicates figuring out where a package was installed on disk.
|
|
374
|
+
|
|
375
|
+
When the `node_modules` folder exists, before installing, Bun checks if the `"name"` and `"version"` in `package/package.json` in the expected node_modules folder matches the expected `name` and `version`. This is how it determines whether it should install. It uses a custom JSON parser which stops parsing as soon as it finds `"name"` and `"version"`.
|
|
376
|
+
|
|
377
|
+
When a `bun.lock` doesn’t exist or `package.json` has changed dependencies, tarballs are downloaded & extracted eagerly while resolving.
|
|
378
|
+
|
|
379
|
+
When a `bun.lock` exists and `package.json` hasn’t changed, Bun downloads missing dependencies lazily. If the package with a matching `name` & `version` already exists in the expected location within `node_modules`, Bun won’t attempt to download the tarball.
|
|
349
380
|
|
|
350
381
|
## CI/CD
|
|
351
382
|
|
|
@@ -395,6 +426,94 @@ jobs:
|
|
|
395
426
|
run: bun run build
|
|
396
427
|
```
|
|
397
428
|
|
|
429
|
+
## Platform-specific dependencies?
|
|
430
|
+
|
|
431
|
+
bun stores normalized `cpu` and `os` values from npm in the lockfile, along with the resolved packages. It skips downloading, extracting, and installing packages disabled for the current target at runtime. This means the lockfile won't change between platforms/architectures even if the packages ultimately installed do change.
|
|
432
|
+
|
|
433
|
+
### `--cpu` and `--os` flags
|
|
434
|
+
|
|
435
|
+
You can override the target platform for package selection:
|
|
436
|
+
|
|
437
|
+
```bash
|
|
438
|
+
bun install --cpu=x64 --os=linux
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
This installs packages for the specified platform instead of the current system. Useful for cross-platform builds or when preparing deployments for different environments.
|
|
442
|
+
|
|
443
|
+
**Accepted values for `--cpu`**: `arm64`, `x64`, `ia32`, `ppc64`, `s390x`
|
|
444
|
+
|
|
445
|
+
**Accepted values for `--os`**: `linux`, `darwin`, `win32`, `freebsd`, `openbsd`, `sunos`, `aix`
|
|
446
|
+
|
|
447
|
+
## Peer dependencies?
|
|
448
|
+
|
|
449
|
+
Peer dependencies are handled similarly to yarn. `bun install` will automatically install peer dependencies. If the dependency is marked optional in `peerDependenciesMeta`, an existing dependency will be chosen if possible.
|
|
450
|
+
|
|
451
|
+
## Lockfile
|
|
452
|
+
|
|
453
|
+
`bun.lock` is Bun’s lockfile format. See [our blogpost about the text lockfile](https://bun.com/blog/bun-lock-text-lockfile).
|
|
454
|
+
|
|
455
|
+
Prior to Bun 1.2, the lockfile was binary and called `bun.lockb`. Old lockfiles can be upgraded to the new format by running `bun install --save-text-lockfile --frozen-lockfile --lockfile-only`, and then deleting `bun.lockb`.
|
|
456
|
+
|
|
457
|
+
## Cache
|
|
458
|
+
|
|
459
|
+
To delete the cache:
|
|
460
|
+
|
|
461
|
+
```bash
|
|
462
|
+
bun pm cache rm
|
|
463
|
+
# or
|
|
464
|
+
rm -rf ~/.bun/install/cache
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
## Platform-specific backends
|
|
468
|
+
|
|
469
|
+
`bun install` uses different system calls to install dependencies depending on the platform. This is a performance optimization. You can force a specific backend with the `--backend` flag.
|
|
470
|
+
|
|
471
|
+
**`hardlink`** is the default backend on Linux. Benchmarking showed it to be the fastest on Linux.
|
|
472
|
+
|
|
473
|
+
```bash
|
|
474
|
+
rm -rf node_modules
|
|
475
|
+
bun install --backend hardlink
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
**`clonefile`** is the default backend on macOS. Benchmarking showed it to be the fastest on macOS. It is only available on macOS.
|
|
479
|
+
|
|
480
|
+
```bash
|
|
481
|
+
rm -rf node_modules
|
|
482
|
+
bun install --backend clonefile
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
**`clonefile_each_dir`** is similar to `clonefile`, except it clones each file individually per directory. It is only available on macOS and tends to perform slower than `clonefile`. Unlike `clonefile`, this does not recursively clone subdirectories in one system call.
|
|
486
|
+
|
|
487
|
+
```bash
|
|
488
|
+
rm -rf node_modules
|
|
489
|
+
bun install --backend clonefile_each_dir
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
**`copyfile`** is the fallback used when any of the above fail, and is the slowest. on macOS, it uses `fcopyfile()` and on linux it uses `copy_file_range()`.
|
|
493
|
+
|
|
494
|
+
```bash
|
|
495
|
+
rm -rf node_modules
|
|
496
|
+
bun install --backend copyfile
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
**`symlink`** is typically only used for `file:` dependencies (and eventually `link:`) internally. To prevent infinite loops, it skips symlinking the `node_modules` folder.
|
|
500
|
+
|
|
501
|
+
If you install with `--backend=symlink`, Node.js won't resolve node_modules of dependencies unless each dependency has its own node_modules folder or you pass `--preserve-symlinks` to `node` or `bun`. See [Node.js documentation on `--preserve-symlinks`](https://nodejs.org/api/cli.html#--preserve-symlinks).
|
|
502
|
+
|
|
503
|
+
```bash
|
|
504
|
+
rm -rf node_modules
|
|
505
|
+
bun install --backend symlink
|
|
506
|
+
bun --preserve-symlinks ./my-file.js
|
|
507
|
+
node --preserve-symlinks ./my-file.js # https://nodejs.org/api/cli.html#--preserve-symlinks
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
## npm registry metadata
|
|
511
|
+
|
|
512
|
+
Bun uses a binary format for caching NPM registry responses. This loads much faster than JSON and tends to be smaller on disk.
|
|
513
|
+
You will see these files in `~/.bun/install/cache/*.npm`. The filename pattern is `${hash(packageName)}.npm`. It’s a hash so that extra directories don’t need to be created for scoped packages.
|
|
514
|
+
|
|
515
|
+
Bun's usage of `Cache-Control` ignores `Age`. This improves performance, but means bun may be about 5 minutes out of date to receive the latest package version metadata from npm.
|
|
516
|
+
|
|
398
517
|
## pnpm migration
|
|
399
518
|
|
|
400
519
|
Bun automatically migrates projects from pnpm to bun. When a `pnpm-lock.yaml` file is detected and no `bun.lock` file exists, Bun will automatically migrate the lockfile to `bun.lock` during installation. The original `pnpm-lock.yaml` file remains unmodified.
|
package/docs/pm/cli/link.mdx
CHANGED
|
@@ -43,6 +43,19 @@ In addition, the `--save` flag can be used to add `cool-pkg` to the `dependencie
|
|
|
43
43
|
}
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
+
## Unlinking
|
|
47
|
+
|
|
48
|
+
Use `bun unlink` in the root directory to unregister a local package.
|
|
49
|
+
|
|
50
|
+
```bash terminal icon="terminal"
|
|
51
|
+
cd /path/to/cool-pkg
|
|
52
|
+
bun unlink
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
```txt
|
|
56
|
+
bun unlink v1.x (7416672e)
|
|
57
|
+
```
|
|
58
|
+
|
|
46
59
|
---
|
|
47
60
|
|
|
48
61
|
<Link />
|
package/docs/pm/cli/publish.mdx
CHANGED
|
@@ -89,6 +89,14 @@ The `--dry-run` flag can be used to simulate the publish process without actuall
|
|
|
89
89
|
bun publish --dry-run
|
|
90
90
|
```
|
|
91
91
|
|
|
92
|
+
### `--tolerate-republish`
|
|
93
|
+
|
|
94
|
+
Exit with code 0 instead of 1 if the package version already exists. Useful in CI/CD where jobs may be re-run.
|
|
95
|
+
|
|
96
|
+
```sh terminal icon="terminal"
|
|
97
|
+
bun publish --tolerate-republish
|
|
98
|
+
```
|
|
99
|
+
|
|
92
100
|
### `--gzip-level`
|
|
93
101
|
|
|
94
102
|
Specify the level of gzip compression to use when packing the package. Only applies to `bun publish` without a tarball path argument. Values range from `0` to `9` (default is `9`).
|