closure-compile
1 Extended example
Version: 5.2.1

closure-compile: Compile and compress JavaScript source with the Google Closure Compiler

This library exposes the Google Closure compiler to Racket.

The module requires runtime access to Java; the value of (find-executable-path "java") should point to a valid Java executable.

 (require (planet dyoo/closure-compile:1:=3))
(closure-compile code [compilation-level])  string?
  code : string?
  compilation-level : (or/c 'whitespace 'simple 'advanced)
   = 'simple
closure-compile takes the given code and passes it to the Closure compiler. It should return a minified version of code. compilation-level adjusts the optimization that the Closure compiler will perform.

If anything bad happens, it will raise an exn:fail and hold the error message in the exception’s exn-message.

Examples:

> (closure-compile "alert('hello ' + 'world');")

"alert(\"hello world\");\n"

> (closure-compile "{this should raise an error")

closure-compile: stdin:1: ERROR - Parse error. missing ;

before statement

{this should raise an error

             ^

stdin:1: ERROR - Parse error. missing } in compound

statement

{this should raise an error

                           ^

2 error(s), 0 warning(s)

> (closure-compile "alert('hello, I see: ' + (3 + 4) + '!');"
                   'whitespace)

"alert(\"hello, I see: \"+(3+4)+\"!\");\n"

> (closure-compile "alert('hello, I see: ' + (3 + 4) + '!');"
                   'simple)

"alert(\"hello, I see: 7!\");\n"

> (closure-compile "\n                       var f = function(x) { \n                           return x * x; \n                       };\n                       alert( f(3) );")

"var f=function(a){return a*a};alert(f(3));\n"

> (closure-compile "\n                       var f = function(x) { \n                           return x * x; \n                       };\n                       alert( f(3) );"
  
  
  
  
                   'advanced)

"alert(9);\n"

1 Extended example

Here’s an extended example of a script that uses the package to closure-compile all the ".js" files in a directory.

#lang racket/base
(require racket/path
         racket/file
         racket/cmdline
         (planet dyoo/closure-compile))
 
;; This program compresses all of the JavaScript files using Closure Compiler,
;; with simple optimizations.  All ".js" files (excluding the -min.js files)
;; get compressed here.
(define path (command-line #:args (p) p))
 
(define js-files (find-files
                  (lambda (p)
                    (and (file-exists? p)
                         (regexp-match #px".js$" (path->string (file-name-from-path p)))
                         (not (regexp-match #px"-min.js$" (path->string (file-name-from-path p))))))
                  (simplify-path path)))
 
;; out-of-date?: path path -> boolean
;; Returns true if the target file looks at least as new as the source file.
(define (out-of-date? source-file target-file)
  (cond
   [(not (file-exists? target-file))
    #t]
   [else
    (>= (file-or-directory-modify-seconds source-file)
        (file-or-directory-modify-seconds target-file))]))
 
(for ([file js-files])
   (define new-path (regexp-replace #px".js$" (path->string file) "-min.js"))
   (cond [(out-of-date? file new-path)
          (printf "Compressing ~s\n" (path->string file))
          (define text (file->string file))
          (define compressed (closure-compile text))
          (call-with-output-file new-path (lambda (op) (display compressed op)) #:exists 'replace)]
         [else
          (printf "Skipping ~s: up to date\n" file)]))