-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathindex.js
More file actions
202 lines (165 loc) · 5.56 KB
/
index.js
File metadata and controls
202 lines (165 loc) · 5.56 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
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var mtime = require('./mtime');
var digest = require('./digest');
var SCHEMA_VERSION = 1;
// hand-tuned optimal concurrency for a 15" macbook pro :)
var CONCURRENCY_LIMIT = 40;
function OnlyIfChangedPlugin(opts) {
if (!opts.cacheDirectory)
throw new Error('missing required opt cacheDirectory');
if (!opts.cacheIdentifier)
throw new Error('missing required opt cacheIdentifier');
this.cacheDirectory = opts.cacheDirectory;
this.cacheIdentifier = digest.digestSHA1(
JSON.stringify(opts.cacheIdentifier)
);
this.concurrencyLimit = opts.concurrencyLimit || CONCURRENCY_LIMIT;
this.cache = makeCacheRecord();
this.makeCacheDirectory();
}
OnlyIfChangedPlugin.prototype.makeCacheDirectory = function () {
mkdirp(this.cacheDirectory, function (err) {
if (err) console.error(err);
});
};
OnlyIfChangedPlugin.prototype.getCacheFilePath = function () {
return path.join(
this.cacheDirectory,
'onlyifchanged-' + SCHEMA_VERSION + '-' + this.cacheIdentifier + '.json'
);
};
OnlyIfChangedPlugin.prototype.writeCacheFile = function () {
fs.writeFileSync(this.getCacheFilePath(), JSON.stringify(this.cache));
};
OnlyIfChangedPlugin.prototype.readCacheFile = function () {
this.cache = JSON.parse(
fs.readFileSync(this.getCacheFilePath(), {encoding: 'utf8'})
);
};
OnlyIfChangedPlugin.prototype.updateDependenciesMtimes = function (
fileDependencies,
done
) {
var pluginContext = this;
mtime.getFilesMtimes(fileDependencies, this.concurrencyLimit, function (
err,
filesMtimes
) {
if (err) return done(err);
// merge in updated mtimes
Object.keys(filesMtimes).forEach(function (file) {
pluginContext.cache.inputFilesMtimes[file] = filesMtimes[file];
});
done();
});
};
OnlyIfChangedPlugin.prototype.updateAssetHash = function (file, contents) {
this.cache.outputFilesHashes[file] = digest.digestMD5(contents);
};
OnlyIfChangedPlugin.prototype.isCacheEmpty = function () {
return (
Object.keys(this.cache.inputFilesMtimes).length === 0 ||
Object.keys(this.cache.outputFilesHashes).length === 0
);
};
OnlyIfChangedPlugin.prototype.hasAnyFileChanged = function (done) {
var pluginContext = this;
mtime.hasAnyFileChanged(
pluginContext.cache.inputFilesMtimes,
pluginContext.concurrencyLimit,
function (err, anyMtimeChanged) {
if (err) return done(err);
if (anyMtimeChanged) return done(null, true);
digest.hasAnyFileChanged(
pluginContext.cache.outputFilesHashes,
pluginContext.concurrencyLimit,
function (err, anyHashChanged) {
if (err) return done(err);
done(null, anyHashChanged);
}
);
}
);
};
OnlyIfChangedPlugin.prototype.apply = function (compiler) {
var pluginContext = this;
// upvar tracking whether for a particular webpack run, compilation should be done
// assumes such runs cannot happen multiple times concurrently per plugin instance
var shouldCompile = true;
// at the very start of the webpack run we determine if we need to rebuild or not
compiler.plugin('run', function (_, runDone) {
shouldCompile = true;
try {
pluginContext.readCacheFile();
} catch (readCacheErr) {
if (readCacheErr.code === 'ENOENT') {
// cache file missing
return runDone();
}
return runDone(readCacheErr);
}
pluginContext.hasAnyFileChanged(function (err, anyChanged) {
if (err) return runDone(err);
// rebuild if any file changed
shouldCompile = anyChanged;
// always rebuild if no known input or output files
if (pluginContext.isCacheEmpty()) {
shouldCompile = true;
}
// clear known files when rebuilding
if (shouldCompile) {
pluginContext.cache = makeCacheRecord();
}
runDone();
});
});
compiler.plugin('compilation', function (compilation) {
if (!shouldCompile) {
// duck punch compilation object to make addEntry a no-op (ignore all entrypoints)
compilation.addEntry = function (context, entry, name, done) {
done();
};
}
});
// collect info about input dependencies to compilation
compiler.plugin('after-compile', function (compilation, afterCompileDone) {
// convert to array in case webpack gives us a set (in webpack v4)
var fileDependencies = Array.from(compilation.fileDependencies);
// get updated mtimes of file dependencies of compilation
pluginContext.updateDependenciesMtimes(fileDependencies, afterCompileDone);
});
compiler.plugin('should-emit', function () {
// don't emit any files if nothing was built
return shouldCompile;
});
// collect info about output of compilation
compiler.plugin('after-emit', function (compilation, done) {
if (!shouldCompile) return done();
var emittedFiles = Object.keys(compilation.assets).filter(function (file) {
var source = compilation.assets[file];
return source.emitted && source.existsAt;
});
emittedFiles.forEach(function (file) {
var source = compilation.assets[file];
var content = source.source();
var contentToHash = Buffer.isBuffer(content)
? content
: new Buffer(content, 'utf-8');
pluginContext.updateAssetHash(source.existsAt, contentToHash);
});
done();
});
compiler.plugin('done', function () {
if (!shouldCompile) return;
pluginContext.writeCacheFile();
});
};
function makeCacheRecord() {
return {
inputFilesMtimes: {},
outputFilesHashes: {},
};
}
module.exports = OnlyIfChangedPlugin;