Skip to content

Commit

Permalink
FS caching improvements (#375)
Browse files Browse the repository at this point in the history
* Instantiate default cache directory lazily, and only once

When using `cacheDirectory=true`, the `findCacheDir` module
is repeatedly called, and it does some synchronous filesystem
calls under the hood. However, since `findCacheDir` is more
or less a pure function, it's probably safe to call it only
once during process lifetime.

* Cache resolve-rc results
  • Loading branch information
akx authored and danez committed Feb 12, 2017
1 parent fd090c9 commit f7e69ab
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/fs-cache.js
Expand Up @@ -15,6 +15,8 @@ const os = require("os");
const path = require("path");
const zlib = require("zlib");

let defaultCacheDirectory = null; // Lazily instantiated when needed

/**
* Read the contents from the compressed file.
*
Expand Down Expand Up @@ -161,13 +163,17 @@ const handleCache = function(directory, params, callback) {
*
* });
*/

module.exports = function(params, callback) {
let directory;

if (typeof params.directory === "string") {
directory = params.directory;
} else {
directory = findCacheDir({ name: "babel-loader" }) || os.tmpdir();
if (defaultCacheDirectory === null) {
defaultCacheDirectory = findCacheDir({ name: "babel-loader" }) || os.tmpdir();
}
directory = defaultCacheDirectory;
}

handleCache(directory, params, callback);
Expand Down
9 changes: 7 additions & 2 deletions src/resolve-rc.js
Expand Up @@ -10,6 +10,8 @@ const path = require("path");
const exists = require("./utils/exists")({});
const read = require("./utils/read")({});

const cache = {};

const find = function find(start, rel) {
const file = path.join(start, rel);

Expand All @@ -27,6 +29,9 @@ const find = function find(start, rel) {

module.exports = function(loc, rel) {
rel = rel || ".babelrc";

return find(loc, rel);
const cacheKey = `${loc}/${rel}`;
if (!(cacheKey in cache)) {
cache[cacheKey] = find(loc, rel);
}
return cache[cacheKey];
};

0 comments on commit f7e69ab

Please sign in to comment.