Skip to content
This repository was archived by the owner on Oct 27, 2020. It is now read-only.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module.exports = {

|Name|Type|Default|Description|
|:--:|:--:|:-----:|:----------|
|**`cacheContext`**|`{String}`|`undefined`|Allows you to override the default cache context in order to generate the cache relatively to a path. By default it will use absolute paths|
|**`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)|
Expand Down
31 changes: 24 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,30 @@ const env = process.env.NODE_ENV || 'development';
const schema = require('./options.json');

const defaults = {
cacheContext: '',
cacheDirectory: path.resolve('.cache-loader'),
cacheIdentifier: `cache-loader:${pkg.version} ${env}`,
cacheKey,
read,
write,
};

function pathWithCacheContext(cacheContext, originalPath) {
if (!cacheContext) {
return originalPath;
}

if (originalPath.includes(cacheContext)) {
return originalPath.split('!')
.map(subPath => path.relative(cacheContext, subPath))
.join('!');
}

return originalPath.split('!')
.map(subPath => path.resolve(cacheContext, subPath))
.join('!');
}

function loader(...args) {
const options = Object.assign({}, defaults, getOptions(this));

Expand Down Expand Up @@ -59,7 +76,7 @@ function loader(...args) {
}

mapCallback(null, {
path: dep,
path: pathWithCacheContext(options.cacheContext, dep),
mtime,
});
});
Expand Down Expand Up @@ -95,19 +112,19 @@ function pitch(remainingRequest, prevRequest, dataInput) {

validateOptions(schema, options, 'Cache Loader (Pitch)');

const { read: readFn, cacheKey: cacheKeyFn } = options;
const { read: readFn, cacheContext, cacheKey: cacheKeyFn } = options;

const callback = this.async();
const data = dataInput;

data.remainingRequest = remainingRequest;
data.cacheKey = cacheKeyFn(options, remainingRequest);
data.remainingRequest = pathWithCacheContext(cacheContext, remainingRequest);
data.cacheKey = cacheKeyFn(options, data.remainingRequest);
readFn(data.cacheKey, (readErr, cacheData) => {
if (readErr) {
callback();
return;
}
if (cacheData.remainingRequest !== remainingRequest) {
if (cacheData.remainingRequest !== data.remainingRequest) {
// in case of a hash conflict
callback();
return;
Expand All @@ -131,8 +148,8 @@ function pitch(remainingRequest, prevRequest, dataInput) {
callback();
return;
}
cacheData.dependencies.forEach(dep => this.addDependency(dep.path));
cacheData.contextDependencies.forEach(dep => this.addContextDependency(dep.path));
cacheData.dependencies.forEach(dep => this.addDependency(pathWithCacheContext(cacheContext, dep.path)));
cacheData.contextDependencies.forEach(dep => this.addContextDependency(pathWithCacheContext(cacheContext, dep.path)));
callback(null, ...cacheData.result);
});
});
Expand Down
3 changes: 3 additions & 0 deletions src/options.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"type": "object",
"properties": {
"cacheContext": {
"type": "string"
},
"cacheKey": {
"instanceof": "Function"
},
Expand Down