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

feat: invalidate cache #9

Merged
merged 3 commits into from
Aug 28, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const path = require('path');
const mkdirp = require('mkdirp');
const async = require('async');
const loaderUtils = require('loader-utils');
const pkgVersion = require('../package.json').version;

const defaultCacheDirectory = path.resolve('.cache-loader');
const ENV = process.env.NODE_ENV || 'development';

function loader(...args) {
const callback = this.async();
Expand Down Expand Up @@ -34,6 +38,7 @@ function loader(...args) {
const writeCacheFile = () => {
fs.writeFile(data.cacheFile, JSON.stringify({
remainingRequest: data.remainingRequest,
cacheIdentifier: data.cacheIdentifier,
dependencies: deps,
contextDependencies: contextDeps,
result: args,
Expand All @@ -58,13 +63,19 @@ function loader(...args) {
}

function pitch(remainingRequest, prevRequest, dataInput) {
const options = loaderUtils.getOptions(this) || {};
const cacheDirectory = options.cacheDirectory || path.resolve('.cache-loader');
const loaderOptions = loaderUtils.getOptions(this) || {};
const defaultOptions = {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to be the same for every call to pitch() so it would make sense to define it outside the fn.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@STRML done

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - doesn't appear pushed though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evilebottnawi Can't the whole defaultOptions block be moved for some reason ? 🙃

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michael-ciniawsky oh, yep, your are right

cacheDirectory: defaultCacheDirectory,
cacheIdentifier: `cache-loader:${pkgVersion} ${ENV}`,
};
const options = Object.assign({}, defaultOptions, loaderOptions);
const { cacheIdentifier, cacheDirectory } = options;
const data = dataInput;
data.remainingRequest = remainingRequest;
const callback = this.async();
const hash = digest(remainingRequest);
const hash = digest(`${cacheIdentifier}\n${remainingRequest}`);
const cacheFile = path.join(cacheDirectory, `${hash}.json`);
data.remainingRequest = remainingRequest;
data.cacheIdentifier = cacheIdentifier;
data.cacheFile = cacheFile;
fs.readFile(cacheFile, 'utf-8', (readFileErr, content) => {
if (readFileErr) {
Expand All @@ -79,7 +90,7 @@ function pitch(remainingRequest, prevRequest, dataInput) {
callback();
return;
}
if (cacheData.remainingRequest !== remainingRequest) {
if (cacheData.remainingRequest !== remainingRequest || cacheData.cacheIdentifier !== cacheIdentifier) {
// in case of a hash conflict
callback();
return;
Expand Down