Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use memoized path.relative #1544

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
111 changes: 56 additions & 55 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ inherits(Browserify, EventEmitter);

var fs = require('fs');
var path = require('path');
var relativePath = require('cached-path-relative');
var paths = {
empty: path.join(__dirname, 'lib/_empty.js')
};
Expand All @@ -37,19 +38,19 @@ function Browserify (files, opts) {
var self = this;
if (!(this instanceof Browserify)) return new Browserify(files, opts);
if (!opts) opts = {};

if (typeof files === 'string' || isarray(files) || isStream(files)) {
opts = xtend(opts, { entries: [].concat(opts.entries || [], files) });
}
else opts = xtend(files, opts);

self._options = opts;
if (opts.noparse) opts.noParse = opts.noparse;

if (opts.basedir !== undefined && typeof opts.basedir !== 'string') {
throw new Error('opts.basedir must be either undefined or a string.');
}

self._external = [];
self._exclude = [];
self._ignore = [];
Expand Down Expand Up @@ -79,20 +80,20 @@ function Browserify (files, opts) {
};

self.pipeline = self._createPipeline(opts);

[].concat(opts.transform).filter(Boolean).filter(self._filterTransform)
.forEach(function (tr) {
self.transform(tr);
});

[].concat(opts.entries).filter(Boolean).forEach(function (file) {
self.add(file, { basedir: opts.basedir });
});

[].concat(opts.require).filter(Boolean).forEach(function (file) {
self.require(file, { basedir: opts.basedir });
});

[].concat(opts.plugin).filter(Boolean).forEach(function (p) {
self.plugin(p, { basedir: opts.basedir });
});
Expand All @@ -109,22 +110,22 @@ Browserify.prototype.require = function (file, opts) {
});
return this;
}

if (!opts) opts = {};
var basedir = defined(opts.basedir, self._options.basedir, process.cwd());
var expose = opts.expose;
if (file === expose && /^[\.]/.test(expose)) {
expose = '/' + path.relative(basedir, expose);
expose = '/' + relativePath(basedir, expose);
expose = expose.replace(/\\/g, '/');
}
if (expose === undefined && this._options.exposeAll) {
expose = true;
}
if (expose === true) {
expose = '/' + path.relative(basedir, file);
expose = '/' + relativePath(basedir, file);
expose = expose.replace(/\\/g, '/');
}

if (isStream(file)) {
self._pending ++;
var order = self._entryOrder ++;
Expand All @@ -149,12 +150,12 @@ Browserify.prototype.require = function (file, opts) {
if (rec.entry) rec.order = order;
if (rec.transform === false) rec.transform = false;
self.pipeline.write(rec);

if (-- self._pending === 0) self.emit('_ready');
}));
return this;
}

var row;
if (typeof file === 'object') {
row = xtend(file, opts);
Expand All @@ -166,7 +167,7 @@ Browserify.prototype.require = function (file, opts) {
else {
row = xtend(opts, { file: path.resolve(basedir, file) });
}

if (!row.id) {
row.id = expose || row.file;
}
Expand All @@ -175,16 +176,16 @@ Browserify.prototype.require = function (file, opts) {
// resolves the pathname.
row.expose = row.id;
}

if (opts.external) return self.external(file, opts);
if (row.entry === undefined) row.entry = false;

if (!row.entry && self._options.exports === undefined) {
self._bpack.hasExports = true;
}

if (row.entry) row.order = self._entryOrder ++;

if (opts.transform === false) row.transform = false;
self.pipeline.write(row);
return self;
Expand Down Expand Up @@ -250,19 +251,19 @@ Browserify.prototype.external = function (file, opts) {
});
return this;
}

if (!opts) opts = {};
var basedir = defined(opts.basedir, process.cwd());
this._external.push(file);
this._external.push('/' + path.relative(basedir, file));
this._external.push('/' + relativePath(basedir, file));
return this;
};

Browserify.prototype.exclude = function (file, opts) {
if (!opts) opts = {};
var basedir = defined(opts.basedir, process.cwd());
this._exclude.push(file);
this._exclude.push('/' + path.relative(basedir, file));
this._exclude.push('/' + relativePath(basedir, file));
return this;
};

Expand All @@ -289,7 +290,7 @@ Browserify.prototype.transform = function (tr, opts) {
opts = tr[1];
tr = tr[0];
}

//if the bundler is ignoring this transform
if (typeof tr === 'string' && !self._filterTransform(tr)) {
return this;
Expand All @@ -308,10 +309,10 @@ Browserify.prototype.transform = function (tr, opts) {
}
}
}

if (!opts) opts = {};
opts._flags = '_flags' in opts ? opts._flags : self._options;

var basedir = defined(opts.basedir, this._options.basedir, process.cwd());
var order = self._transformOrder ++;
self._pending ++;
Expand Down Expand Up @@ -377,14 +378,14 @@ Browserify.prototype._createPipeline = function (opts) {
pipeline.emit('transform', tr, file);
self.emit('transform', tr, file);
});

var dopts = {
index: !opts.fullPaths && !opts.exposeAll,
dedupe: true,
expose: this._expose
};
this._bpack = bpack(xtend(opts, { raw: true }));

var pipeline = splicer.obj([
'record', [ this._recorder() ],
'deps', [ this._mdeps ],
Expand All @@ -405,12 +406,12 @@ Browserify.prototype._createPipeline = function (opts) {
pipeline.get('deps').push(through.obj(function (row, enc, next) {
if (self._external.indexOf(row.id) >= 0) return next();
if (self._external.indexOf(row.file) >= 0) return next();

if (isAbsolutePath(row.id)) {
row.id = '/' + path.relative(basedir, row.file);
row.id = '/' + relativePath(basedir, row.file);
}
Object.keys(row.deps || {}).forEach(function (key) {
row.deps[key] = '/' + path.relative(basedir, row.deps[key]);
row.deps[key] = '/' + relativePath(basedir, row.deps[key]);
});
this.push(row);
next();
Expand Down Expand Up @@ -457,7 +458,7 @@ Browserify.prototype._createDeps = function (opts) {
};
mopts.resolve = function (id, parent, cb) {
if (self._ignore.indexOf(id) >= 0) return cb(null, paths.empty, {});

self._bresolve(id, parent, function (err, file, pkg) {
if (file && self._ignore.indexOf(file) >= 0) {
return cb(null, paths.empty, {});
Expand All @@ -471,9 +472,9 @@ Browserify.prototype._createDeps = function (opts) {
}
}
}

if (file) {
var ex = '/' + path.relative(basedir, file);
var ex = '/' + relativePath(basedir, file);
if (self._external.indexOf(ex) >= 0) {
return cb(null, ex);
}
Expand All @@ -491,7 +492,7 @@ Browserify.prototype._createDeps = function (opts) {
else cb(err, null, pkg)
});
};

if (opts.builtins === false) {
mopts.modules = {};
self._exclude.push.apply(self._exclude, Object.keys(builtins));
Expand All @@ -506,11 +507,11 @@ Browserify.prototype._createDeps = function (opts) {
mopts.modules = opts.builtins;
}
else mopts.modules = xtend(builtins);

Object.keys(builtins).forEach(function (key) {
if (!has(mopts.modules, key)) self._exclude.push(key);
});

mopts.globalTransform = [];
if (!this._bundled) {
this.once('bundle', function () {
Expand All @@ -521,21 +522,21 @@ Browserify.prototype._createDeps = function (opts) {
});
});
}

var no = [].concat(opts.noParse).filter(Boolean);
var absno = no.filter(function(x) {
return typeof x === 'string';
}).map(function (x) {
return path.resolve(basedir, x);
});

function globalTr (file) {
if (opts.detectGlobals === false) return through();

if (opts.noParse === true) return through();
if (no.indexOf(file) >= 0) return through();
if (absno.indexOf(file) >= 0) return through();

var parts = file.split('/node_modules/');
for (var i = 0; i < no.length; i++) {
if (typeof no[i] === 'function' && no[i](file)) {
Expand All @@ -548,16 +549,16 @@ Browserify.prototype._createDeps = function (opts) {
return through();
}
}

var vars = xtend({
process: function () { return "require('_process')" },
}, opts.insertGlobalVars);

if (opts.bundleExternal === false) {
vars.process = undefined;
vars.buffer = undefined;
}

return insertGlobals(file, xtend(opts, {
debug: opts.debug,
always: opts.insertGlobals,
Expand All @@ -575,7 +576,7 @@ Browserify.prototype._recorder = function (opts) {
var self = this;
var ended = false;
this._recorded = [];

if (!this._ticked) {
process.nextTick(function () {
self._ticked = true;
Expand All @@ -585,10 +586,10 @@ Browserify.prototype._recorder = function (opts) {
if (ended) stream.push(null);
});
}

var stream = through.obj(write, end);
return stream;

function write (row, enc, next) {
self._recorded.push(row);
if (self._ticked) this.push(row);
Expand Down Expand Up @@ -671,29 +672,29 @@ Browserify.prototype._dedupe = function () {
Browserify.prototype._label = function (opts) {
var self = this;
var basedir = defined(opts.basedir, process.cwd());

return through.obj(function (row, enc, next) {
var prev = row.id;

if (self._external.indexOf(row.id) >= 0) return next();
if (self._external.indexOf('/' + path.relative(basedir, row.id)) >= 0) {
if (self._external.indexOf('/' + relativePath(basedir, row.id)) >= 0) {
return next();
}
if (self._external.indexOf(row.file) >= 0) return next();

if (row.index) row.id = row.index;

self.emit('label', prev, row.id);
if (row.indexDeps) row.deps = row.indexDeps || {};

Object.keys(row.deps).forEach(function (key) {
if (self._expose[key]) {
row.deps[key] = key;
return;
}

var afile = path.resolve(path.dirname(row.file), key);
var rfile = '/' + path.relative(basedir, afile);
var rfile = '/' + relativePath(basedir, afile);
if (self._external.indexOf(rfile) >= 0) {
row.deps[key] = rfile;
}
Expand All @@ -704,7 +705,7 @@ Browserify.prototype._label = function (opts) {
row.deps[key] = key;
return;
}

for (var i = 0; i < self._extensions.length; i++) {
var ex = self._extensions[i];
if (self._external.indexOf(rfile + ex) >= 0) {
Expand All @@ -713,7 +714,7 @@ Browserify.prototype._label = function (opts) {
}
}
});

if (row.entry || row.expose) {
self._bpack.standaloneModule = row.id;
}
Expand All @@ -736,7 +737,7 @@ Browserify.prototype._debug = function (opts) {
return through.obj(function (row, enc, next) {
if (opts.debug) {
row.sourceRoot = 'file://localhost';
row.sourceFile = path.relative(basedir, row.file)
row.sourceFile = relativePath(basedir, row.file)
.replace(/\\/g, '/');
}
this.push(row);
Expand Down