forked from madrobby/zepto
-
Notifications
You must be signed in to change notification settings - Fork 2
/
make
executable file
·95 lines (75 loc) · 2.54 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
#!/usr/bin/env coffee
require 'shelljs/make'
fs = require 'fs'
version = '1.1.2'
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.coffee '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 detect event ajax form fx assets data selector ie').split(' ')
module_files = ( "src/#{module}.js" for module in modules )
intro = "/* Zepto #{describe_version()} - #{modules.join(' ')} - zeptojs.com/license */\n"
dist = intro + cat(module_files).replace(/^\/[\/*].*$/mg, '').replace(/\n{3,}/g, "\n\n")
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)}"
## 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"
describe_version = ->
desc = exec "git --git-dir='#{root + '.git'}' describe --tags HEAD", silent: true
if desc.code is 0 then desc.output.replace(/\s+$/, '') else version
minify = (source_code) ->
uglify = require('uglify-js')
ast = uglify.parser.parse(source_code)
ast = uglify.uglify.ast_mangle(ast)
ast = uglify.uglify.ast_squeeze(ast)
uglify.uglify.gen_code(ast)