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

if _options.basedir exists, use it in external, exclude and ignore #1551

Open
wants to merge 1 commit 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
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,23 +252,23 @@ Browserify.prototype.external = function (file, opts) {
}

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

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

Browserify.prototype.ignore = function (file, opts) {
if (!opts) opts = {};
var basedir = defined(opts.basedir, process.cwd());
var basedir = defined(opts.basedir, this._options.basedir, process.cwd());

// Handle relative paths
if (file[0] === '.') {
Expand Down
39 changes: 39 additions & 0 deletions test/external_basedir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var browserify = require('../');
var vm = require('vm');
var test = require('tap').test;

test('external', function (t) {
t.plan(2);
var baseDir = __dirname;
var splitFile = baseDir + '/external_basedir/y.js';
var mainSrc = '';
var splitSrc = '';
var pending = 2;

// main bundle should not include splitFile
var b = browserify(baseDir + '/external_basedir/main.js', {basedir: baseDir});
b.external(splitFile); // no need to specify basedir option if it is already set for b
b.bundle(function (err, src) {
if (err) return t.fail(err);
mainSrc = src;
pending--;
done();
});

// consider b2 as for split or vendor bundle
var b2 = browserify([], {basedir: baseDir});
b2.require(splitFile, {expose: splitFile.replace(baseDir, '')});
b2.bundle(function (err, src) {
if (err) return t.fail(err);
splitSrc = src;
pending--;
done();
});

function done() {
if (pending !== 0) {
return;
}
vm.runInNewContext(splitSrc + mainSrc, { t: t });
}
});
2 changes: 2 additions & 0 deletions test/external_basedir/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
t.equal(require('./x'), 'foo');
t.equal(require('./y'), 'bar');
1 change: 1 addition & 0 deletions test/external_basedir/x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'foo';
1 change: 1 addition & 0 deletions test/external_basedir/y.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'bar';