Workspaces can depend on other workspaces and import/export code between them. This is a native capability of workspaces in Bun.
For example, this might be some workspace's package.json file, where my-workspace-b depends on my-workspace-a:
my-workspace-a/package.json:
{
"name": "my-workspace-a",
"main": "index.ts"
}
my-workspace-b/package.json:
{
"name": "my-workspace-b",
"dependencies": {
"my-workspace-a": "workspace:*"
}
}
Code in my-workspace-a can then import code from my-workspace-b like any other dependency.
my-workspace-a/index.ts:
export const myFunction = () => {
console.log("Hello from my-workspace-a");
}
my-workspace-b/index.ts:
import { myFunction } from "my-workspace-a";
myFunction();
When running scripts via the CLI or API, workspaces can be run in dependency order.
This means that a workspace's script will wait until any workspaces it depends on have completed.
This is similar to how Bun's --filter flag works, though for bun-workspaces,
this behavior is opt-in.
This can be useful in situations where one workspace depends on another workspace's output, such as a build script's output.
bw run my-script --dep-order
# Run all scripts even if a dependency fails
bw run my-script --dep-order --ignore-dep-failure
import { createFileSystemProject } from "bun-workspaces";
const project = createFileSystemProject();
project.runScriptAcrossWorkspaces({
script: "my-script",
dependencyOrder: true,
ignoreDependencyFailure: true,
});