index.js 9.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
// builtin
var fs = require('fs');
var path = require('path');

// vendor
var resv = require('resolve');

// given a path, create an array of node_module paths for it
// borrowed from substack/resolve
function nodeModulesPaths (start, cb) {
    var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\/+/;
    var parts = start.split(splitRe);

    var dirs = [];
    for (var i = parts.length - 1; i >= 0; i--) {
        if (parts[i] === 'node_modules') continue;
        var dir = path.join.apply(
            path, parts.slice(0, i + 1).concat(['node_modules'])
        );
        if (!parts[0].match(/([A-Za-z]:)/)) {
            dir = '/' + dir;
        }
        dirs.push(dir);
    }
    return dirs;
}

function find_shims_in_package(pkgJson, cur_path, shims, browser) {
    try {
        var info = JSON.parse(pkgJson);
    }
    catch (err) {
        err.message = pkgJson + ' : ' + err.message
        throw err;
    }

    var replacements = getReplacements(info, browser);

    // no replacements, skip shims
    if (!replacements) {
        return;
    }

    // if browser mapping is a string
    // then it just replaces the main entry point
    if (typeof replacements === 'string') {
        var key = path.resolve(cur_path, info.main || 'index.js');
        shims[key] = path.resolve(cur_path, replacements);
        return;
    }

    // http://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders
    Object.keys(replacements).forEach(function(key) {
        var val;
        if (replacements[key] === false) {
            val = path.normalize(__dirname + '/empty.js');
        }
        else {
            val = replacements[key];
            // if target is a relative path, then resolve
            // otherwise we assume target is a module
            if (val[0] === '.') {
                val = path.resolve(cur_path, val);
            }
        }

        if (key[0] === '/' || key[0] === '.') {
            // if begins with / ../ or ./ then we must resolve to a full path
            key = path.resolve(cur_path, key);
        }
        shims[key] = val;
    });

    [ '.js', '.json' ].forEach(function (ext) {
        Object.keys(shims).forEach(function (key) {
            if (!shims[key + ext]) {
                shims[key + ext] = shims[key];
            }
        });
    });
}

// paths is mutated
// load shims from first package.json file found
function load_shims(paths, browser, cb) {
    // identify if our file should be replaced per the browser field
    // original filename|id -> replacement
    var shims = Object.create(null);

    (function next() {
        var cur_path = paths.shift();
        if (!cur_path) {
            return cb(null, shims);
        }

        var pkg_path = path.join(cur_path, 'package.json');

        fs.readFile(pkg_path, 'utf8', function(err, data) {
            if (err) {
                // ignore paths we can't open
                // avoids an exists check
                if (err.code === 'ENOENT') {
                    return next();
                }

                return cb(err);
            }
            try {
                find_shims_in_package(data, cur_path, shims, browser);
                return cb(null, shims);
            }
            catch (err) {
                return cb(err);
            }
        });
    })();
};

// paths is mutated
// synchronously load shims from first package.json file found
function load_shims_sync(paths, browser) {
    // identify if our file should be replaced per the browser field
    // original filename|id -> replacement
    var shims = Object.create(null);
    var cur_path;

    while (cur_path = paths.shift()) {
        var pkg_path = path.join(cur_path, 'package.json');

        try {
            var data = fs.readFileSync(pkg_path, 'utf8');
            find_shims_in_package(data, cur_path, shims, browser);
            return shims;
        }
        catch (err) {
            // ignore paths we can't open
            // avoids an exists check
            if (err.code === 'ENOENT') {
                continue;
            }

            throw err;
        }
    }
    return shims;
}

function build_resolve_opts(opts, base) {
    var packageFilter = opts.packageFilter;
    var browser = normalizeBrowserFieldName(opts.browser)

    opts.basedir = base;
    opts.packageFilter = function (info, pkgdir) {
        if (packageFilter) info = packageFilter(info, pkgdir);

        var replacements = getReplacements(info, browser);

        // no browser field, keep info unchanged
        if (!replacements) {
            return info;
        }

        info[browser] = replacements;

        // replace main
        if (typeof replacements === 'string') {
            info.main = replacements;
            return info;
        }

        var replace_main = replacements[info.main || './index.js'] ||
            replacements['./' + info.main || './index.js'];

        info.main = replace_main || info.main;
        return info;
    };

    var pathFilter = opts.pathFilter;
    opts.pathFilter = function(info, resvPath, relativePath) {
        if (relativePath[0] != '.') {
            relativePath = './' + relativePath;
        }
        var mappedPath;
        if (pathFilter) {
            mappedPath = pathFilter.apply(this, arguments);
        }
        if (mappedPath) {
            return mappedPath;
        }

        var replacements = info[browser];
        if (!replacements) {
            return;
        }

        mappedPath = replacements[relativePath];
        if (!mappedPath && path.extname(relativePath) === '') {
            mappedPath = replacements[relativePath + '.js'];
            if (!mappedPath) {
                mappedPath = replacements[relativePath + '.json'];
            }
        }
        return mappedPath;
    };

    return opts;
}

function resolve(id, opts, cb) {

    // opts.filename
    // opts.paths
    // opts.modules
    // opts.packageFilter

    opts = opts || {};
    opts.filename = opts.filename || '';

    var base = path.dirname(opts.filename);

    if (opts.basedir) {
        base = opts.basedir;
    }

    var paths = nodeModulesPaths(base);

    if (opts.paths) {
        paths.push.apply(paths, opts.paths);
    }

    paths = paths.map(function(p) {
        return path.dirname(p);
    });

    // we must always load shims because the browser field could shim out a module
    load_shims(paths, opts.browser, function(err, shims) {
        if (err) {
            return cb(err);
        }

        var resid = path.resolve(opts.basedir || path.dirname(opts.filename), id);
        if (shims[id] || shims[resid]) {
            var xid = shims[id] ? id : resid;
            // if the shim was is an absolute path, it was fully resolved
            if (shims[xid][0] === '/') {
                return resv(shims[xid], build_resolve_opts(opts, base), function(err, full, pkg) {
                    cb(null, full, pkg);
                });
            }

            // module -> alt-module shims
            id = shims[xid];
        }

        var modules = opts.modules || Object.create(null);
        var shim_path = modules[id];
        if (shim_path) {
            return cb(null, shim_path);
        }

        // our browser field resolver
        // if browser field is an object tho?
        var full = resv(id, build_resolve_opts(opts, base), function(err, full, pkg) {
            if (err) {
                return cb(err);
            }

            var resolved = (shims) ? shims[full] || full : full;
            cb(null, resolved, pkg);
        });
    });
};

resolve.sync = function (id, opts) {

    // opts.filename
    // opts.paths
    // opts.modules
    // opts.packageFilter

    opts = opts || {};
    opts.filename = opts.filename || '';

    var base = path.dirname(opts.filename);

    if (opts.basedir) {
        base = opts.basedir;
    }

    var paths = nodeModulesPaths(base);

    if (opts.paths) {
        paths.push.apply(paths, opts.paths);
    }

    paths = paths.map(function(p) {
        return path.dirname(p);
    });

    // we must always load shims because the browser field could shim out a module
    var shims = load_shims_sync(paths, opts.browser);
    var resid = path.resolve(opts.basedir || path.dirname(opts.filename), id);

    if (shims[id] || shims[resid]) {
        var xid = shims[id] ? id : resid;
        // if the shim was is an absolute path, it was fully resolved
        if (shims[xid][0] === '/') {
            return resv.sync(shims[xid], build_resolve_opts(opts, base));
        }

        // module -> alt-module shims
        id = shims[xid];
    }

    var modules = opts.modules || Object.create(null);
    var shim_path = modules[id];
    if (shim_path) {
        return shim_path;
    }

    // our browser field resolver
    // if browser field is an object tho?
    var full = resv.sync(id, build_resolve_opts(opts, base));

    return (shims) ? shims[full] || full : full;
};

function normalizeBrowserFieldName(browser) {
    return browser || 'browser';
}

function getReplacements(info, browser) {
    browser = normalizeBrowserFieldName(browser);
    var replacements = info[browser] || info.browser;

    // support legacy browserify field for easier migration from legacy
    // many packages used this field historically
    if (typeof info.browserify === 'string' && !replacements) {
        replacements = info.browserify;
    }

    return replacements;
}

module.exports = resolve;