PNPM build and publish TypeScript monorepo

637 views Asked by At

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.

1

There are 1 answers

0
Shaun Xu On

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 build packages/lib into packages/lib/dist and packages/app into packages/app/dist. We also need to copy package.json into dist as well.

Then add publishConfig.directory section in each package.json as below, indicates to pack the files under dist folder.

{
  // ...
  "publishConfig": {
    "directory": "dist"
  }
}

Now we can use pnpm -r publish to pack and publish each packages with the workspace part translated.