minify-build-system.scm 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 Ricardo Wurmus <rekado@elephly.net>
  3. ;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
  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 minify-build-system)
  20. #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  21. #:use-module ((guix build minify-build-system) #:prefix minify:)
  22. #:use-module (guix build utils)
  23. #:use-module (srfi srfi-1)
  24. #:use-module (srfi srfi-26)
  25. #:use-module (ice-9 popen)
  26. #:export (%standard-phases
  27. minify-build
  28. minify))
  29. ;; Commentary:
  30. ;;
  31. ;; Builder-side code of the standard minification procedure for JavaScript
  32. ;; files.
  33. ;;
  34. ;; Code:
  35. (define* (minify file #:key target (directory ""))
  36. (format #t "minifying ~a\n" file)
  37. (let* ((base (basename file ".js"))
  38. (installed (or target (string-append directory base ".min.js")))
  39. (minified (open-pipe* OPEN_READ "uglifyjs" file)))
  40. (call-with-output-file installed
  41. (cut dump-port minified <>))
  42. #t))
  43. (define* (build #:key javascript-files
  44. #:allow-other-keys)
  45. (let ((files (or javascript-files
  46. (find-files "src" "\\.js$"))))
  47. (mkdir-p "guix/build")
  48. (every (cut minify <> #:directory "guix/build/") files)))
  49. (define* (install #:key outputs #:allow-other-keys)
  50. (let* ((out (assoc-ref outputs "out"))
  51. (js (string-append out "/share/javascript/")))
  52. (mkdir-p js)
  53. (for-each
  54. (lambda (file)
  55. (if (not (zero? (stat:size (stat file))))
  56. (install-file file js)
  57. (error "File is empty: " file)))
  58. (find-files "guix/build" "\\.min\\.js$")))
  59. #t)
  60. (define %standard-phases
  61. (modify-phases gnu:%standard-phases
  62. (delete 'bootstrap)
  63. (delete 'configure)
  64. (replace 'build build)
  65. (delete 'check)
  66. (replace 'install install)))
  67. (define* (minify-build #:key inputs (phases %standard-phases)
  68. #:allow-other-keys #:rest args)
  69. "Build the given JavaScript package, applying all of PHASES in order."
  70. (apply gnu:gnu-build #:inputs inputs #:phases phases args))
  71. ;;; minify-build-system.scm ends here