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

Commit 4fa74df

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

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/index.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ 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');
8+
9+
const defaultCacheDirectory = path.resolve('.cache-loader');
710

811
function loader(...args) {
912
const callback = this.async();
@@ -58,13 +61,21 @@ function loader(...args) {
5861
}
5962

6063
function pitch(remainingRequest, prevRequest, dataInput) {
61-
const options = loaderUtils.getOptions(this) || {};
62-
const cacheDirectory = options.cacheDirectory || path.resolve('.cache-loader');
64+
const loaderOptions = loaderUtils.getOptions(this) || {};
65+
const defaultOptions = {
66+
cacheDirectory: defaultCacheDirectory,
67+
cacheIdentifier: JSON.stringify({
68+
'cache-loader': pkg.version,
69+
env: process.env.NODE_ENV || 'development',
70+
}),
71+
};
72+
const options = Object.assign({}, defaultOptions, loaderOptions);
73+
const { cacheIdentifier, cacheDirectory } = options;
6374
const data = dataInput;
64-
data.remainingRequest = remainingRequest;
6575
const callback = this.async();
66-
const hash = digest(remainingRequest);
76+
const hash = digest(remainingRequest + cacheIdentifier);
6777
const cacheFile = path.join(cacheDirectory, `${hash}.json`);
78+
data.remainingRequest = remainingRequest;
6879
data.cacheFile = cacheFile;
6980
fs.readFile(cacheFile, 'utf-8', (readFileErr, content) => {
7081
if (readFileErr) {

0 commit comments

Comments
 (0)