-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
83 lines (68 loc) · 1.7 KB
/
index.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
'use strict';
const through = require('through2');
const gasEntryGenerator = require('gas-entry-generator');
module.exports = function (b, options) {
let cache = {};
const stubsCache = {};
options = Object.assign({comment: true}, options);
b.on('reset', collect);
collect();
function collect() {
cache = {};
b.pipeline.get('syntax').push(through.obj(function (row, enc, next) {
const file = row.expose ? b._expose[row.id] : row.file;
cache[file] = {
source: row.source
};
this.push(row);
next();
}));
}
b.on('transform', (tr, mfile) => {
tr.on('file', dep => {
invalidate(mfile);
});
});
b.on('update', files => {
files.forEach(file => {
invalidate(file);
});
});
function invalidate(file) {
delete stubsCache[file];
}
function generateEntryPoint() {
let entrypoints = '';
for (const file in cache) {
if ({}.hasOwnProperty.call(cache, file)) {
let stub = stubsCache[file];
if (!stub) {
stub = gasEntryGenerator(cache[file].source, options);
stubsCache[file] = stub;
}
entrypoints += stub;
}
}
return 'var global = this;\n' + entrypoints;
}
const createStream = function () {
let firstChunk = true;
const stream = through.obj(function (buf, enc, next) {
if (firstChunk) {
this.push(Buffer.from(generateEntryPoint()));
firstChunk = false;
}
this.push(buf);
next();
});
stream.label = 'gasentrypoint';
return stream;
};
function setupPipeline() {
b.pipeline.get('wrap').push(createStream());
}
setupPipeline();
b.on('reset', () => {
setupPipeline();
});
};