Can I replace "workspace:*" into the real version as what "pnpm publish" does

219 views Asked by At

I have two packages "app" and "lib". "app" depends on "lib" with the workspace protocol as below

{
  "dependencies": {
    "lib": "workspaces:*"
  }
}

I know that when I execute "pnpm -r publish" the version part can be replaced with the real one, for example "lib": "^1.0.0".

But in my case, I don't want to publish "app" package, just copy source code and run it something like node app/index.js. This means I cannot use "pnpm publish".

Is it possible to manually trigger the workspace replacement manually?

1

There are 1 answers

0
Shaun Xu On

OK after dig into the source code of pnpm I found a solution. Thanks to the monorepo pnpm provided :)

  1. Add two pnpm packages for reading and generating exportable manifest (a.k.a package.json).
pnpm add -wD @pnpm/exportable-manifest @pnpm/read-project-manifest
  1. Create a file in your repository, let's say scripts/create-exportable-manifest.ts, copy the content below.
import { createExportableManifest } from "@pnpm/exportable-manifest";
import { readProjectManifestOnly } from "@pnpm/read-project-manifest";
import { writeFile } from "fs/promises";

(async () => {

    const projectDir = "packages/app"; // folder of "package.json" to be translated
    const distDir = "packages/app/dist"; // folder to save the translated one

    const manifest = await readProjectManifestOnly(projectDir);
    const exportable = await createExportableManifest(projectDir, manifest);
    await writeFile(`${distDir}/package.json`, JSON.stringify(exportable, undefined, 2));

})().then(() => { }).catch(console.error);
  1. Run this file after build. For example, use this command.
{
  "scripts": {
    // ...
    "build": "pnpm -r build && ts-node scripts/create-exportable-manifest.ts"
  }
}
  1. Check out the "package.json" in distDir which should be translated.

Hope this helps,