Merge pull request #233804 from lilyinstarlight/feature/npm-workspaces

buildNpmPackage: add npmWorkspace and npmPruneFlags args

Changed files
+13 -7
doc
languages-frameworks
pkgs
build-support
node
+3 -1
doc/languages-frameworks/javascript.section.md
···
* `npmDepsHash`: The output hash of the dependencies for this project. Can be calculated in advance with [`prefetch-npm-deps`](#javascript-buildNpmPackage-prefetch-npm-deps).
* `makeCacheWritable`: Whether to make the cache writable prior to installing dependencies. Don't set this unless npm tries to write to the cache directory, as it can slow down the build.
* `npmBuildScript`: The script to run to build the project. Defaults to `"build"`.
+
* `npmWorkspace`: The workspace directory within the project to build and install.
* `dontNpmBuild`: Option to disable running the build script. Set to `true` if the package does not have a build script. Defaults to `false`. Alternatively, setting `buildPhase` explicitly also disables this.
* `dontNpmInstall`: Option to disable running `npm install`. Defaults to `false`. Alternatively, setting `installPhase` explicitly also disables this.
* `npmFlags`: Flags to pass to all npm commands.
-
* `npmInstallFlags`: Flags to pass to `npm ci` and `npm prune`.
+
* `npmInstallFlags`: Flags to pass to `npm ci`.
* `npmBuildFlags`: Flags to pass to `npm run ${npmBuildScript}`.
* `npmPackFlags`: Flags to pass to `npm pack`.
+
* `npmPruneFlags`: Flags to pass to `npm prune`. Defaults to the value of `npmInstallFlags`.
#### prefetch-npm-deps {#javascript-buildNpmPackage-prefetch-npm-deps}
+5 -1
pkgs/build-support/node/build-npm-package/default.nix
···
, npmBuildScript ? "build"
# Flags to pass to all npm commands.
, npmFlags ? [ ]
-
# Flags to pass to `npm ci` and `npm prune`.
+
# Flags to pass to `npm ci`.
, npmInstallFlags ? [ ]
# Flags to pass to `npm rebuild`.
, npmRebuildFlags ? [ ]
···
, npmBuildFlags ? [ ]
# Flags to pass to `npm pack`.
, npmPackFlags ? [ ]
+
# Flags to pass to `npm prune`.
+
, npmPruneFlags ? npmInstallFlags
+
# Value for npm `--workspace` flag and directory in which the files to be installed are found.
+
, npmWorkspace ? null
, ...
} @ args:
+1 -1
pkgs/build-support/node/build-npm-package/hooks/npm-build-hook.sh
···
exit 1
fi
-
if ! npm run "$npmBuildScript" $npmBuildFlags "${npmBuildFlagsArray[@]}" $npmFlags "${npmFlagsArray[@]}"; then
+
if ! npm run ${npmWorkspace+--workspace=$npmWorkspace} "$npmBuildScript" $npmBuildFlags "${npmBuildFlagsArray[@]}" $npmFlags "${npmFlagsArray[@]}"; then
echo
echo 'ERROR: `npm build` failed'
echo
+4 -4
pkgs/build-support/node/build-npm-package/hooks/npm-install-hook.sh
···
while IFS= read -r file; do
local dest="$packageOut/$(dirname "$file")"
mkdir -p "$dest"
-
cp "$file" "$dest"
-
done < <(@jq@ --raw-output '.[0].files | map(.path) | join("\n")' <<< "$(npm pack --json --dry-run $npmPackFlags "${npmPackFlagsArray[@]}" $npmFlags "${npmFlagsArray[@]}")")
+
cp "${npmWorkspace-.}/$file" "$dest"
+
done < <(@jq@ --raw-output '.[0].files | map(.path) | join("\n")' <<< "$(npm pack --json --dry-run ${npmWorkspace+--workspace=$npmWorkspace} $npmPackFlags "${npmPackFlagsArray[@]}" $npmFlags "${npmFlagsArray[@]}")")
while IFS=" " read -ra bin; do
mkdir -p "$out/bin"
···
done < <(@jq@ --raw-output '(.bin | type) as $typ | if $typ == "string" then
.name + " " + .bin
elif $typ == "object" then .bin | to_entries | map(.key + " " + .value) | join("\n")
-
else "invalid type " + $typ | halt_error end' package.json)
+
else "invalid type " + $typ | halt_error end' "${npmWorkspace-.}/package.json")
local -r nodeModulesPath="$packageOut/node_modules"
if [ ! -d "$nodeModulesPath" ]; then
if [ -z "${dontNpmPrune-}" ]; then
-
npm prune --omit dev --no-save $npmInstallFlags "${npmInstallFlagsArray[@]}" $npmFlags "${npmFlagsArray[@]}"
+
npm prune --omit=dev --no-save ${npmWorkspace+--workspace=$npmWorkspace} $npmPruneFlags "${npmPruneFlagsArray[@]}" $npmFlags "${npmFlagsArray[@]}"
fi
find node_modules -maxdepth 1 -type d -empty -delete