Skip to content

Commit

Permalink
Cache fix
Browse files Browse the repository at this point in the history
 - Cache folder now have image-optimize-loader package version
 - Fixed broken values
  • Loading branch information
Gleb Mikheev committed Jul 13, 2016
1 parent 5c3a172 commit 22bf128
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
15 changes: 9 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
const pjson = require('./package.json');

const imagemin = require('imagemin');
const imageminMozjpeg = require('imagemin-mozjpeg');
Expand All @@ -15,13 +16,13 @@ const gm = require('gm');
const streamifier = require('streamifier');

const Cache = require('async-disk-cache');
const cache = new Cache('image-optimize-loader', { supportBuffer: true });
const cache = new Cache(`image-optimize-loader-${pjson.version}`, { supportBuffer: true });
const TaskQueue = require('task-queue-async');
const asyncQueue = new TaskQueue({
sleepBetweenTasks: 1,
concurrency: 4,
});
// const concurrency = 0;
// let concurrency = 0;

function getHashOf(value) {
return crypto.createHash('sha256').update(value).digest('hex');
Expand Down Expand Up @@ -127,10 +128,12 @@ module.exports = function (content) {

// check is cache up to date
cache.get(`${cacheKey}-checksum`).then(results => {
const data = JSON.parse(results.value);

// if file not changed, return cached value
if (results.value.fileHash === fileHash) {
if (data.fileHash === fileHash) {
cache.get(cacheKey).then(cacheEntry => {
this.resource = results.value.fileHash;
this.resource = data.resource;
promisePrepareImage.reject();
return callback(null, cacheEntry.value);
}).catch(callback);
Expand Down Expand Up @@ -181,10 +184,10 @@ module.exports = function (content) {
],
}).then(file => {
cache.set(cacheKey, file);
cache.set(`${cacheKey}-checksum`, {
cache.set(`${cacheKey}-checksum`, JSON.stringify({
fileHash,
resource: this.resource,
});
}));

completePromise.resolve();
callback(null, file);
Expand Down
6 changes: 3 additions & 3 deletions test/webpack.default.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var path = require('path');
const path = require('path');

module.exports = [
{
Expand All @@ -9,10 +9,10 @@ module.exports = [
output: {
path: path.join(__dirname, 'results/default'),
publicPath: '/',
filename: 'app.js'
filename: 'app.js',
},
resolve: {
extensions: ['', '.js']
extensions: ['', '.js'],
},
module: {
loaders: [
Expand Down
10 changes: 5 additions & 5 deletions test/webpack.noconvert.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var path = require('path');
const path = require('path');

module.exports = [
{
Expand All @@ -9,10 +9,10 @@ module.exports = [
output: {
path: path.join(__dirname, 'results/noconvert'),
publicPath: '/',
filename: 'app.js'
filename: 'app.js',
},
resolve: {
extensions: ['', '.js']
extensions: ['', '.js'],
},
module: {
loaders: [
Expand All @@ -27,8 +27,8 @@ module.exports = [
},
imageOptimizeLoader: {
optimizer: {
covertPngToJpg:false,
covertPngToJpg: false,
},
}
},
},
];

0 comments on commit 22bf128

Please sign in to comment.