I have a TypeScript monorepo with PNPM. There are two projects in packages
: app
and lib
.
app
depends on lib
with PNPM workspace feature. The package.json
file of app
looks like below, as well as the build script.
{
// ...
"scripts": {
"build": "tsc"
},
"dependencies": {
"@shaunxu/lib": "workspace:^"
}
}
Now I would like to build app
and lib
into JS to the folder dist/app
and dist/lib
so that it can be published to NPM registry. If I run pnpm -r build
it will compile my TypeScript into JavaScript but the dependencies
part of package.json
was NOT translated. If I run pnpm -r publish
the dependencies
part was changed correctly but it pack my TypeScript file.
How can I combine the build (compile) and publish (or dependencies
translate) process.
Well I finally figured out this issue. Basically it seems impossible to publish packages from a root-level
dist
folder because it out of the workspace range.What we need to do is to build each package into its own
dist
folder. For instance we need to buildpackages/lib
intopackages/lib/dist
andpackages/app
intopackages/app/dist
. We also need to copypackage.json
intodist
as well.Then add
publishConfig.directory
section in eachpackage.json
as below, indicates to pack the files underdist
folder.Now we can use
pnpm -r publish
to pack and publish each packages with the workspace part translated.