diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..8effa2d --- /dev/null +++ b/Readme.md @@ -0,0 +1,15 @@ +# GHCJS-optimizer + +This is a simple nix wrapper that optimizes the js with [Google Closure Compiler](https://github.com/google/closure-compiler) and compresses text files with [zopfli](https://github.com/google/zopfli). + +This is very simple and intended for a pretty narrow use case: it is intended for use with [haskell.nix](https://github.com/input-output-hk/haskell.nix) (but will probably work with other nix builders). It also only uses the `all.js` file, since it allows more room for optimization, and having one file in most cases will be better than loading multiple. This means that the default `index.html` generated by the stock builder will not work. I replace `index.html` with one that only uses `all.js`. + +## Example + +```nix +let ghcjs-optimizer = import ../ghcjs-optimizer { pkgs = nixpkgs; }; + in ghcjs-optimizer hsPkgs.projectCross.ghcjs.hsPkgs..components.exes.; +``` + + +I suggest you use [niv](https://github.com/nmattia/niv) for nix dependencies like this. diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..0cfccbd --- /dev/null +++ b/default.nix @@ -0,0 +1,20 @@ +{ pkgs ? null +, closurecompiler ? pkgs.closurecompiler +, zopfli ? pkgs.zopfli +, useZopfli ? true +, preserveHTML ? false +}: +input: +pkgs.runCommand "${input.name}-optimized" +{ } '' + shopt -s globstar + mkdir $out + + cp ${if preserveHTML then "${input}/bin/*/index.html" else ./index.html} $out/index.html + + chmod +w -R $out + + ${closurecompiler}/bin/closure-compiler --compilation_level ADVANCED --jscomp_off=checkVars --warning_level QUIET --js ${input}/bin/*/all.js --externs ${input}/bin/*/all.js.externs --js_output_file $out/all.js + + ${ if useZopfli then "${zopfli}/bin/zopfli $out/**/*.{js,css,json,html}" else "" } +'' diff --git a/index.html b/index.html new file mode 100644 index 0000000..cc876f1 --- /dev/null +++ b/index.html @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file