FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
srcbuild/dev/bundle.ls at master · plotdb/srcbuild · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
plotdb
/
srcbuild
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
1
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
srcbuild
/
dev
/
bundle.ls
Copy path
More file actions
More file actions
Latest commit
History
History
History
263 lines (240 loc) · 8.76 KB
Breadcrumbs
srcbuild
/
dev
/
bundle.ls
Copy path
File metadata and controls
263 lines (240 loc) · 8.76 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
require
! <[
uglify
-
js
uglifycss
]>
fs
=
require
"fs-extra"
# spec
spec
= (
o
= {}) ->
@
mgr
=
o
.manager
@
o
=
JSON
.parse
JSON
.stringify
o
@ <<< @
o
{
type
,
name
,
dirty
}
@
codesrc
=
new
Set(@
o
.codesrc
or
[])
@
specsrc
=
new
Set(@
o
.specsrc
or
[])
@
spec
.prototype =
Object
.create(
Object
.prototype) <<<
do
to
-
object
: ->
{} <<< {
codesrc
:
Array
.from(@
codesrc
)
specsrc
:
Array
.from(@
specsrc
)
} <<< @{
type
,
name
,
dirty
}
cache
-
fn
: -> @
mgr
.get-
cache
-
name
@
sync
-
cache
: ->
fn
= @
cache
-
fn
!
fs
.ensure-
dir
path
.dirname(
fn
)
.then ~>
fs
.write-
file
fn
, @
to
-
object
!
specmgr
= (
o
= {}) ->
@
cachedir
=
o
.cachedir
@
evthdr
= {}
@
_
= {}
# codesrc and specsrc are hashes for with files that should be watched in this builder.
# - codesrc for set of files that are used to generate bundle files (their updates trigger rebuild)
# - specsrc for set of files that use (and thus define) bundles. (their updates change bundle spec)
# the object stored in thes hashes are Set object containing the spec key linked with these files.
@
codesrc
= {}
@
specsrc
= {}
# keep track of keys of spec been updated. batch write back cache by specmgr to reduce file access.
@
_dirty
=
new
Set
!
@
specmgr
.prototype =
Object
.create(
Object
.prototype) <<<
do
on
: (
n
,
cb
) -> (
if
Array
.isArray(
n
) =>
n
else
[
n
]).map (
n
) ~> @
evthdr
.[][
n
].push
cb
fire
: (
n
, ...v) ->
for
cb
in
(@
evthdr
[
n
]
or
[]) =>
cb
.apply @,
v
key
: (
o
= {}) ->
if
typeof
(
o
) == \
object
=>
"#{o.type}/#{o.name}"
else
o
get
-
cache
-
name
: (
spec
) ->
path
.join(@
cachedir
,
spec
.type,
"#{spec.name}.dep"
# we should write back deps and rebuild if dirty and is not delete (spec still exists)
set
-
dirty
: (
o
= {}) ->
@
_dirty
.add @key(
o
)
@
clear
-
dirty
!
clear
-
dirty
:
debounce
1000
, ->
specs
=
Array
.from(@
_dirty
)
.map (
k
) ~> @
get
k
.filter ->
it
@
fire
\
build
-
by
-
spec
,
specs
specs
.map (
s
) ->
s
.sync-
cache
!
@
_dirty
.clear!
add
: (
o
={},
opt
= {}) ->
k
= @
key
o
if
@
_
[
k
]
and
!
opt
.force =>
return
that
@
_
[
k
] =
s
=
new
spec
o
s
.codesrc.
for
-each (
n
) ~> @
link
codesrc
:
n
,
spec
:
r
s
.specsrc.
for
-each (
n
) ~> @
link
specsrc
:
n
,
spec
:
r
if
!
opt
.init => @
set
-
dirty
s
s
set
: (
o
= {},
opt
= {}) -> @
add
o
, ({
force
:
true
} <<<
opt
)
has
-
code
: (
f
) -> @
codesrc
.has
f
touch
-
code
: (
f
) ->
f
=
if
Array
.isArray(
f
) =>
f
else
[
f
]
keys
=
new
Set
!
f
.map ~>
if
!@
codesrc
[
f
] =>
return
Array
.from(@
codesrc
[
f
]).
for
-each (
k
) ~>
keys
.add
k
@
fire
\
build
-
by
-
spec
,
Array
.from(
keys
).map((
k
) ~> @
get
k
)
update
: (
o
= {}) ->
k
= @
key
o
dirty
=
false
if
!(
s
= @
_
[
k
]) =>
@
add
o
return
true
if
Array
.from(
s
.codesrc).join(
','
) == (
o
.codesrc
or
[]).join(
','
) =>
dirty
=
true
s
.codesrc =
new
Set(
o
.codesrc
or
[])
(
if
Array
.isArray(
o
.specsrc) =>
o
.specsrc
else
[
o
.specsrc]).
for
-each (
n
) ->
if
!
s
.specsrc.has
n
=>
dirty
:=
true
s
.specsrc.add
n
if
dirty
=> @
set
-
dirty
s
return
dirty
get
: (
o
={}) -> @
_
[@
key
o
]
delete
: (
o
= {}) ->
r
= @
_
[@
key
o
]
r
.codesrc.
for
-each (
n
) ~> @
unlink
codesrc
:
n
,
spec
:
r
r
.specsrc.
for
-each (
n
) ~> @
unlink
specsrc
:
n
,
spec
:
r
@
set
-
dirty
o
link
: (
o
= {}) ->
f
=
if
o
.codesrc => \
codesrc
else
\
specsrc
s
=
if
@[
f
][
o
[
f
]] =>
that
else
@[
f
][
o
[
f
]] =
new
Set
!
if
!
s
=>
return
s
.add(@
key
o
.spec)
unlink
: (
o
= {}) ->
f
=
if
o
.codesrc => \
codesrc
else
\
specsrc
if
!(
s
= @[
f
][
o
[
f
]]) =>
return
s
.remove @
key
o
.spec
if
s
.size =>
return
delete
@[
f
][
o
[
f
]]
del
-
specsrc
: (
n
) ->
if
!(
s
= @
specsrc
[
n
]) =>
return
s
.
for
-each (
k
) ~>
if
!(
spec
= @
get
k
) =>
return
spec
.unlink
specsrc
:
n
# spec deletion trigger a cache writeback,
# but should not trigger bundle rebuild. how to identify this?
@
set
-
dirty
k
delete
@
specsrc
[
n
]
build
= (
o
={}) ->
@
mgr
=
opt
.manager
# this is the optional bundle specs provided directly through constructor
@
defcfg
=
opt
.config
or
null
# this is the directory storing dependency metadata cache
@
cachedir
=
path
.join(
opt
.base,
'.bundle-dep'
)
# this file keeps optional bundle specs expliticly defines by developer.
@
cfgfn
=
opt
.config-
file
or
null
# this helps us converting files in cfgfn to the correct path
# since cfgfn may locate in any dir,
# the directory relation between cfgfn and the code source files is kinda undefined
# so we use reldir to explicitly define it.
@
reldir
=
if
typeof
(
opt
.relative-
path
) == \
string
=>
opt
.relative-
path
else
if
opt
.relative-
path
and
@
cfgfn
=>
path
.dirname(@
cfgfn
)
else
process
.cwd!
@init({
srcdir
:
'static'
,
desdir
:
'static'
} <<<
opt
)
@
build
.prototype =
Object
.create(
Object
.prototype) <<<
do
get
-
path
: (
f
) ->
if
typeof
(
f
) == \
string
=>
return
f
if
@
mgr
=>
return
@
mgr
.get-url(
f
)
version
=
f
.version
or
\
main
path
=
if
f
.path =>
f
.path
else
if
f
.type == \
css
=> \
index
.min.css
else
if
f
.type == \
block
=> \
index
.html
else
'index.min.js'
# path are defined in frontend context. thus, it will be sth like "/js/site.js" etc
# related to @desdir (e.g., `static` )
return
path
.join(@
desdir
,
"assets/lib/#{f.name}/#version/#path"
)
reload
: ->
@
reset
!
@
load
-
cfg
!
@
load
-
caches
!
reset
: ->
# specmgr manages lifecycle of specs
@
specmgr
=
new
specmgr
cachedir
: @
cachedir
@
specmgr
.on \
build
-
by
-
spec
, (
specs
) ~>
specs
.
for
-each (
spec
) ~> @
build
-
by
-
spec
spec
load
-
cfg
: ->
cfgs
= [[
''
,@
defcfg
]]
if
(@
cfgfn
and
fs
.exists-sync(@
cfgfn
)) =>
try
cfg
=
JSON
.parse
fs
.read-
file
-sync(@
cfgfn
).toString!
catch
e
@
log
.error
"parse error of config file #{@cfgfn}"
.red
cfg
= {}
cfgs
.push [@
cfgfn
,
cfg
]
for
[
fn
,
cfg
]
in
cfgs
=>
for
type
of
cfg
=>
for
name
,
list
of
cfg
[
type
] =>
# this should be the only place we need to join `reldir` with a file name.
list
=
list
.map (
n
) ~>
if
typeof
(
n
) == \
string
=>
path
.join(@
reldir
,
n
)
else
@
get
-
path
n
@
specmgr
.set {
type
,
name
,
codesrc
:
list
,
specsrc
: [
fn
] }, {
init
:
true
}
load
-
caches
: ->
if
!
fs
.exists-sync(@
cachedir
) =>
return
traverse
= (
dir
) ->
files
=
fs
.readdir-
sync
dir
.map (
n
) ->
path
.join(
dir
,
n
)
ret
= []
for
file
in
files
=>
if
fs
.
is
-
directory
file
=>
ret
++= traverse(
file
)
if
/\.dep$/.exec(
file
) =>
ret
.push
file
return
ret
files
=
traverse
@
cachedir
files
.
for
-each (
n
) ~>
try
json
=
JSON
.parse(
fs
.read-
file
-
sync
n
.toString!)
catch
e
@
log
.error
"parse error of cache file #n"
.red
@
specmgr
.set
json
, {
init
:
true
}
del
-
specsrc
: (
n
) ->
specmgr
.del-
specsrc
n
add
-
spec
: (
opts
= []) ->
opts
= (
if
Array
.isArray(
opts
) =>
opts
else
[
opts
]).filter(->
it
)
opts
.map (
o
) ~>
files
=
o
.[]
files
.map (
f
) ~> @
get
-
path
f
@
specmgr
.update({} <<<
o
{
name
,
type
} <<< {
codesrc
:
files
,
specsrc
: [
o
.src]})
get
-
dependencies
: (
file
) ->
return
[]
is
-
supported
: (
file
) ->
return
specmgr
.has-
code
file
purge
: (
files
) -> @
build
files
build
: (
files
,
opt
) ->
force
=
if
typeof
(
opt
) == \
boolean
=>
opt
else
false
if
!
opt
? =>
opt
= {}
if
files
.filter(~>
it
.file == @
cfgfn
).length =>
return
@
reload
!
@
specmgr
.touch-
code
files
des
-
path
: ({
name
,
type
}) ->
desdir
=
path
.join(@
desdir
, \
assets
, \
bundle
)
return
desdir
:
desdir
des
:
path
.join(
desdir
,
"#name.#type"
)
des
-
min
:
path
.join(
desdir
,
"#name.min.#type"
)
build
-
by
-
spec
: (
spec
) ->
<~
Promise
.resolve!
then
_
{
name
,
type
} =
spec
t1
=
Date
.now!
srcs
=
Array
.from
spec
.codesrc
{
desdir
,
des
,
des
-
min
} = @
des
-
path
{
name
,
type
}
fs
.ensure-
dir
desdir
.then ~>
ps
=
srcs
.map (
f
) ->
fs
.read-
file
f
.
catch
->
""
.then (
b
) ->
fs
.read-
file
f
.
catch
->
""
.then (
bm
) ->
{
name
:
f
,
code
:
b
.toString!,
code
-
min
:
bm
.toString!}
Promise
.all
ps
.then (
ret
) ~>
normal
=
ret
.map(->
it
.code
or
it
.code-
min
).join(
''
)
minified
=
ret
.map (
o
) ->
if
o
.code-
min
=>
return
o
.code-
min
if
!
o
.code =>
return
""
return
if
type
== \
js
=>
uglify
-
js
.minify(
o
.code).code
else
if
type
== \
css
=>
uglifycss
.processString(
o
.code,
uglyComments
:
true
)
else
o
.code
.join(
''
)
Promise
.all [
fs
.write-file(
des
,
normal
)
fs
.write-file(
des
-
min
,
minified
)
]
.then ~>
ret
=
do
type
:
type
,
name
:
name
elapsed
:
Date
.now! -
t1
size
:
fs
.stat-sync(
des
).size
size
-
min
:
fs
.stat-sync(
des
-
min
).size
{
size
,
size
-
min
,
elapsed
} =
ret
@
log
.info
"bundle #des ( #size bytes / #{elapsed}ms )"
@
log
.info
"bundle #des-min ( #size-min bytes / #{elapsed}ms )"
ret
.
catch
(
e
) ~>
@
log
.error
"#des failed: "
.red
@
log
.error
e
.message.toString!
Back
|
FazBrowse Home
|
New Git URL