-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
193 lines (165 loc) · 5.19 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
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
'use strict';
var path = require('path'),
fs = require('fs'),
defaults = require('lodash.defaults'),
assign = require('lodash.assign');
var IGNORE = require('./lib/ignore'),
cycle = require('./lib/cycle'),
encode = require('./lib/encode'),
decode = require('./lib/decode');
function PersistentCacheWebpackPlugin(options) {
this.options = defaults(options || {}, {
webpack: null,
file : './webpack.cache.json',
warn : true,
stats : false,
persist: true,
ignore : []
});
}
module.exports = PersistentCacheWebpackPlugin;
PersistentCacheWebpackPlugin.prototype.apply = function apply(compiler) {
var options = this.options,
stats = {
deserialise: {
fs : {},
decode: {},
retrocycle: {}
},
serialise : {
encode : {},
decycle: {},
fs : {}
}
},
pending;
compiler.plugin('watch-run', onInit);
compiler.plugin('run', onInit);
compiler.plugin('compilation', onCompilation);
compiler.plugin('after-emit', afterEmit);
/**
* Deserialise any existing file into pending cache elements.
*/
function onInit(unused, callback) {
var filePath = path.resolve(options.file);
stats.deserialise.fs.start = Date.now();
if (fs.existsSync(filePath)) {
fs.readFile(filePath, complete);
} else {
complete(true);
}
function complete(error, contents) {
stats.deserialise.fs.stop = Date.now();
stats.deserialise.decode.start = Date.now();
pending = !error && contents && cycle.retrocycle(decode(JSON.parse(contents)));
stats.deserialise.decode.stop = Date.now();
stats.deserialise.success = !error;
callback();
}
}
/**
* Apply the cache items as defaults.
*/
function onCompilation(compilation) {
assign(compilation.cache, pending);
}
/**
* Serialise the cache to file, don't wait for async.
*/
function afterEmit(compilation, callback) {
var failures;
if (options.persist) {
var cache = compilation.cache,
filePath = path.resolve(options.file);
stats.serialise.encode.start = Date.now();
var encoded = encode(cache);
failures = (encoded.$failed || [])
.filter(filterIgnored);
delete encoded.$failed;
stats.serialise.encode.stop = Date.now();
stats.failures = failures.length;
// abort
if (failures.length) {
stats.serialise.fs.start = Date.now();
fs.unlink(filePath, complete.bind(null, true));
}
// serialise and write file
else {
stats.serialise.decycle.start = Date.now();
var decycled = cycle.decycle(encoded);
stats.serialise.decycle.stop = Date.now();
stats.serialise.fs.start = Date.now();
var buffer = new Buffer(JSON.stringify(decycled, null, 2));
stats.serialise.size = buffer.length;
fs.writeFile(filePath, buffer, complete);
}
}
else {
failures = [];
complete();
}
function complete(error) {
stats.serialise.fs.stop = Date.now();
stats.serialise.success = !error;
options.warn && pushFailures(failures, compilation.warnings, (options.warn === 'verbose'));
options.stats && printStats(stats);
callback();
}
}
function filterIgnored(value) {
return !IGNORE.concat(options.ignore).some(testRegex);
function testRegex(regex) {
return regex.test(value);
}
}
};
function pushFailures(failures, array, isVerbose) {
if (failures.length) {
var text = ['persistent-cache-webpack-plugin: failed to serialise the compiler cache']
.concat(failures.map(eachFailure).filter(firstOccurance))
.join('\n');
array.push(text);
}
function eachFailure(value) {
return isVerbose ? value.map(addIndent).join('\n') : addIndent(value[0], 0);
}
function firstOccurance(value, i, array) {
return (array.indexOf(value) === i);
}
function addIndent(value, i) {
return (new Array(4 + i * 2)).join(' ') + value;
}
}
function printStats(stats) {
var text = [
'persistent-cache-webpack-plugin: statistics',
' deserialise:',
' success: ' + stats.deserialise.success,
' size : ' + formatFloat(stats.deserialise.size / 1E+6) + ' MB',
' time : ' + getTime(stats.deserialise),
' serialise:',
' success: ' + stats.serialise.success + ', ' + stats.failures + ' encoder failures',
' size : ' + formatFloat(stats.serialise.size / 1E+6) + ' MB',
' time : ' + getTime(stats.serialise)
].join('\n');
console.log(text);
function getTime(collection) {
return collection && Object.keys(collection).filter(isTime).map(eachKey).join(', ') || '-';
function isTime(key) {
return collection[key].start && collection[key].stop;
}
function eachKey(key) {
return formatFloat((collection[key].stop - collection[key].start) / 1E+3) + ' seconds ' + key;
}
}
}
function formatFloat(value) {
if (isNaN(value)) {
return '-';
}
else {
var integer = Math.round(value),
decimal = Math.floor((value % 1.0) * 1E+3);
return integer + '.' + String(decimal + '000').slice(0, 3);
}
}