-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCokefile
122 lines (96 loc) · 3.33 KB
/
Cokefile
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
117
118
119
120
121
122
stylus = require \stylus
nib = do require \nib
time = require \./lib/sand
fs = require \fs
cjs = require \commonjs-everywhere
flatten = -> Array::concat.apply [], it # shallow flatten
join = -> flatten @@ .join \\n
cache = {}
processed = {}
entry-point = \src/c4.co
last-changed = entry-point
coco = time.as \Coco require(\coco)~compile
nephrite = time.as \Nephrite require(\nephrite)
styl = time.as \Stylus (it, filename) ->
stylus it, {filename, paths: ["#{__dirname}/style"]} .use nib .render!
js-to-ast = time.as 'JS->AST' require(\esprima)~parse
ast-to-js = time.as 'AST->JS' require(\escodegen)~generate
traverseDependenciesSync = time.as 'CJSify compile',
minus: [coco, nephrite, styl, js-to-ast],
cjs~traverseDependenciesSync
bundle = time.as 'CJSify bundle' cjs~bundle
handler = (compile) -> -> js-to-ast compile ...
cjs-options =
node: false
cache: cache
handlers:
\.co : handler (it, filename) ->
coco it.toString!, {+bare, filename}
\.ne : handler (it, filename) ->
src = nephrite it.toString!, filename
coco src, {+bare, filename}
\.styl : handler (it, filename) ->
"module.exports = '#{styl it.toString!, filename
.replace /'/g "\/'" .replace /\n/g '\\n'}'"
outfile = \c4.user.js
task \build 'build userscript' time.as \Total !->
try
time.restart-all!
metadata = slurp \metadata.txt
new-deps = traverseDependenciesSync last-changed, __dirname, cjs-options
processed[file] = new-deps[file] for file in new-deps
ast = bundle processed, entry-point, __dirname, cjs-options
# TODO something with sourcemap, when fire(fox|bug) gets support
gen = ast-to-js ast
spit do
outfile
join do
metadata
"(function(){"
'"use strict";'
# XXX hack to invalidate cached data in html5 storage when code changes
"var c4_COMPILATION_VERSION = #{Math.random!};"
gen
"}).call(this)"
time.end do
"JS->AST"
"Coco"
"Nephrite"
"Stylus"
"CJSify compile"
"CJSify bundle"
"AST->JS"
"Total"
say "#{new Date}: compiled script to #{outfile}"
catch
console.error e
console.error e.stack
debounce-leading = (delay, fn) ->
var timeout
reset = !-> timeout := null
-> unless timeout
fn ...
timeout := setTimeout reset, delay
task \watch 'watch for changes and rebuild automatically' ->
invoke \build
require(\gaze) do
<[ metadata.txt src/**/* style/**/* templates/**/* ]>
debounce-delay: 2000ms # yeah, this doesn't work
!->
say "watching for changes..."
@on \all debounce-leading 1000ms !(event, filepath) ->
# XXX for some reason I'm getting extraneous 'added' events on
# directories, so guard against those
if fs.statSync filepath .isFile!
last-changed := filepath
else
# no actual changes we can use with the caching system
return
say "#event event detected on #filepath. rebuilding..."
# XXX styl's internal @include mechanism means only c4.styl is
# invalidated if it changes, so hack around this
if /styl/.test filepath
last-changed := "#__dirname/style/c4.styl"
Object.keys cache .filter /styl/~test .forEach !->
delete cache[it]
invoke \build