Subject: [bug#77231] [PATCH 4/6] build-system/node: Add phase 'delete-unwanted-dev-dependencies.

From: Nicolas Graves <[email-masked]>
Date: Mon, 24 Mar 2025 08:29:15 +0100

* guix/build/node-build-system.scm (npm-ignored-inputs): New variable.
(delete-dependencies): Extend procedure to accept a filtering
procedure.
(delete-unwanted-dev-dependencies): Define new phase to ignore all
npm-ignored-inputs systematically.
(%standard-phases): Add it here.
---
 guix/build/node-build-system.scm | 39 ++++++++++++++++++++++++++++----
 1 file changed, 35 insertions(+), 4 deletions(-)

diff --git a/guix/build/node-build-system.scm b/guix/build/node-build-system.scm
index 05940bc997..50e023b3ca 100644
--- a/guix/build/node-build-system.scm
+++ b/guix/build/node-build-system.scm
@@ -29,6 +29,7 @@ (define-module (guix build node-build-system)
   #:use-module (ice-9 match)
   #:use-module (json)
   #:use-module (srfi srfi-1)
+  #:use-module (srfi srfi-26)
   #:use-module (srfi srfi-71)
   #:export (%standard-phases
             delete-dependencies
@@ -37,9 +38,21 @@ (define-module (guix build node-build-system)
             modify-json
             modify-json-fields
             node-build
+            npm-ignored-inputs
             replace-fields
             with-atomic-json-file-replacement))
 
+(define npm-ignored-inputs
+  (list
+   (list "aud" "nsp" ; passed end-of-life
+         "covert" ; code coverage
+         "auto-changelog" "npmignore" "evalmd" ; development tools
+         "eclint" "eslint" "prettier-standard" "standard" ; lint
+         "in-publish" "np" "safe-publish-latest") ; upload integration tools
+   ;; Second value is a list of prefixes to ignore
+   ;; Handy for personal configs and extensions of ignored inputs
+   (list "@ljharb/" "eslint-")))
+
 (define* (assoc-ref* alist key #:optional default)
   "Like assoc-ref, but return DEFAULT instead of #f if no value exists."
   (match (assoc key alist)
@@ -100,7 +113,7 @@ (define* (modify-json #:key (file "package.json") #:rest all-arguments)
           modifications))
       file)))
 
-(define (delete-dependencies dependencies-to-remove)
+(define (delete-dependencies predicate-or-dependencies)
   "Rewrite 'package.json' to allow the build to proceed without packages
 listed in 'dependencies-to-remove', a list of strings naming npm packages.
 
@@ -114,9 +127,13 @@ (define (delete-dependencies dependencies-to-remove)
           dependency-key
           (lambda (dependencies)
             (remove
-              (lambda (dependency)
-                (member (car dependency) dependencies-to-remove))
-              dependencies))))
+             (match predicate-or-dependencies
+               ((? procedure? predicate)
+                predicate)
+               ((? list? dependencies-to-remove)
+                (lambda (dependency)
+                  (member (car dependency) dependencies-to-remove))))
+             dependencies))))
       pkg-meta
       (list
         "devDependencies"
@@ -399,11 +416,25 @@ (define scripts
                                   "echo Guix: avoiding node-gyp rebuild"))
           out)))))
 
+(define delete-unwanted-dev-dependencies
+  (lambda* args
+    (modify-json
+     (delete-dependencies
+      (lambda (input)
+        (match npm-ignored-inputs
+          (((ignored ...) (prefixes ...) . ())
+           (or (member (car input) ignored)
+               (any (cut string-prefix? <> (car input))
+                    prefixes)))
+          (_ #f)))))))
+
 (define %standard-phases
   (modify-phases gnu:%standard-phases
     (add-after 'unpack 'set-home set-home)
     (add-before 'configure 'patch-dependencies patch-dependencies)
     (add-after 'patch-dependencies 'delete-lockfiles delete-lockfiles)
+    (add-after 'patch-dependencies 'delete-unwanted-dev-dependencies
+      delete-unwanted-dev-dependencies)
     (replace 'configure configure)
     (replace 'build build)
     (replace 'check check)
-- 
2.48.1