Workspace (ruang kerja)
pnpm memiliki dukungan bawaan untuk monorepositori (repositori multi paket, repositori multi proyek, atau repositori monolitik). Anda dapat membuat workspace untuk menyatukan beberapa proyek di dalam satu repositori.
Sebuah workspace haruslah memilikipnpm-workspace.yaml
di dalam folder akarnya. Sebuah workspace juga mungkin perlu memiliki.npmrc
di dalam folder akarnya.
If you are looking into monorepo management, you might also want to look intoBit. Bit uses pnpm under the hood but automates a lot of the things that are currently done manually in a traditional workspace managed by pnpm/npm/Yarn. There's an article aboutbit install
that talks about it:Painless Monorepo Dependency Management with Bit.
Protokol workspace (workspace:)
Iflink-workspace-packages is set totrue
, pnpm will link packages from the workspace if the available packages match the declared ranges. For instance,foo@1.0.0
is linked intobar
ifbar
has"foo": "^1.0.0"
in its dependencies andfoo@1.0.0
is in the workspace. Namun, jikabar
memiliki"foo": "2.0.0"
dalam dependensi danfoo@2.0.0
tidak ada di workspace, makafoo@2.0.0
akan diinstal dari registri. Perilaku ini memperkenalkan beberapa ketidakpastian.
Untungnya, pnpm mendukung protokolworkspace:
. Ketika protokol ini digunakan, pnpm akan menolak untuk menyelesaikan apa pun selain paket workspace lokal. Jadi, jika Anda menyetel"foo": "workspace:2.0.0"
, kali ini instalasi akan gagal karena"foo@2.0.0"
tidak ada di workspace.
This protocol is especially useful when thelink-workspace-packages option is set tofalse
. Dalam hal ini, pnpm hanya akan menautkan paket dari workspace jika protokolworkspace:
digunakan.
Merujuk paket workspace melalui alias
Katakanlah Anda memiliki paket di workspace bernamafoo
. Biasanya, Anda akan mereferensikannya sebagai"foo": "workspace:*"
.
Jika Anda ingin menggunakan alias yang berbeda, sintaks berikut juga akan berfungsi:"bar": "workspace:foo@*"
.
Sebelum dipublikasikan, alias dikonversi ke dependensi alias biasa. Contoh di atas akan menjadi:"bar": "npm:foo@1.0.0"
.
Merujuk paket workspace melalui jalur relatifnya
Di workspace dengan 2 paket:
+ packages
+ foo
+ bar
bar
mungkin memilikifoo
dalam dependensinya yang dinyatakan sebagai"foo": "workspace:../foo"
. Sebelum dipublikasikan, spesifikasi ini dikonversi ke spesifikasi versi reguler yang didukung oleh semua pengelola paket.
Memublikasikan paket workspace
Ketika paket workspace dikemas ke dalam sebuah arsip (baik itu melaluipnpm pack
atau salah satu perintah terbitkan sepertipnpm publish
), kami secara dinamis menggantikan setiap dependensiworkspace:
dengan:
- Versi yang sesuai di workspace target (jika Anda menggunakan
workspace:*
,workspace:~
, atauworkspace:^
) - Rentang semver terkait (untuk jenis rentang lainnya)
So for example, if we havefoo
,bar
,qar
,zoo
in the workspace and they all are at version1.5.0
, the following:
{
"dependencies":{
"foo":"workspace:*",
"bar":"workspace:~",
"qar":"workspace:^",
"zoo":"workspace:^1.5.0"
}
}
Will be transformed into:
{
"dependencies":{
"foo":"1.5.0",
"bar":"~1.5.0",
"qar":"^1.5.0",
"zoo":"^1.5.0"
}
}
This feature allows you to depend on your local workspace packages while still being able to publish the resulting packages to the remote registry without needing intermediary publish steps - your consumers will be able to use your published workspaces as any other package, still benefitting from the guarantees semver offers.
Alur kerja rilis
Versioning packages inside a workspace is a complex task and pnpm currently does not provide a built-in solution for it. However, there are 2 well tested tools that handle versioning and support pnpm:
For how to set up a repository using Rush, readthis page.
For using Changesets with pnpm, readthis guide.
Pemecahan Masalah
pnpm cannot guarantee that scripts will be run in topological order if there are cycles between workspace dependencies. If pnpm detects cyclic dependencies during installation, it will produce a warning. If pnpm is able to find out which dependencies are causing the cycles, it will display them too.
If you see the messageThere are cyclic workspace dependencies
, please inspect workspace dependencies declared independencies
,optionalDependencies
anddevDependencies
.
Usage examples
Here are a few of the most popular open source projects that use the workspace feature of pnpm: