minify-build-system.scm 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 match)
  26. #:use-module (ice-9 popen)
  27. #:export (%standard-phases
  28. minify-build
  29. minify))
  30. ;; Commentary:
  31. ;;
  32. ;; Builder-side code of the standard minification procedure for JavaScript
  33. ;; files.
  34. ;;
  35. ;; Code:
  36. (define* (minify file #:key target (directory ""))
  37. (format #t "minifying ~a\n" file)
  38. (let* ((base (basename file ".js"))
  39. (installed (or target (string-append directory base ".min.js")))
  40. (minified (open-pipe* OPEN_READ "uglifyjs" file)))
  41. (call-with-output-file installed
  42. (cut dump-port minified <>))
  43. (match (close-pipe minified)
  44. (0 #t)
  45. (status
  46. (error "uglify-js failed" status)))))
  47. (define* (build #:key javascript-files
  48. #:allow-other-keys)
  49. (let ((files (or javascript-files
  50. (find-files "src" "\\.js$"))))
  51. (mkdir-p "guix/build")
  52. (for-each (cut minify <> #:directory "guix/build/") files)))
  53. (define* (install #:key outputs #:allow-other-keys)
  54. (let* ((out (assoc-ref outputs "out"))
  55. (js (string-append out "/share/javascript/")))
  56. (mkdir-p js)
  57. (for-each
  58. (lambda (file)
  59. (if (not (zero? (stat:size (stat file))))
  60. (install-file file js)
  61. (error "File is empty: " file)))
  62. (find-files "guix/build" "\\.min\\.js$"))))
  63. (define %standard-phases
  64. (modify-phases gnu:%standard-phases
  65. (delete 'bootstrap)
  66. (delete 'configure)
  67. (replace 'build build)
  68. (delete 'check)
  69. (replace 'install install)))
  70. (define* (minify-build #:key inputs (phases %standard-phases)
  71. #:allow-other-keys #:rest args)
  72. "Build the given JavaScript package, applying all of PHASES in order."
  73. (apply gnu:gnu-build #:inputs inputs #:phases phases args))
  74. ;;; minify-build-system.scm ends here