Skip to content

Commit 81cb16a

Browse files
committed
README, doc, pretty
1 parent b6cc6f8 commit 81cb16a

3 files changed

Lines changed: 130 additions & 27 deletions

File tree

README.md

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,73 @@
1-
a boot task to uglify some JS files
1+
A boot task to uglify JS files
2+
3+
No man river
4+
5+
6+
Intended usage :
7+
8+
As an extra optimisation step in the release process to make the output file smaller.
9+
10+
11+
Relevant parts to add to the `build.boot` :
12+
13+
14+
```
15+
(set-env! :dependencies '[;; ...
16+
[nha/boot-uglify "0.0.1"]
17+
])
18+
19+
(require
20+
;;...
21+
'[nha.boot-uglify :refer [uglify]]
22+
)
23+
24+
;; sample task
25+
(deftask package
26+
"Build the package"
27+
[]
28+
(comp
29+
;;(watch)
30+
(cljs :optimizations :advanced)
31+
(uglify)
32+
;;(aot)
33+
;;(pom)
34+
;;(uber)
35+
;;(jar)
36+
))
37+
38+
```
39+
40+
The options listed at See http://lisperator.net/uglifyjs/compress are supported.
41+
Additionally, :mangle is supported.
42+
43+
The defaults :
44+
45+
```
46+
{
47+
:sequences true, ; join consecutive statemets with the “comma operator”
48+
:properties true, ; optimize property access: a["foo"] → a.foo
49+
:dead_code true, ; discard unreachable code
50+
:drop_debugger true, ; discard “debugger” statements
51+
:unsafe false, ; some unsafe optimizations
52+
:conditionals true, ; optimize if-s and conditional expressions
53+
:comparisons true, ; optimize comparisons
54+
:evaluate true, ; evaluate constant expressions
55+
:booleans true, ; optimize boolean expressions
56+
:loops true, ; optimize loops
57+
:unused true, ; drop unused variables/functions
58+
:hoist_funs true, ; hoist function declarations
59+
:hoist_vars false, ; hoist variable declarations
60+
:if_return true, ; optimize if-s followed by return/continue
61+
:join_vars true, ; join var declarations
62+
:cascade true, ; try to cascade `right` into `left` in sequences
63+
:side_effects true, ; drop side-effect-free statements
64+
:warnings true, ; warn about potentially dangerous optimizations/code
65+
:global_defs {}, ; global definitions
66+
:mangle false
67+
}
68+
```
69+
70+
Future :
71+
72+
- support source maps (UglifyJS supports it already)
73+
- benchmark output sizes versus the google closure compiler when gzipped on a variety of projects

src/nha/boot_uglify.clj

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,48 +65,76 @@
6565
:id (string/replace (.getName file) #"\.cljs\.edn$" ""))))
6666

6767

68+
(def default-options {:sequences true, ; join consecutive statemets with the “comma operator”
69+
:properties true, ; optimize property access: a["foo"] → a.foo
70+
:dead_code true, ; discard unreachable code
71+
:drop_debugger true, ; discard “debugger” statements
72+
:unsafe false, ; some unsafe optimizations
73+
:conditionals true, ; optimize if-s and conditional expressions
74+
:comparisons true, ; optimize comparisons
75+
:evaluate true, ; evaluate constant expressions
76+
:booleans true, ; optimize boolean expressions
77+
:loops true, ; optimize loops
78+
:unused true, ; drop unused variables/functions
79+
:hoist_funs true, ; hoist function declarations
80+
:hoist_vars false, ; hoist variable declarations
81+
:if_return true, ; optimize if-s followed by return/continue
82+
:join_vars true, ; join var declarations
83+
:cascade true, ; try to cascade `right` into `left` in sequences
84+
:side_effects true, ; drop side-effect-free statements
85+
:warnings true, ; warn about potentially dangerous optimizations/code
86+
:global_defs {}, ; global definitions
87+
:mangle false ; mangle names
88+
})
89+
6890
(core/deftask uglify
6991
"Uglify JS code."
70-
[;o options OPTS {} "option map to pass to Uglify. See http://lisperator.net/uglifyjs/compress. Also, you can pass :mangle true to mangle names"
71-
]
92+
[o options OPTS {} "option map to pass to Uglify. See http://lisperator.net/uglifyjs/compress. Also, you can pass :mangle true to mangle names."]
93+
7294
(util/info "Uglifying JS...\n")
7395

74-
(let [pod (make-pod)
75-
tgt (core/tmp-dir!)]
96+
(println "OPTIONS ARE " options)
97+
98+
(let [options (merge default-options options)
99+
pod (make-pod)
100+
tgt (core/tmp-dir!)]
76101

77102
;; idea
78103
;; "intercept" files from the fileset, get cljs.edn files
79104
;; and use them (how) to find the final output file an minify it
80105
;; improvement : source maps (suported by UglifyJS2)
81106

107+
82108
(core/with-pre-wrap [fs]
83109
;;(util/info "Task files : " (seq (core/input-files fs)))
84110
;;(util/info "no more task files\n")
85-
(helpers/print-fileset fs)
111+
;;(helpers/print-fileset fs)
86112
(core/empty-dir! tgt)
87113
;; (println "Files :" (get-files fs)) ;; util/info does not work here ?
88-
;; (println "Intersting files " (find-mainfiles fs [".js"]))
89-
;; (println "Intersting files READABLE " (find-mainfiles fs [".js"]))
114+
;; (println "Interesting files " (find-mainfiles fs [".js"]))
115+
;; (println "Interesting files READABLE " (find-mainfiles fs [".js"]))
90116
(let [js-files (find-mainfiles fs [".js"])
91117
edn-files (find-mainfiles fs [".cljs.edn"])
92118
edn-content (map read-cljs-edn edn-files)
93119
;;out-paths (-> edn-files :compiler-options :asset-path )
94120
out-paths (map (comp :asset-path :compiler-options) edn-content)
95-
test-files (filter #(= "js/main.js" (:path %)) js-files)]
96-
;;(println "JS FILES _______________________________" (map :path js-files))
97-
;; (println "JS FILES _______________________________" test-files)
98-
;; (println "OUT_PATHS "out-paths)
99-
;; (println "END-FILES _______________________________" edn-files)
100-
;; (println "EDN CONTENT _______________________________" edn-content)
121+
test-files (filter #(= "js/main.js" (:path %)) js-files)
122+
files test-files]
123+
124+
;; if-let here ??
101125

102126
;; run the minification in a pod, in a temp target directory
103-
(doseq [f test-files :let [subf (copy f tgt)
127+
(doseq [f files :let [subf (copy f tgt)
104128
txt (slurp subf)
105129
path (core/tmp-path f)]]
106-
107-
(println "EVAL " (pod/with-call-in @pod (nha.boot-uglify.impl/minify-str ~txt ~{})))
108-
;;(spit subf (pod/with-call-in @pod (minify-str js-engine txt {})))
109-
))
130+
(let [minified (pod/with-call-in @pod (nha.boot-uglify.impl/minify-str ~txt ~{}))]
131+
(println "Size before : " (count txt))
132+
(println "Size after : " (count minified))
133+
(spit subf minified)))
134+
(-> fs
135+
(core/rm files)
136+
(core/add-resource tgt)
137+
core/commit!))
110138
fs)))
111139

112140

src/nha/boot_uglify/impl.clj

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,11 @@
114114

115115
(defn minify-str*
116116
"Minify a string"
117-
([^String s] (minify-str* s {} (create-uglify-engine)))
118-
([^String s opts] (minify-str* s opts (create-uglify-engine)))
119-
([^String s opts engine]
117+
([^String s] (minify-str* (create-uglify-engine) s {}))
118+
([engine ^String s] (minify-str* (create-uglify-engine) s {}))
119+
([engine ^String s opts]
120120
(let [js-opts (generate-string (dissoc opts :mangle))
121121
mangle (str (or (:mangle opts) false))
122-
123122
code (str "var BOOT_UGLIFY_CODE =\"" (escape-js s) "\";"
124123
"compress(BOOT_UGLIFY_CODE, " js-opts ", " mangle ");")]
125124
(eval-str engine code))))
@@ -135,21 +134,25 @@
135134
[^String s opts]
136135
;; js-engine cannot be passed as argument
137136
;; from boot through a reader (no reader macro for it)
138-
(let [res (minify-str* s opts js-engine)]
139-
(println "MINIFIED RESULT :")
140-
(println res)
137+
(let [res (minify-str* js-engine s opts)]
138+
;;(println "MINIFIED RESULT :")
139+
;;(println res)
141140
res))
142141

143142

144143
(comment
145144
;; Arities without the engine are used for REPLing
146145

146+
(def e (create-uglify-engine))
147+
147148
(escape-js "a = 'test'; // 'test' used here \n print(\"a is\", a); ")
148149
(escape-js "a = \"test\"; // \"test\" used here")
149150
(escape-js "a = `test`; // 'test' used here")
150151

151152
(minify-str* "a = 'test'; // 'test' used here")
152153
(minify-str* "var c = function myTest() {print('myTest'); return 123;}")
153-
(minify-str* "var unused= 456; /*remove me*/var c = function myTest() {print(\"myTest\"); return 123;} // a comment")
154+
(minify-str* "var unused = 456; /*remove me*/var c = function myTest() {print(\"myTest\"); return 123;} // a comment")
155+
156+
(minify-str* e "var unused = 456; /*remove me*/var c = function myTest() {print(\"myTest\"); return 123;} // a comment")
154157

155158
)

0 commit comments

Comments
 (0)