pnpm icon indicating copy to clipboard operation
pnpm copied to clipboard

Apply publishConfig for workspace packages on deployment

Open vteivans opened this issue 1 year ago • 5 comments

Describe the user story

When I run pnpm --filter=<deployed project name> deploy <target directory> command on an application in workspace it copies related packages from the workspace to <target directory>/node_modules as they are. This means that the package.json files in workspace packages must be in "publishable" state (e.g. "main": "dist/index.js"). This makes the packages harder to use in development mode as they must be rebuilt on any change for the dist to represent the actual state.

I would like the package.json to be transformed from "development" state to "publishing" state for any packages that get copied over by pnpm deploy command.

Describe the solution you'd like

As far as I understand pnpm already has a solution for this. When pnpm pack (or publish) is ran, publishConfig from package.json is applied. This allows for easy switching from "development" to "publishing" state. I suggest applying the same logic on pnpm deploy - if the package that is being copied has publishConfig apply it during pnpm deploy.

Describe the drawbacks of your solution

Not sure how it could break as this would be the normal package state when it's installed from a repository.

vteivans avatar Jun 20 '23 20:06 vteivans

I got the same problem, is there any solution?

logan70 avatar Jul 28 '23 01:07 logan70

The solution we've landed on so far is to have a POST install script, that looks something like this:

const writeFileSync = require('fs').writeFileSync;

const pkgJsonPath = './package.json';

const json = require(pkgJsonPath);

if (
  !json.hasOwnProperty('publishConfig') ||
  !json.publishConfig ||
  typeof json.publishConfig !== 'object'
) {
  return;
}

for (const prop of ['main', 'typings']) {
  if (json.publishConfig.hasOwnProperty(prop)) {
    json[prop] = json.publishConfig[prop];
  }
}

writeFileSync(pkgJsonPath, JSON.stringify(json, null, 2));

Keep in mind that it needs to be adjusted for when should the script be run (probably not on every package install, but only for deployment). Also the location of package.json might change if this code should be made in to a package.

vteivans avatar Aug 14 '23 11:08 vteivans

Took an attempt at implementing this: https://github.com/pnpm/pnpm/pull/6943/files

Alongside the publishConfig application, will also transform workspace: dependencies into the literal version.

Potentially this is a breaking change because it now requires install to run prior to deploy, but I believe that it is a reasonable assumption users are already doing that, especially considering these packages are assumed to have been "built" prior to deploy as well.

Agreed with @vteivans that the application of publishConfig itself should not be considered a breaking change. It could be, but that would generally mean the config is incorrectly setup.

JacobLey avatar Aug 15 '23 21:08 JacobLey

Thank you very much @jakebailey!

vteivans avatar Aug 24 '23 17:08 vteivans

I don't remember how hard it was to fix all the issues that were caused by the change. But I will try to get it to v9. Created a PR again https://github.com/pnpm/pnpm/pull/7647

zkochan avatar Feb 13 '24 00:02 zkochan