Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalidate cache when dependencies included in parent change #514

Merged
merged 1 commit into from
Jan 8, 2018
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
36 changes: 33 additions & 3 deletions src/FSCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const path = require('path');
const md5 = require('./utils/md5');
const objectHash = require('./utils/objectHash');
const pkg = require('../package.json');
const json5 = require('json5');

// These keys can affect the output, so if they differ, the cache should not match
const OPTION_KEYS = ['publicURL', 'minify', 'hmr'];
Expand All @@ -30,16 +29,42 @@ class FSCache {
return path.join(this.dir, hash + '.json');
}

async writeDepMtimes(data) {
// Write mtimes for each dependent file that is already compiled into this asset
for (let dep of data.dependencies) {
if (dep.includedInParent) {
let stats = await fs.stat(dep.name);
dep.mtime = stats.mtime.getTime();
}
}
}

async write(filename, data) {
try {
await this.ensureDirExists();
await this.writeDepMtimes(data);
await fs.writeFile(this.getCacheFile(filename), JSON.stringify(data));
this.invalidated.delete(filename);
} catch (err) {
console.error('Error writing to cache', err);
}
}

async checkDepMtimes(data) {
// Check mtimes for files that are already compiled into this asset
// If any of them changed, invalidate.
for (let dep of data.dependencies) {
if (dep.includedInParent) {
let stats = await fs.stat(dep.name);
if (stats.mtime > dep.mtime) {
return false;
}
}
}

return true;
}

async read(filename) {
if (this.invalidated.has(filename)) {
return null;
Expand All @@ -55,8 +80,13 @@ class FSCache {
return null;
}

let data = await fs.readFile(cacheFile);
return json5.parse(data);
let json = await fs.readFile(cacheFile);
let data = JSON.parse(json);
if (!await this.checkDepMtimes(data)) {
return null;
}

return data;
} catch (err) {
return null;
}
Expand Down