react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / umd / node_modules / rfile / node_modules / resolve / lib / async.js
80762 viewsvar core = require('./core');1var fs = require('fs');2var path = require('path');34module.exports = function resolve (x, opts, cb) {5if (core[x]) return cb(null, x);67if (typeof opts === 'function') {8cb = opts;9opts = {};10}11if (!opts) opts = {};1213var isFile = opts.isFile || function (file, cb) {14fs.stat(file, function (err, stat) {15if (err && err.code === 'ENOENT') cb(null, false)16else if (err) cb(err)17else cb(null, stat.isFile() || stat.isFIFO())18});19};20var readFile = opts.readFile || fs.readFile;2122var extensions = opts.extensions || [ '.js' ];23var y = opts.basedir24|| path.dirname(require.cache[__filename].parent.filename)25;2627opts.paths = opts.paths || [];2829if (x.match(/^(?:\.\.?\/|\/|([A-Za-z]:)?\\)/)) {30loadAsFile(path.resolve(y, x), function (err, m) {31if (err) cb(err)32else if (m) cb(null, m)33else loadAsDirectory(path.resolve(y, x), function (err, d) {34if (err) cb(err)35else if (d) cb(null, d)36else cb(new Error("Cannot find module '" + x + "'"))37})38});39}40else loadNodeModules(x, y, function (err, n) {41if (err) cb(err)42else if (n) cb(null, n)43else cb(new Error("Cannot find module '" + x + "'"))44});4546function loadAsFile (x, cb) {47(function load (exts) {48if (exts.length === 0) return cb(null, undefined);49var file = x + exts[0];5051isFile(file, function (err, ex) {52if (err) cb(err)53else if (ex) cb(null, file)54else load(exts.slice(1))55});56})([''].concat(extensions));57}5859function loadAsDirectory (x, cb) {60var pkgfile = path.join(x, '/package.json');61isFile(pkgfile, function (err, ex) {62if (err) return cb(err);63if (!ex) return loadAsFile(path.join(x, '/index'), cb);6465readFile(pkgfile, function (err, body) {66if (err) return cb(err);67try {68var pkg = JSON.parse(body);69}70catch (err) {}7172if (opts.packageFilter) {73pkg = opts.packageFilter(pkg, x);74}7576if (pkg.main) {77loadAsFile(path.resolve(x, pkg.main), function (err, m) {78if (err) return cb(err);79if (m) return cb(null, m);80var dir = path.resolve(x, pkg.main);81loadAsDirectory(dir, function (err, n) {82if (err) return cb(err);83if (n) return cb(null, n);84loadAsFile(path.join(x, '/index'), cb);85});86});87return;88}8990loadAsFile(path.join(x, '/index'), cb);91});92});93}9495function loadNodeModules (x, start, cb) {96(function process (dirs) {97if (dirs.length === 0) return cb(null, undefined);98var dir = dirs[0];99100loadAsFile(path.join(dir, '/', x), function (err, m) {101if (err) return cb(err);102if (m) return cb(null, m);103loadAsDirectory(path.join(dir, '/', x), function (err, n) {104if (err) return cb(err);105if (n) return cb(null, n);106process(dirs.slice(1));107});108});109})(nodeModulesPaths(start));110}111112function nodeModulesPaths (start, cb) {113var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\/+/;114var parts = start.split(splitRe);115116var dirs = [];117for (var i = parts.length - 1; i >= 0; i--) {118if (parts[i] === 'node_modules') continue;119var dir = path.join(120path.join.apply(path, parts.slice(0, i + 1)),121'node_modules'122);123if (!parts[0].match(/([A-Za-z]:)/)) {124dir = '/' + dir;125}126dirs.push(dir);127}128return dirs.concat(opts.paths);129}130};131132133