minify.scm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #:export (%minify-build-system-modules
  29. minify-build
  30. minify-build-system))
  31. ;; Commentary:
  32. ;;
  33. ;; Standard minification procedure for JavaScript files.
  34. ;;
  35. ;; Code:
  36. (define %minify-build-system-modules
  37. ;; Build-side modules imported by default.
  38. `((guix build minify-build-system)
  39. ,@%gnu-build-system-modules))
  40. (define (default-uglify-js)
  41. "Return the default package to minify JavaScript source files."
  42. ;; Lazily resolve the binding to avoid a circular dependency.
  43. (let ((mod (resolve-interface '(gnu packages uglifyjs))))
  44. (module-ref mod 'uglifyjs)))
  45. (define* (lower name
  46. #:key source inputs native-inputs outputs system
  47. (uglify-js (default-uglify-js))
  48. #:allow-other-keys
  49. #:rest arguments)
  50. "Return a bag for NAME."
  51. (define private-keywords
  52. '(#:target #:inputs #:native-inputs))
  53. (bag
  54. (name name)
  55. (system system)
  56. (host-inputs `(,@(if source
  57. `(("source" ,source))
  58. '())
  59. ,@inputs
  60. ,@(standard-packages)))
  61. (build-inputs `(("uglify-js" ,uglify-js)
  62. ,@native-inputs))
  63. (outputs outputs)
  64. (build minify-build)
  65. (arguments (strip-keyword-arguments private-keywords arguments))))
  66. (define* (minify-build name inputs
  67. #:key
  68. source
  69. (javascript-files #f)
  70. (phases '%standard-phases)
  71. (outputs '("out"))
  72. (system (%current-system))
  73. search-paths
  74. (guile #f)
  75. (imported-modules %minify-build-system-modules)
  76. (modules '((guix build minify-build-system)
  77. (guix build utils))))
  78. "Build SOURCE with INPUTS."
  79. (define builder
  80. (with-imported-modules imported-modules
  81. #~(begin
  82. (use-modules #$@(sexp->gexp modules))
  83. (minify-build #:name #$name
  84. #:source #+source
  85. #:javascript-files #$javascript-files
  86. #:phases #$phases
  87. #:outputs #$(outputs->gexp outputs)
  88. #:search-paths '#$(sexp->gexp
  89. (map search-path-specification->sexp
  90. search-paths))
  91. #:inputs #$(input-tuples->gexp inputs)))))
  92. (mlet %store-monad ((guile (package->derivation (or guile (default-guile))
  93. system #:graft? #f)))
  94. (gexp->derivation name builder
  95. #:system system
  96. #:guile-for-build guile)))
  97. (define minify-build-system
  98. (build-system
  99. (name 'minify)
  100. (description "The trivial JavaScript minification build system")
  101. (lower lower)))
  102. ;;; minify.scm ends here