minify.scm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2018 Ricardo Wurmus <rekado@elephly.net>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix build-system minify)
  19. #:use-module (guix store)
  20. #:use-module (guix utils)
  21. #:use-module (guix packages)
  22. #:use-module (guix derivations)
  23. #:use-module (guix search-paths)
  24. #:use-module (guix build-system)
  25. #:use-module (guix build-system gnu)
  26. #:use-module (ice-9 match)
  27. #:use-module (srfi srfi-26)
  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 lisp-xyz))))
  44. (module-ref mod 'uglify-js)))
  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. '(#:source #: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 store name inputs
  67. #:key
  68. (javascript-files #f)
  69. (phases '(@ (guix build minify-build-system)
  70. %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. `(begin
  81. (use-modules ,@modules)
  82. (minify-build #:name ,name
  83. #:source ,(match (assoc-ref inputs "source")
  84. (((? derivation? source))
  85. (derivation->output-path source))
  86. ((source)
  87. source)
  88. (source
  89. source))
  90. #:javascript-files ,javascript-files
  91. #:phases ,phases
  92. #:outputs %outputs
  93. #:search-paths ',(map search-path-specification->sexp
  94. search-paths)
  95. #:inputs %build-inputs)))
  96. (define guile-for-build
  97. (match guile
  98. ((? package?)
  99. (package-derivation store guile system #:graft? #f))
  100. (#f ; the default
  101. (let* ((distro (resolve-interface '(gnu packages commencement)))
  102. (guile (module-ref distro 'guile-final)))
  103. (package-derivation store guile system #:graft? #f)))))
  104. (build-expression->derivation store name builder
  105. #:inputs inputs
  106. #:system system
  107. #:modules imported-modules
  108. #:outputs outputs
  109. #:guile-for-build guile-for-build))
  110. (define minify-build-system
  111. (build-system
  112. (name 'minify)
  113. (description "The trivial JavaScript minification build system")
  114. (lower lower)))
  115. ;;; minify.scm ends here