-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompiler.js
282 lines (242 loc) · 7.7 KB
/
compiler.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
const { Target } = require('./target')
const { Module } = require('module')
const { Pool } = require('nanoresource-pool')
const messages = require('./messages')
const varint = require('varint')
const rimraf = require('rimraf')
const Batch = require('batch')
const debug = require('debug')('tiny-module-compiler:compiler')
const magic = require('./magic')
const ready = require('nanoresource-ready')
const path = require('path')
const raf = require('random-access-file')
const ncc = require('@zeit/ncc')
const vm = require('vm')
// quick util
const noopback = (...args) => void args.pop()(null)
const errback = (p, cb) => void p.then((r) => cb(null, r), cb).catch(cb)
const noop = () => void 0
/**
* Default extension for compiler target output file names.
* @private
*/
const DEFAULT_OUTPUT_EXTNAME = '.out'
/**
* Default file permissions for assets.
* @private
*/
const DEFAULT_ASSET_PERMISSIONS = 438 // 0666
/**
* The `Compiler` class represents a container of compile targets
* that can be compiled into a single binary file containing
* v8 cache data and header information about the compiled output.
* @public
* @class
* @extends Pool
*/
class Compiler extends Pool {
/**
* `Compiler` class constructor.
* @param {?(Object)} opts
* @param {?(String)} opts.cwd
*/
constructor(opts) {
if (!opts || 'object' !== typeof opts) {
opts = {}
}
super(Target)
this.cwd = opts.cwd || process.cwd()
}
/**
* All opened targets in the compiler.
* @accessor
*/
get targets() {
return this.list()
}
/**
* Waits for compiler to be ready and calls `callback()` upon
* success.
* @param {Function} ready
*/
ready(callback) {
ready(this, callback)
}
/**
* Creates and returns a new compile target that is added to
* compiler pool. The target will be compiled when `compiler.compile()`
* is called.
* @param {String} filename
* @param {?(Object)} opts
* @param {?(String)} opts.output
* @param {?(Object)} opts.storage
* @param {?(Boolean)} [opts.autoOpen = true]
* @param {?(Function)} callback
* @return {Target}
*/
target(filename, opts, callback) {
if ('function' === typeof opts) {
callback = opts
opts = {}
}
if (!opts || 'object' !== typeof opts) {
opts = {}
}
if ('function' !== typeof callback) {
callback = noop
}
const target = Object.assign(this.resource(filename, opts), {
output: opts.output || (filename + DEFAULT_OUTPUT_EXTNAME)
})
if (false !== opts.autoOpen) {
target.open((err) => {
callback(err, target)
})
}
return target
}
/**
* Compiles all pending compile targets calling
* `callback(err, objects, assets)` upon success or error. Callback
* will be given a `Map` of compiled objects and a `Map` of extracted
* assets that should live with the compiled objects on the file system.
* @param {?(Object)} opts
* @param {?(Boolean)} opts.map
* @param {?(Boolean)} opts.cache
* @param {?(Boolean)} opts.debug
* @param {?(Boolean)} opts.quiet
* @param {?(Array<String>)} opts.externals
* @param {Function} callback
*/
compile(opts, callback) {
if ('function' === typeof opts) {
callback = opts
opts = {}
}
// istanbul ignore next
if (!opts || 'object' !== typeof opts) {
opts = {}
}
const { targets } = this
const objects = new Map()
const assets = new Map()
const writes = new Batch()
const batch = new Batch()
try {
for (const target of targets) {
const { filename } = target
const extname = path.extname(filename)
const basename = path.basename(filename)
const targetName = path.resolve(target.output)
.replace(path.join(path.resolve(this.cwd), path.sep), '')
const shouldOptimize = (opts.minify || opts.optimize)
batch.push((next) => {
debug('compile', filename)
// assume target is on the file system for now
errback(ncc(filename, {
sourceMap: opts.map || false,
externals: opts.externals || [],
cache: opts.cache || false,
v8cache: false, // we'll do this manually
minify: shouldOptimize,
quiet: false !== opts.quiet
}), (err, result) => {
// istanbul ignore next
if (err) { return next(err) }
for (const name in result.assets) {
assets.set(
path.dirname(targetName) + path.sep + name,
result.assets[name])
}
if (opts.debug) {
assets.set(targetName + '.debug.compiled.js', {
source: Buffer.from(result.code),
permissions: DEFAULT_ASSET_PERMISSIONS
})
}
if (result.map) {
assets.set(targetName + '.map', {
source: Buffer.from(result.map),
permissions: DEFAULT_ASSET_PERMISSIONS
})
}
const src = Buffer.from(Module.wrap(result.code))
const script = new vm.Script(src.toString(), {
produceCachedData: true,
filename: basename
})
// istanbul ignore next
const cache = 'function' === typeof script.createCachedData
? script.createCachedData()
: script.cachedData
// istanbul ignore next
if (!cache) {
return next(new Error('Unable to capture compiled cached data.'))
}
// read `uint32_t` from the "source hash" part of the serialized cache data:
// borrowed from: https://github.com/OsamaAbbas/bytenode/blob/master/index.js#L56
// read from: https://github.com/v8/v8/blob/master/src/snapshot/code-serializer.h#L93
const sourceHash = cache.slice(8, 12).reduce((y, x, i) => y += x * Math.pow(256, i), 0)
const versions = messages.Versions.encode(process.versions)
const options = messages.Options.encode({
filename: path.basename(filename),
optimized: shouldOptimize,
externals: opts.externals,
assets: Array.from(assets.keys()),
})
objects.set(targetName, Buffer.concat([
// header (4 bytes)
magic.TMCO,
// versions
Buffer.from(varint.encode(versions.length)),
versions,
// options
Buffer.from(varint.encode(options.length)),
options,
// source hash
Buffer.from(varint.encode(sourceHash)),
// cache
Buffer.from(varint.encode(cache.length)),
cache
]))
target.close(next)
})
})
}
} catch (err) {
return callback(err)
}
batch.end((err) => {
// istanbul ignore next
if (err) { return callback(err) }
for (let [ filename, object ] of objects) {
filename = path.resolve(this.cwd, filename)
writes.push((next) => {
const rm = opts.storage ? noopback : rimraf
rm(filename, (err) => {
// istanbul ignore next
if (err) { return callback(err) }
const output = 'function' === typeof opts.storage
? opts.storage(filename)
: raf(filename)
output.write(0, object, (err) => {
next(err)
if ('function' !== typeof opts.storage) {
output.close()
}
})
})
})
}
writes.end((err) => {
callback(err, objects, assets)
})
})
}
}
/**
* Module exports.
*/
module.exports = {
Compiler
}