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

Handle invalidating cache if dependency is a globAsset #1417

Merged
merged 5 commits into from
Jul 7, 2018
Merged
Show file tree
Hide file tree
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
25 changes: 17 additions & 8 deletions src/FSCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const md5 = require('./utils/md5');
const objectHash = require('./utils/objectHash');
const pkg = require('../package.json');
const logger = require('./Logger');
const glob = require('fast-glob');
const isGlob = require('is-glob');

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

async getLastModified(filename) {
if (isGlob(filename)) {
let files = await glob(filename, {
onlyFiles: true
});

return (await Promise.all(
files.map(file => fs.stat(file).then(({mtime}) => mtime.getTime()))
)).reduce((a, b) => Math.max(a, b), 0);
}
return (await fs.stat(filename)).mtime.getTime();
}

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 depPath = dep.name;
if (depPath[depPath.length - 1] === '*') {
depPath = path.dirname(depPath);
}
let stats = await fs.stat(depPath);
dep.mtime = stats.mtime.getTime();
dep.mtime = await this.getLastModified(dep.name);
}
}
}
Expand All @@ -60,8 +70,7 @@ class FSCache {
// 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) {
if ((await this.getLastModified(dep.name)) > dep.mtime) {
return false;
}
}
Expand Down
27 changes: 27 additions & 0 deletions test/fs-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,31 @@ describe('FSCache', () => {
});
});
});

it('should invalidate cache if a wildcard dependency changes', async () => {
const cache = new FSCache({cacheDir: cachePath});
const wildcardPath = path.join(inputPath, 'wildcard');
await fs.mkdirp(wildcardPath);
await ncp(__dirname + '/integration/fs', wildcardPath);
const filePath = path.join(wildcardPath, 'test.txt');

await cache.write(__filename, {
dependencies: [
{
includedInParent: true,
name: path.join(wildcardPath, '*')
}
]
});

let cached = await cache.read(__filename);
assert(cached !== null);

// delay and update dependency
await sleep(1000);
await fs.writeFile(filePath, 'world');

cached = await cache.read(__filename);
assert.equal(cached, null);
});
});