var fs = require("fs")
var path = require("path")
var resolve = require("resolve")

var moduleDirectories = [
  "web_modules",
  "node_modules",
]

function isFile(path) {
  return new Promise(function(resolve, reject) {
    fs.stat(path, function(err, stat) {
      if (err) {
        if (err.code === "ENOENT") {
          return resolve(false)
        }
        return reject(err)
      }
      resolve(stat.isFile() || stat.isFIFO())
    })
  })
}

function resolveModule(id, opts) {
  return new Promise(function(res, rej) {
    resolve(id, opts, function(err, path) {
      if (err) {
        return rej(err)
      }
      res(path)
    })
  })
}

module.exports = function(id, base, options) {
  var paths = options.path

  var resolveOpts = {
    basedir: base,
    moduleDirectory: moduleDirectories,
    paths: paths,
    extensions: [ ".css" ],
    packageFilter: function processPackage(pkg) {
      if (pkg.style) {
        pkg.main = pkg.style
      }
      else if (!pkg.main || !/\.css$/.test(pkg.main)) {
        pkg.main = "index.css"
      }
      return pkg
    },
  }

  // local detect
  return resolveModule("./" + id, resolveOpts).catch(function() {
    // modules and paths detect
    return resolveModule(id, resolveOpts)
  }).catch(function() {
    // LAST HOPE
    return Promise.all(paths.map(function(p) {
      return isFile(path.resolve(p, id))
    })).then(function(results) {
      for (var i = 0; i < results.length; i += 1) {
        if (results[i]) {
          return path.resolve(paths[i], id)
        }
      }

      if (paths.indexOf(base) === -1) {
        paths.unshift(base)
      }

      throw new Error([
        "Failed to find '" + id + "'",
        "in [ ",
        "    " + paths.join(",\n        "),
        "]",
      ].join("\n    "))
    })
  })
}

@@ -1,54 +1,4 @@
-var fs = require(%22fs%22)%0Avar path = require(%22path%22)%0A
 var 
@@ -95,313 +95,8 @@
 %0A%5D%0A%0A
-function isFile(path) %7B%0A  return new Promise(function(resolve, reject) %7B%0A    fs.stat(path, function(err, stat) %7B%0A      if (err) %7B%0A        if (err.code === %22ENOENT%22) %7B%0A          return resolve(false)%0A        %7D%0A        return reject(err)%0A      %7D%0A      resolve(stat.isFile() %7C%7C stat.isFIFO())%0A    %7D)%0A  %7D)%0A%7D%0A%0A
 func
@@ -732,26 +732,8 @@
  %7D%0A%0A
-  // local detect%0A
   re
@@ -774,16 +774,19 @@
 lveOpts)
+%0A  
 .catch(f
@@ -801,40 +801,8 @@
 ) %7B%0A
-    // modules and paths detect%0A
     
@@ -843,16 +843,19 @@
 ts)%0A  %7D)
+%0A  
 .catch(f
@@ -870,289 +870,8 @@
 ) %7B%0A
-    // LAST HOPE%0A    return Promise.all(paths.map(function(p) %7B%0A      return isFile(path.resolve(p, id))%0A    %7D)).then(function(results) %7B%0A      for (var i = 0; i %3C results.length; i += 1) %7B%0A        if (results%5Bi%5D) %7B%0A          return path.resolve(paths%5Bi%5D, id)%0A        %7D%0A      %7D%0A%0A  
     
@@ -910,18 +910,16 @@
 %7B%0A      
- 
- 
 paths.un
@@ -930,18 +930,16 @@
 t(base)%0A
-  
     %7D%0A%0A 
@@ -941,18 +941,16 @@
  %7D%0A%0A    
-  
 throw ne
@@ -965,18 +965,16 @@
 %5B%0A      
- 
- 
 %22Failed 
@@ -1002,18 +1002,16 @@
 ,%0A      
-  
 %22in %5B %22,
@@ -1011,18 +1011,16 @@
 in %5B %22,%0A
-  
       %22 
@@ -1063,17 +1063,13 @@
     
-  
 %22%5D%22,%0A
-  
     
@@ -1086,22 +1086,15 @@
     %22))%0A
-    %7D)%0A
   %7D)%0A%7D%0A

Remove dead code