minify.scm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2018 Ricardo Wurmus <rekado@elephly.net>
  3. ;;; Copyright © 2021 Ludovic Courtès <ludo@gnu.org>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (guix build-system minify)
  20. #:use-module (guix store)
  21. #:use-module (guix utils)
  22. #:use-module (guix packages)
  23. #:use-module (guix gexp)
  24. #:use-module (guix monads)
  25. #:use-module (guix search-paths)
  26. #:use-module (guix build-system)
  27. #:use-module (guix build-system gnu)
  28. #:use-module (ice-9 match)
  29. #:use-module (srfi srfi-26)
  30. #:export (%minify-build-system-modules
  31. minify-build
  32. minify-build-system))
  33. ;; Commentary:
  34. ;;
  35. ;; Standard minification procedure for JavaScript files.
  36. ;;
  37. ;; Code:
  38. (define %minify-build-system-modules
  39. ;; Build-side modules imported by default.
  40. `((guix build minify-build-system)
  41. ,@%gnu-build-system-modules))
  42. (define (default-uglify-js)
  43. "Return the default package to minify JavaScript source files."
  44. ;; Lazily resolve the binding to avoid a circular dependency.
  45. (let ((mod (resolve-interface '(gnu packages uglifyjs))))
  46. (module-ref mod 'uglifyjs)))
  47. (define* (lower name
  48. #:key source inputs native-inputs outputs system
  49. (uglify-js (default-uglify-js))
  50. #:allow-other-keys
  51. #:rest arguments)
  52. "Return a bag for NAME."
  53. (define private-keywords
  54. '(#:target #:inputs #:native-inputs))
  55. (bag
  56. (name name)
  57. (system system)
  58. (host-inputs `(,@(if source
  59. `(("source" ,source))
  60. '())
  61. ,@inputs
  62. ,@(standard-packages)))
  63. (build-inputs `(("uglify-js" ,uglify-js)
  64. ,@native-inputs))
  65. (outputs outputs)
  66. (build minify-build)
  67. (arguments (strip-keyword-arguments private-keywords arguments))))
  68. (define* (minify-build name inputs
  69. #:key
  70. source
  71. (javascript-files #f)
  72. (phases '%standard-phases)
  73. (outputs '("out"))
  74. (system (%current-system))
  75. search-paths
  76. (guile #f)
  77. (imported-modules %minify-build-system-modules)
  78. (modules '((guix build minify-build-system)
  79. (guix build utils))))
  80. "Build SOURCE with INPUTS."
  81. (define builder
  82. (with-imported-modules imported-modules
  83. #~(begin
  84. (use-modules #$@(sexp->gexp modules))
  85. (minify-build #:name #$name
  86. #:source #+source
  87. #:javascript-files #$javascript-files
  88. #:phases #$phases
  89. #:outputs #$(outputs->gexp outputs)
  90. #:search-paths '#$(sexp->gexp
  91. (map search-path-specification->sexp
  92. search-paths))
  93. #:inputs #$(input-tuples->gexp inputs)))))
  94. (mlet %store-monad ((guile (package->derivation (or guile (default-guile))
  95. system #:graft? #f)))
  96. (gexp->derivation name builder
  97. #:system system
  98. #:guile-for-build guile)))
  99. (define minify-build-system
  100. (build-system
  101. (name 'minify)
  102. (description "The trivial JavaScript minification build system")
  103. (lower lower)))
  104. ;;; minify.scm ends here