diff --git a/README.md b/README.md
index 06a1a12..9129da9 100644
--- a/README.md
+++ b/README.md
@@ -47,11 +47,11 @@ module.exports = {
|Name|Type|Default|Description|
|:--:|:--:|:-----:|:----------|
-|**`cacheKey`**|`{Function(options, request) -> {String}}`|`undefined`|Allows you to override default cache key generator|
-|**`cacheDirectory`**|`{String}`|`path.resolve('.cache-loader')`|Provide a cache directory where cache items should be stored (used for default read/write implementation)|
-|**`cacheIdentifier`**|`{String}`|`cache-loader:{version} {process.env.NODE_ENV}`|Provide an invalidation identifier which is used to generate the hashes. You can use it for extra dependencies of loaders (used for default read/write implementation)|
+|**`key`**|`{Function(options, request) -> {String}}`|`undefined`|Allows you to override default cache key generator|
+|**`directory`**|`{String}`|`path.resolve('.cache-loader')`|Provide a cache directory where cache items should be stored (used for default read/write implementation)|
+|**`identifier`**|`{String}`|`cache-loader:{version} {process.env.NODE_ENV}`|Provide an invalidation cache identifier which is used to generate the hashes. You can use it for extra dependencies of loaders (used for default read/write implementation)|
|**`write`**|`{Function(cacheKey, data, callback) -> {void}}`|`undefined`|Allows you to override default write cache data to file (e.g. Redis, memcached)|
-|**`read`**|`{Function(cacheKey, callback) -> {void}}`|`undefined`|Allows you to override default read cache data from file|
+|**`read`**|`{Function(key, callback) -> {void}}`|`undefined`|Allows you to override default read cache data from file|
Examples
@@ -91,12 +91,10 @@ function digest(str) {
return crypto.createHash('md5').update(str).digest('hex');
}
-// Generate own cache key
-function cacheKey(options, request) {
+function key(options, request) {
return `build:cache:${digest(request)}`;
}
-
// Read data from database and parse them
function read(key, callback) {
client.get(key, (err, result) => {
@@ -117,8 +115,7 @@ function read(key, callback) {
});
}
-
-// Write data to database under cacheKey
+// Write data to the database under the generated key
function write(key, data, callback) {
client.set(key, JSON.stringify(data), 'EX', BUILD_CACHE_TIMEOUT, callback);
}
@@ -132,7 +129,7 @@ module.exports = {
{
loader: 'cache-loader',
options: {
- cacheKey,
+ key,
read,
write,
}
diff --git a/src/index.js b/src/index.js
index d73d6f5..fa7d293 100644
--- a/src/index.js
+++ b/src/index.js
@@ -9,20 +9,25 @@ const pkgVersion = require('../package.json').version;
const ENV = process.env.NODE_ENV || 'development';
const defaults = {
- cacheDirectory: path.resolve('.cache-loader'),
- cacheIdentifier: `cache-loader:${pkgVersion} ${ENV}`,
- cacheKey,
+ directory: path.resolve('.cache-loader'),
+ identifier: `cache-loader:${pkgVersion} ${ENV}`,
+ key,
read,
write,
};
function loader(...args) {
+ const { data } = this;
+
const options = Object.assign({}, defaults, loaderUtils.getOptions(this));
const { write: writeFn } = options;
+
const callback = this.async();
- const { data } = this;
- const dependencies = this.getDependencies().concat(this.loaders.map(l => l.path));
+
+ const dependencies = this.getDependencies()
+ .concat(this.loaders.map(loader => loader.path));
const contextDependencies = this.getContextDependencies();
+
const toDepDetails = (dep, mapCallback) => {
fs.stat(dep, (err, stats) => {
if (err) {
@@ -45,7 +50,8 @@ function loader(...args) {
return;
}
const [deps, contextDeps] = taskResults;
- writeFn(data.cacheKey, {
+
+ writeFn(data.key, {
remainingRequest: data.remainingRequest,
dependencies: deps,
contextDependencies: contextDeps,
@@ -60,12 +66,13 @@ function loader(...args) {
function pitch(remainingRequest, prevRequest, dataInput) {
const options = Object.assign({}, defaults, loaderUtils.getOptions(this));
const callback = this.async();
- const { read: readFn, cacheKey: cacheKeyFn } = options;
+ const { read: readFn, key: keyFn } = options;
const data = dataInput;
data.remainingRequest = remainingRequest;
- data.cacheKey = cacheKeyFn(options, remainingRequest);
- readFn(data.cacheKey, (readErr, cacheData) => {
+ data.key = keyFn(options, remainingRequest);
+
+ readFn(data.key, (readErr, cacheData) => {
if (readErr) {
callback();
return;
@@ -79,10 +86,12 @@ function pitch(remainingRequest, prevRequest, dataInput) {
fs.stat(dep.path, (statErr, stats) => {
if (statErr) {
eachCallback(statErr);
+
return;
}
if (stats.mtime.getTime() !== dep.mtime) {
eachCallback(true);
+
return;
}
eachCallback();
@@ -90,10 +99,13 @@ function pitch(remainingRequest, prevRequest, dataInput) {
}, (err) => {
if (err) {
callback();
+
return;
}
+
cacheData.dependencies.forEach(dep => this.addDependency(dep.path));
cacheData.contextDependencies.forEach(dep => this.addContextDependency(dep.path));
+
callback(null, ...cacheData.result);
});
});
@@ -142,11 +154,11 @@ function read(key, callback) {
});
}
-function cacheKey(options, request) {
- const { cacheIdentifier, cacheDirectory } = options;
- const hash = digest(`${cacheIdentifier}\n${request}`);
+function key(options, request) {
+ const { identifier, directory } = options;
+ const hash = digest(`${identifier}\n${request}`);
- return path.join(cacheDirectory, `${hash}.json`);
+ return path.join(directory, `${hash}.json`);
}
export { loader as default, pitch };