react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / resolve / lib / async.js
80724 viewsvar core = require('./core');1var fs = require('fs');2var path = require('path');3var caller = require('./caller.js');4var nodeModulesPaths = require('./node-modules-paths.js');56module.exports = function resolve (x, opts, cb) {7if (typeof opts === 'function') {8cb = opts;9opts = {};10}11if (!opts) opts = {};12if (typeof x !== 'string') {13return process.nextTick(function () {14cb(new Error('path must be a string'));15});16}1718var isFile = opts.isFile || function (file, cb) {19fs.stat(file, function (err, stat) {20if (err && err.code === 'ENOENT') cb(null, false)21else if (err) cb(err)22else cb(null, stat.isFile() || stat.isFIFO())23});24};25var readFile = opts.readFile || fs.readFile;2627var extensions = opts.extensions || [ '.js' ];28var y = opts.basedir || path.dirname(caller());2930opts.paths = opts.paths || [];3132if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[\\\/])/.test(x)) {33var res = path.resolve(y, x);34if (x === '..') res += '/';35loadAsFile(res, function (err, m, pkg) {36if (err) cb(err)37else if (m) cb(null, m, pkg)38else loadAsDirectory(path.resolve(y, x), function (err, d, pkg) {39if (err) cb(err)40else if (d) cb(null, d, pkg)41else cb(new Error("Cannot find module '" + x + "' from '" + y + "'"))42})43});44}45else loadNodeModules(x, y, function (err, n, pkg) {46if (err) cb(err)47else if (n) cb(null, n, pkg)48else if (core[x]) return cb(null, x);49else cb(new Error("Cannot find module '" + x + "' from '" + y + "'"))50});5152function loadAsFile (x, pkg, cb) {53if (typeof pkg === 'function') {54cb = pkg;55pkg = opts.package;56}5758(function load (exts) {59if (exts.length === 0) return cb(null, undefined, pkg);60var file = x + exts[0];6162isFile(file, function (err, ex) {63if (err) cb(err)64else if (ex) cb(null, file, pkg)65else load(exts.slice(1))66});67})([''].concat(extensions));68}6970function loadAsDirectory (x, fpkg, cb) {71if (typeof fpkg === 'function') {72cb = fpkg;73fpkg = opts.package;74}7576var pkgfile = path.join(x, '/package.json');77isFile(pkgfile, function (err, ex) {78if (err) return cb(err);79if (!ex) return loadAsFile(path.join(x, '/index'), fpkg, cb);8081readFile(pkgfile, function (err, body) {82if (err) return cb(err);83try {84var pkg = JSON.parse(body);85}86catch (err) {}8788if (opts.packageFilter) {89pkg = opts.packageFilter(pkg, x);90}9192if (pkg.main) {93if (pkg.main === '.' || pkg.main === './'){94pkg.main = 'index'95}96loadAsFile(path.resolve(x, pkg.main), pkg, function (err, m, pkg) {97if (err) return cb(err);98if (m) return cb(null, m, pkg);99if (!pkg) return loadAsFile(path.join(x, '/index'), pkg, cb);100101var dir = path.resolve(x, pkg.main);102loadAsDirectory(dir, pkg, function (err, n, pkg) {103if (err) return cb(err);104if (n) return cb(null, n, pkg);105loadAsFile(path.join(x, '/index'), pkg, cb);106});107});108return;109}110111loadAsFile(path.join(x, '/index'), pkg, cb);112});113});114}115116function loadNodeModules (x, start, cb) {117(function process (dirs) {118if (dirs.length === 0) return cb(null, undefined);119var dir = dirs[0];120121loadAsFile(path.join(dir, '/', x), undefined, function (err, m, pkg) {122if (err) return cb(err);123if (m) return cb(null, m, pkg);124loadAsDirectory(path.join(dir, '/', x), undefined, function (err, n, pkg) {125if (err) return cb(err);126if (n) return cb(null, n, pkg);127process(dirs.slice(1));128});129});130})(nodeModulesPaths(start, opts));131}132};133134135