git.iko.soy/ghcjs-optimizer
on main help sign in
First release
commitafcb48e7e2917a577273b0cbad781c03011da7c8 authoriko <ilyakooo0@gmail.com> dateSun 12 Sep 2021, 15:12 (5 years ago) parentc3878fb
3 files changed+47
Readme.md+15 default.nix+20 index.html+12
Readme.mdnew file+15
@@ -0,0 +1,15 @@
1+# GHCJS-optimizer
2+
3+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).
4+
5+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`.
6+
7+## Example
8+
9+```nix
10+let ghcjs-optimizer = import ../ghcjs-optimizer { pkgs = nixpkgs; };
11+ in ghcjs-optimizer hsPkgs.projectCross.ghcjs.hsPkgs.<your project>.components.exes.<your executable>;
12+```
13+
14+
15+I suggest you use [niv](https://github.com/nmattia/niv) for nix dependencies like this.
default.nixnew file+20
@@ -0,0 +1,20 @@
1+{ pkgs ? null
2+, closurecompiler ? pkgs.closurecompiler
3+, zopfli ? pkgs.zopfli
4+, useZopfli ? true
5+, preserveHTML ? false
6+}:
7+input:
8+pkgs.runCommand "${input.name}-optimized"
9+{ } ''
10+ shopt -s globstar
11+ mkdir $out
12+
13+ cp ${if preserveHTML then "${input}/bin/*/index.html" else ./index.html} $out/index.html
14+
15+ chmod +w -R $out
16+
17+ ${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
18+
19+ ${ if useZopfli then "${zopfli}/bin/zopfli $out/**/*.{js,css,json,html}" else "" }
20+''
index.htmlnew file+12
@@ -0,0 +1,12 @@
1+<!DOCTYPE html>
2+<html>
3+
4+<head>
5+ <meta charset="UTF-8">
6+</head>
7+
8+<body>
9+</body>
10+<script language="javascript" src="all.js"></script>
11+
12+</html>