/
BrowserifyCache.js
222 lines (180 loc) · 5.92 KB
/
BrowserifyCache.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
var fs = require('fs');
var assert = require('assert');
var through = require('through2');
var assign = require('xtend/mutable');
var assertExists = require('./assertExists');
var proxyEvent = require('./proxyEvent');
var Cache = require('./Cache');
var invalidateCache = require('./invalidateCache');
var invalidateDependentFiles = require('./invalidateDependentFiles');
function BrowserifyCache(b, opts) {
assertExists(b);
opts = opts || {};
if (BrowserifyCache.getCache(b)) return b; // already attached
// certain opts must have been set when browserify instance was created
assert(b._options.cache, "required browserify 'cache' opt not set");
// load cache from file specified by cacheFile opt
var cacheFile = opts.cacheFile || opts.cachefile || b._options && b._options.cacheFile || null;
var cacheData = loadCacheData(b, cacheFile);
// b._options.cache is a shared object into which loaded module cache is merged.
// it will be reused for each build, and mutated when the cache is invalidated.
assign(b._options.cache, cacheData.modules);
cacheData.modules = b._options.cache;
var cache = Cache(cacheData);
BrowserifyCache.setCache(b, cache);
attachCacheHooksToPipeline(b);
attachCacheDiscoveryHandlers(b);
attachCachePersistHandler(b, cacheFile);
return b;
}
BrowserifyCache.args = {
cache: {},
packageCache: {},
};
BrowserifyCache.getCache = function(b) {
return b.__cacheObjects;
};
BrowserifyCache.setCache = function(b, cache) {
b.__cacheObjects = cache;
};
// keep track of deps which are pending for the purpose of writing cache file
// (eg. being transformed)
function addCacheBlocker(b) {
if (b.__cacheBlockerCount == null) {
b.__cacheBlockerCount = 0;
}
b.__cacheBlockerCount++;
}
function removeCacheBlocker(b) {
assert(b.__cacheBlockerCount >= 1);
b.__cacheBlockerCount--;
if (b.__cacheBlockerCount === 0) {
b.emit('_cacheReadyToWrite');
}
}
function attachCacheHooksToPipeline(b) {
var prevBundle = b.bundle;
b.bundle = function(cb) {
var outputStream = through.obj();
invalidateCacheBeforeBundling(b, function(err) {
if (err) return outputStream.emit('error', err);
var bundleStream = prevBundle.call(b, cb);
proxyEvent(bundleStream, outputStream, 'file');
proxyEvent(bundleStream, outputStream, 'package');
proxyEvent(bundleStream, outputStream, 'transform');
proxyEvent(bundleStream, outputStream, 'error');
bundleStream.pipe(outputStream);
});
return outputStream;
};
}
function invalidateCacheBeforeBundling(b, done) {
var cache = BrowserifyCache.getCache(b);
invalidateCache(cache.mtimes, cache.modules, function(err, invalidated, deleted) {
invalidateDependentFiles(cache, [].concat(invalidated, deleted), function(err) {
b.emit('changedDeps', invalidated, deleted);
done(err, invalidated);
});
});
}
function attachCacheDiscoveryHandlers(b) {
// based on how watchify adds deps to cache
function insertDepCollector() {
b.pipeline.get('deps').push(through.obj(function(row, enc, next) {
var file = row.expose ? b._expose[row.id] : row.file;
var dep = {
file: file,
source: row.source,
deps: assign({}, row.deps),
};
addCacheBlocker(b);
updateCacheOnDep(b, dep, function(err) {
if (err) b.emit('error', err);
removeCacheBlocker(b);
});
this.push(row);
next();
}));
}
b.on('reset', insertDepCollector);
insertDepCollector();
b.on('transform', function(transformStream, moduleFile) {
transformStream.on('file', function(dependentFile) {
addCacheBlocker(b);
updateCacheOnTransformFile(b, moduleFile, dependentFile, function(err) {
if (err) b.emit('error', err);
removeCacheBlocker(b);
});
});
});
}
function updateCacheOnDep(b, dep, done) {
var cache = BrowserifyCache.getCache(b);
var file = dep.file || dep.id;
if (typeof file === 'string') {
if (dep.source != null) {
cache.modules[file] = dep;
if (!cache.mtimes[file])
return updateMtime(cache.mtimes, file, done);
} else {
console.warn('missing source for dep', file);
}
} else {
console.warn('got dep missing file or string id', file);
}
done();
}
function updateCacheOnTransformFile(b, moduleFile, dependentFile, done) {
var cache = BrowserifyCache.getCache(b);
if (cache.dependentFiles[dependentFile] == null) {
cache.dependentFiles[dependentFile] = {};
}
cache.dependentFiles[dependentFile][moduleFile] = true;
if (!cache.mtimes[dependentFile])
return updateMtime(cache.mtimes, dependentFile, done);
done();
}
function attachCachePersistHandler(b, cacheFile) {
if (!cacheFile) return;
b.on('bundle', function(bundleStream) {
addCacheBlocker(b);
bundleStream.on('end', function() {
removeCacheBlocker(b);
});
// We need to wait until the cache is done being populated.
// Use .once because the `b` browserify object can be re-used for multiple
// bundles. We only want to save the cache once per bundle call.
b.once('_cacheReadyToWrite', function() {
storeCache(b, cacheFile);
});
});
}
function storeCache(b, cacheFile) {
assertExists(cacheFile);
var cache = BrowserifyCache.getCache(b);
fs.writeFile(cacheFile, JSON.stringify(cache), {encoding: 'utf8'}, function(err) {
if (err) b.emit('_cacheFileWriteError', err);
else b.emit('_cacheFileWritten', cacheFile);
});
}
function loadCacheData(b, cacheFile) {
var cacheData = {};
if (cacheFile) {
try {
cacheData = JSON.parse(fs.readFileSync(cacheFile, {encoding: 'utf8'}));
} catch (err) {
// no existing cache file
b.emit('_cacheFileReadError', err);
}
}
return cacheData;
}
function updateMtime(mtimes, file, done) {
assertExists(mtimes);
assertExists(file);
fs.stat(file, function(err, stat) {
if (!err) mtimes[file] = stat.mtime.getTime();
done();
});
}
module.exports = BrowserifyCache;