pnpm.fetchDeps: pnpmWorkspace -> pnpmWorkspaces

This commit is contained in:
Pyrox 2024-10-24 01:06:27 -04:00
parent 02ba5770a7
commit 09cec0f560
No known key found for this signature in database
GPG Key ID: 8CDF3F7CAA53A0F5
3 changed files with 45 additions and 11 deletions

View File

@ -428,7 +428,26 @@ NOTE: It is highly recommended to use a pinned version of pnpm (i.e. `pnpm_8` or
In case you are patching `package.json` or `pnpm-lock.yaml`, make sure to pass `finalAttrs.patches` to the function as well (i.e. `inherit (finalAttrs) patches`. In case you are patching `package.json` or `pnpm-lock.yaml`, make sure to pass `finalAttrs.patches` to the function as well (i.e. `inherit (finalAttrs) patches`.
`pnpm.configHook` supports adding additional `pnpm install` flags via `pnpmInstallFlags` which can be set to a Nix string array. `pnpm.configHook` supports adding additional `pnpm install` flags via `pnpmInstallFlags` which can be set to a Nix string array:
```nix
{
pnpm,
}:
stdenv.mkDerivation (finalAttrs: {
pname = "foo";
version = "0-unstable-1980-01-01";
src = ...;
pnpmInstallFlags = [ "--shamefully-hoist" ];
pnpmDeps = pnpm.fetchDeps {
inherit (finalAttrs) pnpmInstallFlags;
};
})
```
#### Dealing with `sourceRoot` {#javascript-pnpm-sourceRoot} #### Dealing with `sourceRoot` {#javascript-pnpm-sourceRoot}
@ -459,16 +478,16 @@ Assuming the following directory structure, we can define `sourceRoot` and `pnpm
#### PNPM Workspaces {#javascript-pnpm-workspaces} #### PNPM Workspaces {#javascript-pnpm-workspaces}
If you need to use a PNPM workspace for your project, then set `pnpmWorkspace = "<workspace project name>"` in your `pnpm.fetchDeps` call, If you need to use a PNPM workspace for your project, then set `pnpmWorkspaces = [ "<workspace project name 1>" "<workspace project name 2>" ]`, etc, in your `pnpm.fetchDeps` call,
which will make PNPM only install dependencies for that workspace package. which will make PNPM only install dependencies for those workspace packages.
For example: For example:
```nix ```nix
... ...
pnpmWorkspace = "@astrojs/language-server"; pnpmWorkspaces = [ "@astrojs/language-server" ];
pnpmDeps = pnpm.fetchDeps { pnpmDeps = pnpm.fetchDeps {
inherit (finalAttrs) pnpmWorkspace; inherit (finalAttrs) pnpmWorkspaces;
... ...
} }
``` ```
@ -476,7 +495,7 @@ pnpmDeps = pnpm.fetchDeps {
The above would make `pnpm.fetchDeps` call only install dependencies for the `@astrojs/language-server` workspace package. The above would make `pnpm.fetchDeps` call only install dependencies for the `@astrojs/language-server` workspace package.
Note that you do not need to set `sourceRoot` to make this work. Note that you do not need to set `sourceRoot` to make this work.
Usually in such cases, you'd want to use `pnpm --filter=$pnpmWorkspace build` to build your project, as `npmHooks.npmBuildHook` probably won't work. A `buildPhase` based on the following example will probably fit most workspace projects: Usually in such cases, you'd want to use `pnpm --filter=<pnpm workspace name> build` to build your project, as `npmHooks.npmBuildHook` probably won't work. A `buildPhase` based on the following example will probably fit most workspace projects:
```nix ```nix
buildPhase = '' buildPhase = ''

View File

@ -15,7 +15,7 @@
{ {
hash ? "", hash ? "",
pname, pname,
pnpmWorkspace ? "", pnpmWorkspaces ? [ ],
prePnpmInstall ? "", prePnpmInstall ? "",
... ...
}@args: }@args:
@ -32,8 +32,14 @@
outputHash = ""; outputHash = "";
outputHashAlgo = "sha256"; outputHashAlgo = "sha256";
}; };
installFlags = lib.optionalString (pnpmWorkspace != "") "--filter=${pnpmWorkspace}";
filterFlags = lib.map (package: "--filter=${package}") pnpmWorkspaces;
in in
# pnpmWorkspace was deprecated, so throw if it's used.
assert (lib.throwIf (args ? pnpmWorkspace)
"pnpm.fetchDeps: `pnpmWorkspace` is no longer supported, please migrate to `pnpmWorkspaces`."
) true;
stdenvNoCC.mkDerivation ( stdenvNoCC.mkDerivation (
finalAttrs: finalAttrs:
( (
@ -74,6 +80,7 @@
--force \ --force \
--ignore-scripts \ --ignore-scripts \
${installFlags} \ ${installFlags} \
${lib.escapeShellArgs filterFlags} \
--frozen-lockfile --frozen-lockfile
runHook postInstall runHook postInstall

View File

@ -22,11 +22,19 @@ pnpmConfigHook() {
pnpm config set store-dir "$STORE_PATH" pnpm config set store-dir "$STORE_PATH"
echo "Installing dependencies"
if [[ -n "$pnpmWorkspace" ]]; then if [[ -n "$pnpmWorkspace" ]]; then
pnpmInstallFlags+=("--filter=$pnpmWorkspace") echo "'pnpmWorkspace' is deprecated, please migrate to 'pnpmWorkspaces'."
exit 2
fi fi
echo "Installing dependencies"
if [[ -n "$pnpmWorkspaces" ]]; then
local IFS=" "
for ws in $pnpmWorkspaces; do
pnpmInstallFlags+=("--filter=$ws")
done
fi
runHook prePnpmInstall runHook prePnpmInstall
pnpm install \ pnpm install \