-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (71 loc) · 2.11 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
var caching = require('./caching');
var loaderUtils = require('loader-utils');
function cachedLoaderPitch(remainingRequest, precedingRequest, data) {
this.cacheable && this.cacheable();
var callback = this.async();
var query = loaderUtils.parseQuery(this.query);
if (!query.cacheDirectory) {
return callback(new Error('required query arg "cacheDirectory" not set'));
}
data.cacheDirectory = query.cacheDirectory;
data.cacheIdentifier = query.cacheIdentifier || '';
data.debug = query.debug;
var resourcePath = this.resourcePath;
var pitchContext = this;
caching.ensureCacheDir(query.cacheDirectory)
.then(function() {
return caching.readCache(
query.cacheIdentifier,
query.cacheDirectory,
resourcePath,
query.debug ? logToStderr : logToNull
);
})
.then(function(cacheRecord) {
if (cacheRecord != null) {
Object.keys(cacheRecord.dependencies)
.forEach(function(depFile) {
pitchContext.addDependency(depFile);
});
callback(null, cacheRecord.content);
} else {
callback();
}
})
.catch(function(err) {
callback(err);
});
}
function cachedLoader(content) {
this.cacheable && this.cacheable();
var callback = this.async();
if (!this.data.cacheDirectory) {
return callback(new Error('required query arg "cacheDirectory" not set'));
}
var resourcePath = this.resourcePath;
var data = this.data;
var fileDependencies = this._module.fileDependencies;
caching.ensureCacheDir(this.data.cacheDirectory)
.then(function() {
return caching.writeCache(
data.cacheIdentifier,
data.cacheDirectory,
resourcePath,
content,
fileDependencies,
data.debug ? logToStderr : logToNull
);
})
.then(function() {
callback(null, content);
})
.catch(function(err) {
callback(err);
});
}
function logToStderr() {
console.error.apply(console, ['cached-loader:'].concat([].slice.call(arguments)));
}
function logToNull() {}
cachedLoader.pitch = cachedLoaderPitch;
module.exports = cachedLoader;