-
Notifications
You must be signed in to change notification settings - Fork 77
/
cmd-build.js
212 lines (189 loc) · 6.59 KB
/
cmd-build.js
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
var ansi = require('ansi-escape-sequences')
var pinoColada = require('pino-colada')
var async = require('async-collection')
var stdout = require('stdout-stream')
var pumpify = require('pumpify')
var iltorb = require('iltorb')
var mkdirp = require('mkdirp')
var path = require('path')
var zlib = require('zlib')
var fs = require('fs')
var bankai = require('../')
var utils = require('./utils')
module.exports = build
function build (entry, opts) {
var basedir = utils.dirname(entry)
var outdir = path.join(basedir, 'dist')
mkdirp(outdir, function (err) {
if (err) return console.error(err)
var bankaiOpts = {
logStream: pumpify(pinoColada(), stdout),
assert: false,
watch: false
}
var compiler = bankai(entry, bankaiOpts)
var log = compiler.log
log.info('Compiling & compressing files\n')
created(basedir, outdir + '/')
compiler.on('error', function (topic, sub, err) {
if (err.pretty) log.error(err.pretty)
else {
log.error(`${topic}:${sub} ${err.message}\n${err.stack}`)
}
})
compiler.on('ssr', function (result) {
if (!result.success) log.warn('Server Side Rendering Skipped due to error: ' + result.error.message)
})
compiler.on('change', function (nodeName, edgeName, nodeState) {
var stepName = nodeName + ':' + edgeName
if (stepName === 'assets:list') writeAssets('assets')
if (stepName === 'documents:list') writeDocuments('documents')
if (stepName === 'service-worker:bundle') {
var filename = compiler.graph.metadata.serviceWorker
compiler.serviceWorker(writeSimple(filename, 'service-worker'))
}
})
compiler.manifest(writeSimple('manifest.json', 'manifest'))
compiler.style(writeSingle('bundle.css', 'style'))
compiler.scripts('bundle.js', writeSingle('bundle.js', 'scripts'))
function writeAssets (step) {
var node = compiler.graph.data[step].list
var list = String(node.buffer).split(',')
async.mapLimit(list, 3, iterator, function (err) {
if (err) return log.error(err)
completed(step)
})
// TODO: It'd be sick if we could optimize our paths to assets,
// add etags to our tags and put them in the right dir.
function iterator (filename, done) {
compiler[step](filename, function (err, node) {
if (err) return done(err)
filename = path.join(outdir, filename)
var dirname = utils.dirname(filename)
if (dirname === filename) {
writePlain(filename, node.buffer, done)
} else {
mkdirp(dirname, function (err) {
if (err) return done(err)
writePlain(filename, node.buffer, done)
})
}
})
}
}
function writeDocuments (step) {
var write = writeCompressed
var node = compiler.graph.data[step].list
var list = String(node.buffer).split(',')
async.mapLimit(list, 3, iterator, function (err) {
if (err) return log.error(err)
completed(step)
})
// TODO: It'd be sick if we could optimize our paths to assets,
// add etags to our tags and put them in the right dir.
function iterator (filename, done) {
// if (filename === '/') filename = 'index.html'
// skip over all partial files
if (/[:*]/.test(filename)) return done()
compiler[step](filename, function (err, node) {
if (err) return done(err)
filename = path.join(outdir, filename, 'index.html')
var dirname = utils.dirname(filename)
if (dirname === filename) {
write(filename, node.buffer, done)
} else {
mkdirp(dirname, function (err) {
if (err) return done(err)
write(filename, node.buffer, done)
})
}
})
}
}
function writeSimple (filename, type) {
return function (err, node) {
if (err) return log.error(err)
filename = path.join(outdir, filename)
writeCompressed(filename, node.buffer, function (err) {
if (err) return log.error(err)
completed(type)
})
}
}
function writeSingle (filename, type) {
return function (err, node) {
if (err) return log.error(err)
var dirname = path.join(outdir, node.hash.toString('hex').slice(0, 16))
mkdirp(dirname, function (err) {
if (err) return log.error(err)
filename = path.join(dirname, filename)
writeCompressed(filename, node.buffer, function (err) {
if (err) return log.error(err)
completed(type)
})
})
}
}
function created (basedir, filename) {
var relative = path.relative(process.cwd(), filename)
log.info(`${clr('created', 'magenta')}: ${relative}`)
}
function completed (step) {
log.debug(`${clr('completed', 'green')}: ${step}`)
}
function writeCompressed (filename, buffer, done) {
async.series([
function raw (done) {
fs.writeFile(filename, buffer, function (err) {
if (err) return done(err)
created(basedir, filename)
done()
})
},
function gzip (done) {
zlib.gzip(buffer, function (err, compressed) {
if (err) return done(err)
var outfile = filename + '.gz'
fs.writeFile(outfile, compressed, function (err) {
if (err) return done(err)
created(basedir, outfile)
done()
})
})
},
function deflate (done) {
zlib.deflate(buffer, function (err, compressed) {
if (err) return done(err)
var outfile = filename + '.deflate'
fs.writeFile(outfile, compressed, function (err) {
if (err) return done(err)
created(basedir, outfile)
done()
})
})
},
function brotli (done) {
iltorb.compress(buffer, function (err, compressed) {
if (err) return done(err)
var outfile = filename + '.br'
fs.writeFile(outfile, compressed, function (err) {
if (err) return done(err)
created(basedir, outfile)
done()
})
})
}
], done)
}
function writePlain (filename, buffer, done) {
fs.writeFile(filename, buffer, function (err) {
if (err) return done(err)
created(basedir, filename)
done()
})
}
})
}
function clr (text, color) {
return process.stdout.isTTY ? ansi.format(text, color) : text
}