-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (95 loc) · 4.01 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
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
(() => {
'use strict';
const crypto = require('crypto');
const path = require('path');
const fs = require('fs');
const readonly = require('read-only-stream');
const promisify = require('util').promisify;
const fileStat = promisify(fs.stat);
const cacheFileFolder = path.join(process.cwd(), '.browserifyCache');
const Cache = require('./cache.js');
module.exports = incrementallify;
module.exports.args = {
cache : {},
packageCache: {}
};
function createChecksum (data) {
if (Array.isArray(data) ||
data === Object(data)) {
data = JSON.stringify(data);
}
return crypto.createHash('sha1').update(data, 'utf8').digest('hex');
}
async function checkIfFileExists (filePath, notExistCallback) {
try {
await fileStat(filePath);
} catch (err) {
if (err.code !== 'ENOENT') {
throw Error(err);
}
notExistCallback();
}
}
async function incrementallify (browserify, opts = {}) {
let cache = await Cache();
let cacheFileData = await cache.getCacheFileData();
// create cache directory
await checkIfFileExists(cacheFileFolder, () => {
fs.mkdirSync(cacheFileFolder);
});
const runParams = Object.assign({}, browserify._options, browserify.argv);
const verbose = runParams.v || runParams.verbose;
const runCheckSum = createChecksum(runParams);
const cacheFileFullPath = path.join(cacheFileFolder, runCheckSum);
if (cacheFileData.items[runCheckSum]) {
let stillValid = Object.keys(cacheFileData.items[runCheckSum]).every((originalFilePath) => {
let originalFilePathStat = JSON.stringify(fs.statSync(originalFilePath));
let originalFileCheckSum = createChecksum(originalFilePathStat);
return originalFileCheckSum === cacheFileData.items[runCheckSum][originalFilePath];
});
if (!stillValid) {
if (verbose) console.log(`[${new Date().toLocaleTimeString()}] bundling, sourcefile has changed since last run`);
if (fs.existsSync(cacheFileFullPath)) {
fs.unlinkSync(cacheFileFullPath);
}
}
}
browserify.on('file', function (filePath, id, parent) {
if (!cacheFileData.items[runCheckSum]) {
cacheFileData.items[runCheckSum] = {};
}
let fileCheckSum = JSON.stringify(fs.statSync(filePath));
Object.assign(cacheFileData.items[runCheckSum], {
[path.normalize(filePath)]: createChecksum(fileCheckSum)
});
});
const _bundle = browserify.bundle;
browserify.bundle = function (cb) {
if (fs.existsSync(cacheFileFullPath)) {
browserify.reset();
if (verbose) console.log(`[${new Date().toLocaleTimeString()}] no bundling, use cache`);
const self = this;
const stream = fs.createReadStream(cacheFileFullPath);
const output = readonly(stream);
if (cb) cb(null, output);
const ready = () => {
self.pipeline.end();
};
if (this._pending === 0) ready();
else this.once('_ready', ready);
return output;
}
var args = (typeof (cb) === 'function') ? [cb] : [];
return _bundle.apply(browserify, args);
};
browserify.on('bundle', function (readStream) {
const writeCacheStream = fs.createWriteStream(cacheFileFullPath);
readStream.on('data', (pack) => writeCacheStream.write(pack));
readStream.on('end', () => writeCacheStream.end());
});
browserify.pipeline.get('wrap').on('end', () => {
cache.saveCacheFileData(cacheFileData);
});
return browserify;
}
})();