Files
bun.sh/docs/install/workspaces.md
Dylan Greene f3fe4508ae docs - Add "workspace:*" to workspace docs. (#5379)
* add workspace:* to docs

* Updates

---------

Co-authored-by: Colin McDonnell <colinmcd94@gmail.com>
2023-09-14 15:04:26 -07:00

2.2 KiB
Raw Blame History

Bun supports workspaces in package.json. Workspaces make it easy to develop complex software as a monorepo consisting of several independent packages.

To try it, specify a list of sub-packages in the workspaces field of your package.json; it's conventional to place these sub-packages in a directory called packages.

{
  "name": "my-project",
  "version": "1.0.0",
  "workspaces": ["packages/*"],
  "devDependencies": {
    "example-package-in-monorepo": "workspace:*"
  }
}

{% callout %} Glob support — Bun supports simple <directory>/* globs in "workspaces". Full glob syntax (e.g. ** and ?) is not yet supported. {% /callout %}

When referencing other packages in the monorepo, use "workspace:*" as the version field in your package.json.

{
  "name": "pkg-a",
  "version": "1.0.0",
  "dependencies": {
    "pkg-b": "workspace:*"
  }
}

{% callout %} Version support — Bun supports simple workspace:* versions in "dependencies". Full version syntax (e.g. workspace:^*) is not yet supported. {% /callout %}

Workspaces have a couple major benefits.

  • Code can be split into logical parts. If one package relies on another, you can simply add it as a dependency with bun add. If package b depends on a, bun install will symlink your local packages/a directory into the node_modules folder of b, instead of trying to download it from the npm registry.
  • Dependencies can be de-duplicated. If a and b share a common dependency, it will be hoisted to the root node_modules directory. This reduces redundant disk usage and minimizes "dependency hell" issues associated with having multiple versions of a package installed simultaneously.

{% callout %} Speed — Installs are fast, even for big monorepos. Bun installs the Remix monorepo in about 500ms on Linux.

  • 28x faster than npm install
  • 12x faster than yarn install (v1)
  • 8x faster than pnpm install

{% image src="https://user-images.githubusercontent.com/709451/212829600-77df9544-7c9f-4d8d-a984-b2cd0fd2aa52.png" /%} {% /callout %}