Use recursive ctime to determine plugin cache expiration

Summary:
Disabling `atime` on Linux is quite common. (I don't have data to back this up,
but with my sample size of n=1, 100% fall into this bucket.) In that case,
the plugins will be cached indefinitely.

Using `ctime` on the directory doesn't really mean anything because it is
only affected by changes *to* the directory, not the files inside.

So, let's do this right and use the most recent change to any of the files
*inside* the directory instead.

Reviewed By: danielbuechele

Differential Revision: D9479491

fbshipit-source-id: 6945d7bf87defa67679cacdaf0a978d8ff1770c3
This commit is contained in:
Pascal Hartig
2018-08-23 04:46:03 -07:00
committed by Facebook Github Bot
parent 624d06f2c2
commit 715d12db8d
3 changed files with 58 additions and 38 deletions

View File

@@ -8,6 +8,8 @@
const path = require('path');
const fs = require('fs');
const metro = require('metro');
const util = require('util');
const recursiveReaddir = require('recursive-readdir');
const HOME_DIR = require('os').homedir();
module.exports = (reloadCallback, pluginPaths, pluginCache) => {
@@ -135,6 +137,15 @@ function changeExport(path) {
);
fs.writeFileSync(path, file);
}
function mostRecentlyChanged(dir) {
return util
.promisify(recursiveReaddir)(dir)
.then(files =>
files
.map(f => fs.lstatSync(f).ctime)
.reduce((a, b) => (a > b ? a : b), new Date(0)),
);
}
function compilePlugin(
{rootDir, name, entry, packageJSON},
force,
@@ -145,41 +156,43 @@ function compilePlugin(
const out = path.join(pluginCache, fileName);
const result = Object.assign({}, packageJSON, {rootDir, name, entry, out});
// check if plugin needs to be compiled
if (
!force &&
fs.existsSync(out) &&
fs.lstatSync(rootDir).atime < fs.lstatSync(out).ctime
) {
// eslint-disable-next-line no-console
console.log(`🥫 Using cached version of ${name}...`);
resolve(result);
} else {
console.log(`⚙️ Compiling ${name}...`); // eslint-disable-line no-console
metro
.runBuild({
config: {
getProjectRoots: () => [rootDir, path.join(__dirname, '..')],
getTransformModulePath: () =>
path.join(__dirname, 'transforms', 'index.js'),
// a custom hash function is required, because by default metro starts
// numbering the modules by 1. This means all plugins would start at
// ID 1, which causes a clash. This is why we have a custom IDFactory.
createModuleIdFactory,
},
dev: false,
entry,
out,
})
.then(() => {
changeExport(out);
resolve(result);
})
.catch(err => {
console.error(
`❌ Plugin ${name} is ignored, because it could not be compiled.`,
);
console.error(err);
});
}
mostRecentlyChanged(rootDir).then(rootDirCtime => {
if (
!force &&
fs.existsSync(out) &&
rootDirCtime < fs.lstatSync(out).ctime
) {
// eslint-disable-next-line no-console
console.log(`🥫 Using cached version of ${name}...`);
resolve(result);
} else {
console.log(`⚙️ Compiling ${name}...`); // eslint-disable-line no-console
metro
.runBuild({
config: {
getProjectRoots: () => [rootDir, path.join(__dirname, '..')],
getTransformModulePath: () =>
path.join(__dirname, 'transforms', 'index.js'),
// a custom hash function is required, because by default metro starts
// numbering the modules by 1. This means all plugins would start at
// ID 1, which causes a clash. This is why we have a custom IDFactory.
createModuleIdFactory,
},
dev: false,
entry,
out,
})
.then(() => {
changeExport(out);
resolve(result);
})
.catch(err => {
console.error(
`❌ Plugin ${name} is ignored, because it could not be compiled.`,
);
console.error(err);
});
}
});
});
}