git.iko.soy/ghcjs-optimizer
on main help sign in
Slowly becoming actually usable
commit7a2e50d588b12519e2800dfa25aadaf89e627948 authoriko <ilyakooo0@gmail.com> dateSun 5 Dec 2021, 12:17 (5 years ago) parentf6c268c
1 file changed+57−18
default.nix+57−18
default.nix+57−18
@@ -1,30 +1,69 @@
11{ pkgs ? null
22, closurecompiler ? pkgs.closurecompiler
33, zopfli ? pkgs.zopfli
4−, useZopfli ? true
5−, useClosureCompiler ? true
6−, createHTML ? true
7−, allJsPath ? "/bin/*/all.js"
4+, fileExtentionsToZopfli ? [ "js" "css" "json" "html" ]
5+, jsToOptimize ? [ ]
6+, filesToCopy ? { "" = "*"; }
7+, copyInsteadOfOptimizeJs ? false
88}:
99input:
10+let
11+ flattenDirAttrSet' = prfx: x:
12+ with builtins;
13+ if isAttrs x
14+ then concatLists (pkgs.lib.mapAttrsToList (p: v: flattenDirAttrSet' (prfx + "/" + p) v) x)
15+ else [{ p = prfx; v = x; }];
16+ flattenDirAttrSet = name: x:
17+ if builtins.isAttrs x
18+ then (flattenDirAttrSet' "" x)
19+ else builtins.hrow "${name} should be an attribute set.";
20+
21+
22+ flattenFileTree = name: x: builtins.map ({ p, v }: p + "/" + v) (flattenDirAttrSet name x);
23+
24+ filesToList = name: x:
25+ if builtins.isAttrs x
26+ then flattenFileTree x
27+ else if builtins.isList x
28+ then x
29+ else builtins.throw "${name} should be either a list or an attribute set.";
30+ jsToOptimizeList = flattenDirAttrSet "jsToOptimize" jsToOptimize;
31+in
1032pkgs.runCommand "${input.name}-optimized"
1133{ } ''
12− shopt -s globstar
13− mkdir $out
1434
15− ${if createHTML then "cp ${input}/bin/*/index.html $out/index.html" else ""}
35+ cd ${input}
36+ shopt -s globstar
37+ mkdir $out
38+
39+ chmod +w -R $out
1640
17− cp -afv ${input}/* $out
41+ ${
42+ builtins.concatStringsSep "\n"
43+ (builtins.map ({p, v}: "cp -afv ${v} $out${p}")
44+ (flattenDirAttrSet "filesToCopy" filesToCopy ++
45+ pkgs.lib.optionals copyInsteadOfOptimizeJs jsToOptimizeList))
46+ }
1847
19− chmod +w -R $out
48+ chmod +w -R $out
2049
21− rm $out${allJsPath} $out${input}${allJsPath}.externs $out/all.js $out/all.js.externs || true
22−
23− ${
24− if useClosureCompiler
25− then "${closurecompiler}/bin/closure-compiler --compilation_level ADVANCED --jscomp_off=checkVars --warning_level QUIET --js ${input}${allJsPath} --externs ${input}${allJsPath}.externs --js_output_file $out/all.js"
26− else ""
27− }
50+ ${
51+ pkgs.lib.optionalString (!copyInsteadOfOptimizeJs)
52+ (builtins.concatStringsSep "\n"
53+ (builtins.map ({p, v}:
54+ let
55+ i = v;
56+ o = p;
57+ in ''
58+ rm -f $out${o}
59+ ${closurecompiler}/bin/closure-compiler --compilation_level ADVANCED --jscomp_off=checkVars --warning_level QUIET --js ${input}/${i} --externs ${input}/${i}.externs --js_output_file $out${o}
60+ '')
61+ (jsToOptimizeList)))
62+ }
2863
29− ${ if useZopfli then "${zopfli}/bin/zopfli $out/**/*.{js,css,json,html}" else "" }
30−''
64+ ${
65+ if fileExtentionsToZopfli != []
66+ then "${zopfli}/bin/zopfli $out/**/*.{${builtins.concatStringsSep "\n" fileExtentionsToZopfli}}"
67+ else ""
68+ }
69+ ''