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

View File

@@ -12,6 +12,7 @@
"babel-plugin-transform-object-rest-spread": "^7.0.0-beta.3", "babel-plugin-transform-object-rest-spread": "^7.0.0-beta.3",
"babel-preset-react": "^7.0.0-beta.3", "babel-preset-react": "^7.0.0-beta.3",
"babylon": "^7.0.0-beta.40", "babylon": "^7.0.0-beta.40",
"metro": "^0.28.0" "metro": "^0.28.0",
"recursive-readdir": "2.2.2"
} }
} }

View File

@@ -2252,7 +2252,7 @@ min-document@^2.19.0:
dependencies: dependencies:
dom-walk "^0.1.0" dom-walk "^0.1.0"
minimatch@^3.0.0, minimatch@^3.0.2, minimatch@^3.0.4: minimatch@3.0.4, minimatch@^3.0.0, minimatch@^3.0.2, minimatch@^3.0.4:
version "3.0.4" version "3.0.4"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083" resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
dependencies: dependencies:
@@ -2574,6 +2574,12 @@ readable-stream@^2.0.1, readable-stream@^2.0.6, readable-stream@^2.1.4, readable
string_decoder "~1.0.3" string_decoder "~1.0.3"
util-deprecate "~1.0.1" util-deprecate "~1.0.1"
recursive-readdir@2.2.2:
version "2.2.2"
resolved "https://registry.yarnpkg.com/recursive-readdir/-/recursive-readdir-2.2.2.tgz#9946fb3274e1628de6e36b2f6714953b4845094f"
dependencies:
minimatch "3.0.4"
regenerate@^1.2.1: regenerate@^1.2.1:
version "1.3.3" version "1.3.3"
resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.3.3.tgz#0c336d3980553d755c39b586ae3b20aa49c82b7f" resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.3.3.tgz#0c336d3980553d755c39b586ae3b20aa49c82b7f"