This repository was archived by the owner on Oct 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathcompile-cache.js
More file actions
385 lines (330 loc) · 13.8 KB
/
compile-cache.js
File metadata and controls
385 lines (330 loc) · 13.8 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import fs from 'fs';
import path from 'path';
import zlib from 'zlib';
import createDigestForObject from './digest-for-object';
import {pfs, pzlib} from './promise';
import mkdirp from 'mkdirp';
const d = require('debug')('electron-compile:compile-cache');
/**
* CompileCache manages getting and setting entries for a single compiler; each
* in-use compiler will have an instance of this class, usually created via
* {@link createFromCompiler}.
*
* You usually will not use this class directly, it is an implementation class
* for {@link CompileHost}.
*/
export default class CompileCache {
/**
* Creates an instance, usually used for testing only.
*
* @param {string} cachePath The root directory to use as a cache path
*
* @param {FileChangedCache} fileChangeCache A file-change cache that is
* optionally pre-loaded.
* @param {string} sourceMapPath The directory to store sourcemap separately if compiler option enabled to emit.
* Default to cachePath if not specified.
*/
constructor(cachePath, fileChangeCache, sourceMapPath = null) {
this.cachePath = cachePath;
this.fileChangeCache = fileChangeCache;
this.sourceMapPath = sourceMapPath || this.cachePath;
}
/**
* Creates a CompileCache from a class compatible with the CompilerBase
* interface. This method uses the compiler name / version / options to
* generate a unique directory name for cached results
*
* @param {string} cachePath The root path to use for the cache, a directory
* representing the hash of the compiler parameters
* will be created here.
*
* @param {CompilerBase} compiler The compiler to use for version / option
* information.
*
* @param {FileChangedCache} fileChangeCache A file-change cache that is
* optionally pre-loaded.
*
* @param {boolean} readOnlyMode Don't attempt to create the cache directory.
*
* @param {string} sourceMapPath The directory to store sourcemap separately if compiler option enabled to emit.
* Default to cachePath if not specified.
*
* @return {CompileCache} A configured CompileCache instance.
*/
static createFromCompiler(cachePath, compiler, fileChangeCache, readOnlyMode = false, sourceMapPath = null) {
let newCachePath = null;
let getCachePath = () => {
if (newCachePath) return newCachePath;
const digestObj = {
name: compiler.name || Object.getPrototypeOf(compiler).constructor.name,
version: compiler.getCompilerVersion(),
options: compiler.compilerOptions
};
newCachePath = path.join(cachePath, createDigestForObject(digestObj));
d(`Path for ${digestObj.name}: ${newCachePath}`);
d(`Set up with parameters: ${JSON.stringify(digestObj)}`);
if (!readOnlyMode) mkdirp.sync(newCachePath);
return newCachePath;
};
let ret = new CompileCache('', fileChangeCache);
ret.getCachePath = getCachePath;
const newSourceMapPath = sourceMapPath;
ret.getSourceMapPath = () => newSourceMapPath || getCachePath();
return ret;
}
/**
* Returns a file's compiled contents from the cache.
*
* @param {string} filePath The path to the file. FileChangedCache will look
* up the hash and use that as the key in the cache.
*
* @return {Promise<Object>} An object with all kinds of information
*
* @property {Object} hashInfo The hash information returned from getHashForPath
* @property {string} code The source code if the file was a text file
* @property {Buffer} binaryData The file if it was a binary file
* @property {string} mimeType The MIME type saved in the cache.
* @property {string[]} dependentFiles The dependent files returned from
* compiling the file, if any.
*/
async get(filePath) {
d(`Fetching ${filePath} from cache`);
let hashInfo = await this.fileChangeCache.getHashForPath(path.resolve(filePath));
let code = null;
let mimeType = null;
let binaryData = null;
let dependentFiles = null;
let cacheFile = null;
try {
cacheFile = path.join(this.getCachePath(), hashInfo.hash);
let result = null;
if (hashInfo.isFileBinary) {
d("File is binary, reading out info");
let info = JSON.parse(await pfs.readFile(cacheFile + '.info'));
mimeType = info.mimeType;
dependentFiles = info.dependentFiles;
binaryData = hashInfo.binaryData;
if (!binaryData) {
binaryData = await pfs.readFile(cacheFile);
binaryData = await pzlib.gunzip(binaryData);
}
} else {
let buf = await pfs.readFile(cacheFile);
let str = (await pzlib.gunzip(buf)).toString('utf8');
result = JSON.parse(str);
code = result.code;
mimeType = result.mimeType;
dependentFiles = result.dependentFiles;
}
} catch (e) {
d(`Failed to read cache for ${filePath}, looked in ${cacheFile}: ${e.message}`);
}
return { hashInfo, code, mimeType, binaryData, dependentFiles };
}
/**
* Saves a compiled result to cache
*
* @param {Object} hashInfo The hash information returned from getHashForPath
*
* @param {string / Buffer} codeOrBinaryData The file's contents, either as
* a string or a Buffer.
* @param {string} mimeType The MIME type returned by the compiler.
*
* @param {string[]} dependentFiles The list of dependent files returned by
* the compiler.
* @return {Promise} Completion.
*/
async save(hashInfo, codeOrBinaryData, mimeType, dependentFiles) {
let buf = null;
let target = path.join(this.getCachePath(), hashInfo.hash);
d(`Saving to ${target}`);
if (hashInfo.isFileBinary) {
buf = await pzlib.gzip(codeOrBinaryData);
await pfs.writeFile(target + '.info', JSON.stringify({mimeType, dependentFiles}), 'utf8');
} else {
buf = await pzlib.gzip(new Buffer(JSON.stringify({code: codeOrBinaryData, mimeType, dependentFiles})));
}
await pfs.writeFile(target, buf);
}
/**
* Attempts to first get a key via {@link get}, then if it fails, call a method
* to retrieve the contents, then save the result to cache.
*
* The fetcher parameter is expected to have the signature:
*
* Promise<Object> fetcher(filePath : string, hashInfo : Object);
*
* hashInfo is a value returned from getHashForPath
* The return value of fetcher must be an Object with the properties:
*
* mimeType - the MIME type of the data to save
* code (optional) - the source code as a string, if file is text
* binaryData (optional) - the file contents as a Buffer, if file is binary
* dependentFiles - the dependent files returned by the compiler.
*
* @param {string} filePath The path to the file. FileChangedCache will look
* up the hash and use that as the key in the cache.
*
* @param {Function} fetcher A method which conforms to the description above.
*
* @param {string} appRoot (optional)The top-level directory for your application to consturct hierarchy for map files.
* If not specified, all maps are stored as flattened under given sourcemap path.
*
* @return {Promise<Object>} An Object which has the same fields as the
* {@link get} method return result.
*/
async getOrFetch(filePath, fetcher, appRoot = null) {
let cacheResult = await this.get(filePath);
let anyDependenciesChanged = await this.haveAnyDependentFilesChanged(cacheResult);
if ((cacheResult.code || cacheResult.binaryData) && !anyDependenciesChanged) {
return cacheResult;
}
let result = await fetcher(filePath, cacheResult.hashInfo) || { hashInfo: cacheResult.hashInfo };
if (result.mimeType && !cacheResult.hashInfo.isInNodeModules) {
d(`Cache miss: saving out info for ${filePath}`);
await this.save(cacheResult.hashInfo, result.code || result.binaryData, result.mimeType, result.dependentFiles);
const map = result.sourceMaps;
if (map) {
d(`source map for ${filePath} found, saving it to ${this.getSourceMapPath()}`);
await this.saveSourceMap(cacheResult.hashInfo, filePath, appRoot, map);
}
}
result.hashInfo = cacheResult.hashInfo;
return result;
}
/**
* @private Check if any of a file's dependencies have changed
*/
async haveAnyDependentFilesChanged(cacheResult) {
if (!cacheResult.code || !cacheResult.dependentFiles.length) return false;
for (let dependentFile of cacheResult.dependentFiles) {
let hasFileChanged = await this.fileChangeCache.hasFileChanged(dependentFile);
if (hasFileChanged) {
return true;
}
let dependentFileCacheResult = await this.get(dependentFile);
if (dependentFileCacheResult.dependentFiles && dependentFileCacheResult.dependentFiles.length) {
let anySubdependentFilesChanged = await this.haveAnyDependentFilesChanged(dependentFileCacheResult);
if (anySubdependentFilesChanged) return true;
}
}
return false;
}
getSync(filePath) {
d(`Fetching ${filePath} from cache`);
let hashInfo = this.fileChangeCache.getHashForPathSync(path.resolve(filePath));
let code = null;
let mimeType = null;
let binaryData = null;
let dependentFiles = null;
try {
let cacheFile = path.join(this.getCachePath(), hashInfo.hash);
let result = null;
if (hashInfo.isFileBinary) {
d("File is binary, reading out info");
let info = JSON.parse(fs.readFileSync(cacheFile + '.info'));
mimeType = info.mimeType;
dependentFiles = info.dependentFiles;
binaryData = hashInfo.binaryData;
if (!binaryData) {
binaryData = fs.readFileSync(cacheFile);
binaryData = zlib.gunzipSync(binaryData);
}
} else {
let buf = fs.readFileSync(cacheFile);
let str = (zlib.gunzipSync(buf)).toString('utf8');
result = JSON.parse(str);
code = result.code;
mimeType = result.mimeType;
dependentFiles = result.dependentFiles;
}
} catch (e) {
d(`Failed to read cache for ${filePath}`);
}
return { hashInfo, code, mimeType, binaryData, dependentFiles };
}
saveSync(hashInfo, codeOrBinaryData, mimeType, dependentFiles) {
let buf = null;
let target = path.join(this.getCachePath(), hashInfo.hash);
d(`Saving to ${target}`);
if (hashInfo.isFileBinary) {
buf = zlib.gzipSync(codeOrBinaryData);
fs.writeFileSync(target + '.info', JSON.stringify({mimeType, dependentFiles}), 'utf8');
} else {
buf = zlib.gzipSync(new Buffer(JSON.stringify({code: codeOrBinaryData, mimeType, dependentFiles})));
}
fs.writeFileSync(target, buf);
}
getOrFetchSync(filePath, fetcher, appRoot = null) {
let cacheResult = this.getSync(filePath);
if (cacheResult.code || cacheResult.binaryData) return cacheResult;
let result = fetcher(filePath, cacheResult.hashInfo) || { hashInfo: cacheResult.hashInfo };
if (result.mimeType && !cacheResult.hashInfo.isInNodeModules) {
d(`Cache miss: saving out info for ${filePath}`);
this.saveSync(cacheResult.hashInfo, result.code || result.binaryData, result.mimeType, result.dependentFiles);
}
const map = result.sourceMaps;
if (map) {
d(`source map for ${filePath} found, saving it to ${this.getSourceMapPath()}`);
this.saveSourceMapSync(cacheResult.hashInfo, filePath, appRoot, map);
}
result.hashInfo = cacheResult.hashInfo;
return result;
}
buildSourceMapTarget(hashInfo, filePath, appRoot) {
const mapPath = appRoot ? path.relative(appRoot, filePath) : filePath;
const mapFileName = mapPath.replace(path.extname(filePath), '.js.map');
const target = path.join(this.getSourceMapPath(), mapFileName);
mkdirp.sync(path.dirname(target));
d(`Sourcemap target is: ${target}`);
return target;
}
/**
* Saves sourcemap string into cache, or specified separate dir
*
* @param {Object} hashInfo The hash information returned from getHashForPath
*
* @param {string} filePath Path to original file to construct sourcemap file name
*
* @param {string} appRoot The top-level directory for your application to consturct hierarchy for map files.
* If not specified, all maps are stored as flattened under given sourcemap path.
*
* @param {string} sourceMap Sourcemap data as string
*
* @memberOf CompileCache
*/
async saveSourceMap(hashInfo, filePath, appRoot, sourceMap) {
const target = this.buildSourceMapTarget(hashInfo, filePath, appRoot);
await pfs.writeFile(target, sourceMap, 'utf-8');
}
saveSourceMapSync(hashInfo, filePath, appRoot, sourceMap) {
const target = this.buildSourceMapTarget(hashInfo, filePath, appRoot);
fs.writeFileSync(target, sourceMap, 'utf-8');
}
/**
* @private
*/
getCachePath() {
// NB: This is an evil hack so that createFromCompiler can stomp it
// at will
return this.cachePath;
}
/**
* @private
*/
getSourceMapPath() {
return this.sourceMapPath;
}
/**
* Returns whether a file should not be compiled. Note that this doesn't
* necessarily mean it won't end up in the cache, only that its contents are
* saved verbatim instead of trying to find an appropriate compiler.
*
* @param {Object} hashInfo The hash information returned from getHashForPath
*
* @return {boolean} True if a file should be ignored
*/
static shouldPassthrough(hashInfo) {
return hashInfo.isMinified || hashInfo.isInNodeModules || hashInfo.hasSourceMap || hashInfo.isFileBinary;
}
}