Skip to content
This repository was archived by the owner on Oct 27, 2020. It is now read-only.

Commit b6932ab

Browse files
author
evilebottnawi
committed
feat: invalidate cache
1 parent 9ca733c commit b6932ab

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/index.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const path = require('path');
44
const mkdirp = require('mkdirp');
55
const async = require('async');
66
const loaderUtils = require('loader-utils');
7+
const pkg = require('../package.json');
78

89
function loader(...args) {
910
const callback = this.async();
@@ -58,13 +59,25 @@ function loader(...args) {
5859
}
5960

6061
function pitch(remainingRequest, prevRequest, dataInput) {
61-
const options = loaderUtils.getOptions(this) || {};
62-
const cacheDirectory = options.cacheDirectory || path.resolve('.cache-loader');
62+
const loaderOptions = loaderUtils.getOptions(this) || {};
63+
const defaultOptions = {
64+
cacheDirectory: path.resolve('.cache-loader'),
65+
cacheIdentifier: JSON.stringify({
66+
'cache-loader': pkg.version,
67+
env: process.env.NODE_ENV || 'development',
68+
}),
69+
};
70+
const options = Object.assign({}, defaultOptions, loaderOptions);
71+
const { cacheIdentifier, cacheDirectory } = options;
6372
const data = dataInput;
64-
data.remainingRequest = remainingRequest;
6573
const callback = this.async();
66-
const hash = digest(remainingRequest);
74+
const hash = digest(JSON.stringify({
75+
remainingRequest,
76+
cacheIdentifier,
77+
options,
78+
}));
6779
const cacheFile = path.join(cacheDirectory, `${hash}.json`);
80+
data.remainingRequest = remainingRequest;
6881
data.cacheFile = cacheFile;
6982
fs.readFile(cacheFile, 'utf-8', (readFileErr, content) => {
7083
if (readFileErr) {

0 commit comments

Comments
 (0)