var fs = require("fs");
var path = require("path");
var compile = require("./compiler.js").compile;
var hasOwn = Object.prototype.hasOwnProperty;
var isEnabledCache = Object.create(null);

exports.compile = function (content, filename) {
  return isEnabled(filename)
    ? compile(content)
    : content;
};

function isEnabled(filename) {
  if (hasOwn.call(isEnabledCache, filename)) {
    return isEnabledCache[filename];
  }

  try {
    var stat = fs.statSync(filename);
  } catch (e) {
    return isEnabledCache[filename] = false;
  }

  if (stat.isDirectory()) {
    if (path.basename(filename) === "node_modules") {
      return isEnabledCache[filename] = false;
    }

    var pkg = readPkgInfo(filename);
    if (pkg) {
      if (hasOwn.call(pkg, "reify")) {
        // An explicit "reify": false property in package.json disables
        // reification even if "reify" is listed as a dependency.
        return isEnabledCache[filename] = !! pkg.reify;
      }

      function check(name) {
        return typeof pkg[name] === "object" &&
          hasOwn.call(pkg[name], "reify");
      }

      return isEnabledCache[filename] =
        check("dependencies") ||
        check("peerDependencies") ||
        // Use case: a package.json file may have "reify" in its
        // "devDependencies" section because it expects another package or
        // application to enable reification in production, but needs its
        // own copy of the "reify" package during development. Disabling
        // reification in production when it was enabled in development
        // would be dangerous in this case.
        check("devDependencies");
    }
  }

  var parentDir = path.dirname(filename);
  return isEnabledCache[filename] =
    parentDir !== filename &&
    isEnabled(parentDir);
}

function readPkgInfo(dir) {
  try {
    return JSON.parse(fs.readFileSync(
      path.join(dir, "package.json")
    ));

  } catch (e) {
    if (! (e instanceof SyntaxError ||
           e.code === "ENOENT")) {
      throw e;
    }
  }

  return null;
}

@@ -69,16 +69,21 @@
 quire(%22.
+./lib
 /compile
@@ -148,134 +148,1074 @@
 ty;%0A
-var isEnabledCache = Object.create(null);%0A%0Aexports.compile = function (content, filename) %7B%0A  return isEnabled(filename)%0A    ?
+%0A// Map from absolute file paths to the package.json that governs them.%0Avar pkgJsonCache = Object.create(null);%0A%0Aexports.compile = function (content, filename) %7B%0A  var pkg = getPkgJson(filename);%0A  if (pkg) %7B%0A    if (hasOwn.call(pkg, %22reify%22) && ! pkg.reify) %7B%0A      // An explicit %22reify%22: false property in package.json disables%0A      // reification even if %22reify%22 is listed as a dependency.%0A      return content;%0A    %7D%0A%0A    function check(name) %7B%0A      return typeof pkg%5Bname%5D === %22object%22 &&%0A        hasOwn.call(pkg%5Bname%5D, %22reify%22);%0A    %7D%0A%0A    if (check(%22dependencies%22) %7C%7C%0A        check(%22peerDependencies%22) %7C%7C%0A        // Use case: a package.json file may have %22reify%22 in its%0A        // %22devDependencies%22 section because it expects another package or%0A        // application to enable reification in production, but needs its%0A        // own copy of the %22reify%22 package during development. Disabling%0A        // reification in production when it was enabled in development%0A        // would be dangerous in this case.%0A        check(%22devDependencies%22)) %7B%0A      return
  com
@@ -1231,14 +1231,29 @@
 ent)
+;
 %0A    
-:
+%7D%0A  %7D%0A%0A  return
  con
@@ -1271,25 +1271,26 @@
 unction 
-isEnabled
+getPkgJson
 (filenam
@@ -1312,25 +1312,23 @@
 wn.call(
-isEnabled
+pkgJson
 Cache, f
@@ -1346,33 +1346,31 @@
 %0A    return 
-isEnabled
+pkgJson
 Cache%5Bfilena
@@ -1448,33 +1448,31 @@
 %0A    return 
-isEnabled
+pkgJson
 Cache%5Bfilena
@@ -1473,29 +1473,28 @@
 filename%5D = 
-false
+null
 ;%0A  %7D%0A%0A  if 
@@ -1579,33 +1579,31 @@
      return 
-isEnabled
+pkgJson
 Cache%5Bfilena
@@ -1608,21 +1608,20 @@
 name%5D = 
-false
+null
 ;%0A    %7D%0A
@@ -1683,918 +1683,43 @@
     
-if (hasOwn.call(pkg, %22reify%22)) %7B%0A        // An explicit %22reify%22: false property in package.json disables%0A        // reification even if %22reify%22 is listed as a dependency.%0A        return isEnabledCache%5Bfilename%5D = !! pkg.reify;%0A      %7D%0A%0A      function check(name) %7B%0A        return typeof pkg%5Bname%5D === %22object%22 &&%0A          hasOwn.call(pkg%5Bname%5D, %22reify%22);%0A      %7D%0A%0A      return isEnabledCache%5Bfilename%5D =%0A        check(%22dependencies%22) %7C%7C%0A        check(%22peerDependencies%22) %7C%7C%0A        // Use case: a package.json file may have %22reify%22 in its%0A        // %22devDependencies%22 section because it expects another package or%0A        // application to enable reification in production, but needs its%0A        // own copy of the %22reify%22 package during development. Disabling%0A        // reification in production when it was enabled in development%0A        // would be dangerous in this case.%0A        check(%22devDependencies%22)
+return pkgJsonCache%5Bfilename%5D = pkg
 ;%0A  
@@ -1778,25 +1778,23 @@
  return 
-isEnabled
+pkgJson
 Cache%5Bfi
@@ -1841,17 +1841,18 @@
     
-isEnabled
+getPkgJson
 (par

Reorganize caching compiler to allow examining package.json files.