-
Notifications
You must be signed in to change notification settings - Fork 3
/
make
116 lines (93 loc) · 3.02 KB
/
make
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env coffee
require 'shelljs/make'
fs = require 'fs'
zepto_js = 'dist/zepto.js'
zepto_min = 'dist/zepto.min.js'
zepto_gz = 'dist/zepto.min.gz'
port = 3999
root = __dirname + '/'
target.all = ->
target[zepto_js]()
target.test()
## TASKS ##
target.test = ->
test_app = require './test/server'
server = test_app.listen port
exec "phantomjs --disk-cache=true test/runner.js 'http://localhost:#{port}/'", (code) ->
server.close -> exit(code)
target[zepto_js] = ->
target.build() unless test('-e', zepto_js)
target[zepto_min] = ->
target.minify() if stale(zepto_min, zepto_js)
target[zepto_gz] = ->
target.compress() if stale(zepto_gz, zepto_min)
target.dist = ->
target.build()
target.minify()
target.compress()
target.build = ->
cd __dirname
mkdir '-p', 'dist'
modules = (env['MODULES'] || 'zepto event ajax form ie').split(' ')
module_files = ( "src/#{module}.js" for module in modules )
intro = "/* Zepto #{describe_version()} - #{modules.join(' ')} - zeptojs.com/license */\n"
dist = cat(module_files).replace(/^\/[\/*].*$/mg, '').replace(/\n{3,}/g, "\n\n")
dist = cat('src/amd_layout.js').replace(/YIELD/, -> dist.trim()) unless env['NOAMD']
(intro + dist).to(zepto_js)
report_size(zepto_js)
target.minify = ->
target.build() unless test('-e', zepto_js)
zepto_code = cat(zepto_js)
intro = zepto_code.slice(0, zepto_code.indexOf("\n") + 1)
(intro + minify(zepto_code)).to(zepto_min)
report_size(zepto_min)
target.compress = ->
gzip = require('zlib').createGzip()
inp = fs.createReadStream(zepto_min)
out = fs.createWriteStream(zepto_gz)
inp.pipe(gzip).pipe(out)
out.on 'close', ->
report_size(zepto_gz)
factor = fsize(zepto_js) / fsize(zepto_gz)
echo "compression factor: #{format_number(factor)}"
target.publish = ->
tag = 'v' + package_version()
if git_version() == tag
rm '-f', zepto_js
env['MODULES'] = env['NOAMD'] = ''
target.dist()
res = exec 'npm publish'
exit res.code
else
console.error 'error: latest commit should be tagged with ' + tag
exit 1
## HELPERS ##
stale = (file, source) ->
target[source]()
!test('-e', file) || mtime(file) < mtime(source)
mtime = (file) ->
fs.statSync(file).mtime.getTime()
fsize = (file) ->
fs.statSync(file).size
format_number = (size, precision = 1) ->
factor = Math.pow(10, precision)
decimal = Math.round(size * factor) % factor
parseInt(size) + "." + decimal
report_size = (file) ->
echo "#{file}: #{format_number(fsize(file) / 1024)} KiB"
package_version = ->
JSON.parse(cat('package.json')).version
git_version = ->
desc = exec "git --git-dir='#{root + '.git'}' describe --tags HEAD", silent: true
desc.output.replace(/\s+$/, '') if desc.code is 0
describe_version = ->
git_version() || package_version()
minify = (source_code) ->
uglify = require('uglify-js')
compressor = uglify.Compressor()
ast = uglify.parse(source_code)
ast.figure_out_scope()
ast.compute_char_frequency()
ast.mangle_names()
ast = ast.transform(compressor)
return ast.print_to_string()