Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for pnpm workspaces #35

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

iteratee
Copy link

@iteratee iteratee commented Mar 5, 2024

Currently a monorepo with multiple components doesn't work with the current pnpm2nix.

You can try to build the entire monorepo, but then fail to copy out all of the node_modules directories or you can try and build individual components, but pnpm complains if you pass it the project-wide lockfile for a specific component.

Add workspace and components as an alternative to src. When building the node-modules derivation, copy out the node_modules directory for every named component in addition to the node_modules directory at the root.

When building, pass the list of components as a filter to pnpm run --recursive ${script}. When doing the install, allow the user to specify which directories to copy out, with a default of ${component}/dist for each component.

Currently doesn't have support for building individual components as separate derivations with shared pnpm-store and node-modules derivations.

This works for building a project where we needed workspace support.

It is likely that a user may want to specify environment variables
during the build phase as well as during pnpm install. Add an argument
for a build specific environment. This allows the user to avoid
duplicating the environment between a configure or prebuild step and
installEnv, by using something like:

let
  sharedEnv = { ... };
  buildEnvOverrides = { ... };
  installEnvOverrides = { ... };
in
  mkPnpmPackage {
    buildEnv = sharedEnv // buildEnvOverrides;
    installEnv = sharedEnv // installEnvOverrides;
    ...
  }
Allow a caller to specify a workspace and a list of components rather
than a single source directory. This allows for the building of pnpm
monorepo projects with dependency links between the various projects and
a single pnpm-lock.yaml for the whole workspace. Requires that the
script has the same name in all components.

The location of all of the per-component package.json files is
overridable. A workspace project can still specify the location of the
root package.json via packageJSON, as well as the components'
package.json files via componentPackageJSONs. The list defaults to
c/package.json for c in components.

Similarly, the list of distDirs for the components is overridable. For a
workspace, the distDirs are all subdirs of $out rather than dist
becoming $out. The default list is c/dist for c in components. Allowing
for override handles the case where one component is built by a
different tool like next and produces a different directory like ".next"

NB: Any "link:" dependencies will need to be recreated as a preBuild
step. pnpm will create the symlinks during install, and nix removes them
because when the node_modules derivation is built, they are dangling.

Attempts to leave the non-workspace pnpm package support intact.
A user may want the node_modules and package artifacts as a part of the
output in order to have a runnable build artifact without needing the
node-modules derivation or the sources available. Add options to control
additional copies as a part of the install.

Simplify the values used in the derivation so that the conditional logic
is almost all in the let rather than in the derivation body.

Some of this change should probably rebased into the first commit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant