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

Browser resolve direct #1887

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
86 changes: 43 additions & 43 deletions index.js
Expand Up @@ -40,7 +40,7 @@ function Browserify (files, opts) {
var self = this;
if (!(this instanceof Browserify)) return new Browserify(files, opts);
if (!opts) opts = {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please revert all these unrelated whitespace changes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sublime automatically cleans up trailing whitespaces. Not sure I understand what is the meaning of keeping them.
Bah, sad to see there is no way for my contributions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wouldn't keep them in most situations, but browserify is old and most of the code was written by people who no longer maintain it, so history tools like git blame are important for people like me who have no idea why things are the way they are a lot of the time 😅 minimal patches are valuable in that respect.

thanks for the test, i hope we can get the browser-resolve patch in soon.


if (typeof files === 'string' || isArray(files) || isStream(files)) {
opts = xtend(opts, { entries: [].concat(opts.entries || [], files) });
}
Expand All @@ -62,10 +62,10 @@ function Browserify (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.');
}
Expand Down Expand Up @@ -101,20 +101,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 @@ -131,7 +131,7 @@ 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;
Expand All @@ -144,7 +144,7 @@ Browserify.prototype.require = function (file, opts) {
if (expose === true) {
expose = '/' + relativePath(basedir, file);
}

if (isStream(file)) {
self._pending ++;
var order = self._entryOrder ++;
Expand All @@ -169,12 +169,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 @@ -186,7 +186,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 @@ -195,16 +195,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 @@ -270,7 +270,7 @@ Browserify.prototype.external = function (file, opts) {
});
return this;
}

if (!opts) opts = {};
var basedir = defined(opts.basedir, process.cwd());
this._external.push(file);
Expand Down Expand Up @@ -323,7 +323,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 @@ -342,10 +342,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 @@ -411,14 +411,14 @@ Browserify.prototype._createPipeline = function (opts) {
pipeline.emit('transform', tr, file);
self.emit('transform', tr, file);
});

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

var pipeline = splicer.obj([
'record', [ this._recorder() ],
'deps', [ this._mdeps ],
Expand All @@ -439,7 +439,7 @@ 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 = '/' + relativePath(basedir, row.file);
}
Expand Down Expand Up @@ -491,7 +491,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 @@ -505,7 +505,7 @@ Browserify.prototype._createDeps = function (opts) {
}
}
}

if (file) {
var ex = '/' + relativePath(basedir, file);
if (self._external.indexOf(ex) >= 0) {
Expand All @@ -530,7 +530,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 @@ -545,11 +545,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 @@ -560,21 +560,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.replace(/\\/g, '/').split('/node_modules/');
for (var i = 0; i < no.length; i++) {
if (typeof no[i] === 'function' && no[i](file)) {
Expand All @@ -600,11 +600,11 @@ Browserify.prototype._createDeps = function (opts) {
}
}, opts.insertGlobalVars);
}

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

if (opts.bundleExternal === false) {
vars.process = undefined;
vars.buffer = undefined;
Expand All @@ -627,7 +627,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 @@ -637,10 +637,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 @@ -723,7 +723,7 @@ 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;

Expand All @@ -732,12 +732,12 @@ Browserify.prototype._label = function (opts) {
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;
Expand All @@ -756,7 +756,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 @@ -765,7 +765,7 @@ Browserify.prototype._label = function (opts) {
}
}
});

if (row.entry || row.expose) {
self._bpack.standaloneModule = row.id;
}
Expand Down
14 changes: 13 additions & 1 deletion test/browser_field_resolve.js
Expand Up @@ -30,7 +30,7 @@ test('browser field resolve (c)', function (t) {
vm.runInNewContext(src, { console: { log: log } });
function log (x) { t.equal(x, 333) }
});

});

test('browser field resolve (d)', function (t) {
Expand Down Expand Up @@ -122,3 +122,15 @@ test('browser field resolve (l)', function (t) {
function log (x) { t.deepEqual(x, 3000) }
});
});

test('browser field resolve (m)', function (t) {
t.plan(2);
var b = browserify(__dirname + '/browser_field_resolve/m/main.js');
b.bundle(function (err, src) {
t.ifError(err);
vm.runInNewContext(src, { console: { log: log } });
function log (x) {
t.deepEqual(x, 2000)
}
});
});
2 changes: 2 additions & 0 deletions test/browser_field_resolve/m/main.js
@@ -0,0 +1,2 @@
var zzz = require('x')
console.log(zzz)
Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/browser_field_resolve/m/node_modules/x/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions test/no_builtins.js
Expand Up @@ -2,6 +2,7 @@ var browserify = require('../');
var test = require('tap').test;
var path = require('path');
var vm = require('vm');
var EOL = require('os').EOL;

test('builtins false', function (t) {
t.plan(1);
Expand All @@ -15,7 +16,7 @@ test('builtins false', function (t) {
b.bundle(function (err, src) {
var c = {
console: { log: function (msg) {
t.equal(msg, 'beep boop\n');
t.equal(msg, 'beep boop' + EOL);
} },
require: require,
__dirname: process.cwd()
Expand All @@ -34,7 +35,7 @@ test('builtins []', function (t) {
b.bundle(function (err, src) {
var c = {
console: { log: function (msg) {
t.equal(msg, 'beep boop\n');
t.equal(msg, 'beep boop' + EOL);
} },
require: require
};
Expand Down