Skip to content

Commit

Permalink
Update patch docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed Jun 19, 2024
1 parent 7c27f3f commit 3003dcb
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 42 deletions.
42 changes: 0 additions & 42 deletions docs/cli/patch.md

This file was deleted.

58 changes: 58 additions & 0 deletions docs/install/patch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
`bun patch` is lets you persistently patch node_modules in a maintainable, git-friendly way.

Sometimes, you need to make a small change to a package in `node_modules/` to fix a bug or add a feature. `bun patch` makes it easy to do this without vendoring the entire package and reuse the patch across multiple installs, multiple projects, and multiple machines.

Features:

- Generates `.patch` files that can be shared with your team and re-applied consistently across multiple machines
- Automatically updates your `package.json` and lockfile to include the patched version
- Edit packages in `node_modules/` while preserving the integrity of Bun's [Global Cache](https://bun.sh/docs/install/cache)
- Test your changes locally before committing them with `bun patch --commit <pkg>`
- To preserve disk space and keep `bun install` fast, patched packages are committed to the Global Cache and shared across projects where possible

{% callout %}

#### Step 1. Prepare the package for patching

To get started, use `bun patch <pkg>` to prepare the package for patching:

```bash
# you can supply the package name
$ bun patch react

# ...and a precise version in case multiple versions are installed
$ bun patch [email protected]

# or the path to the package
$ bun patch node_modules/react
```

{% callout %}
**Note** — Don't forget to call `bun patch <pkg>`! This ensures the package folder in `node_modules/` contains a fresh copy of the package with no symlinks/hardlinks to Bun's cache.

If you forget to do this, you might end up editing the package globally in the cache!
{% /callout %}

#### Step 2. Test your changes locally

`bun patch <pkg>` makes it safe to edit the `<pkg>` in `node_modules/` directly, while preserving the integrity of Bun's [Global Cache](https://bun.sh/docs/install/cache). This works by You can test your changes locally before committing them.

#### Step 3. Commit your changes

Once you're happy with your changes, run `bun patch --commit <path or pkg>`.

Bun will generate a patch file in `patches/`, update your `package.json` and lockfile, and Bun will start using the patched package:

```bash
# you can supply the path to the patched package
$ bun patch --commit node_modules/react

# ... or the package name and optionally the version
$ bun patch --commit [email protected]

# choose the directory to store the patch files
$ bun patch --commit react --patches-dir=mypatches

# `patch-commit` is available for compatibility with pnpm
$ bun patch-commit react
```
4 changes: 4 additions & 0 deletions docs/nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ export default {
page("install/overrides", "Overrides and resolutions", {
description: "Specify version ranges for nested dependencies",
}),
page("install/patch", "Patch dependencies", {
description:
"Patch dependencies in your project to fix bugs or add features without vendoring the entire package.",
}),
// page("install/utilities", "Utilities", {
// description: "Use `bun pm` to introspect your global module cache or project dependency tree.",
// }),
Expand Down

0 comments on commit 3003dcb

Please sign in to comment.