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

Array support for b.exclude() and b.ignore() #1769

Merged
merged 2 commits into from
Nov 17, 2017
Merged
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
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ Browserify.prototype.external = function (file, opts) {

Browserify.prototype.exclude = function (file, opts) {
if (!opts) opts = {};
if (isArray(file)) {
var self = this;
file.forEach(function(file) {
self.exclude(file, opts);
});
return this;
}
var basedir = defined(opts.basedir, process.cwd());
this._exclude.push(file);
this._exclude.push('/' + relativePath(basedir, file));
Expand All @@ -271,6 +278,13 @@ Browserify.prototype.exclude = function (file, opts) {

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

// Handle relative paths
Expand Down
4 changes: 4 additions & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -538,12 +538,16 @@ from the current bundle as the bundle in `file` gets bundled.

Prevent the module name or file at `file` from showing up in the output bundle.

If `file` is an array, each item in `file` will be ignored.

Instead you will get a file with `module.exports = {}`.

## b.exclude(file)

Prevent the module name or file at `file` from showing up in the output bundle.

If `file` is an array, each item in `file` will be excluded.

If your code tries to `require()` that file it will throw unless you've provided
another mechanism for loading it.

Expand Down
21 changes: 21 additions & 0 deletions test/exclude.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var browserify = require('../');
var test = require('tap').test;
var vm = require('vm');

test('exclude array', function(t) {
t.plan(2);

var b = browserify();
b.add(__dirname + '/exclude/array.js');
b.exclude([
__dirname + '/exclude/skip.js',
__dirname + '/exclude/skip2.js'
]);

b.bundle(function (err, src) {
if (err) {
t.fail(err);
}
vm.runInNewContext(src, { t: t });
});
});
2 changes: 2 additions & 0 deletions test/exclude/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
t.throws(function () { require('./skip.js') });
t.throws(function () { require('./skip2.js') });
1 change: 1 addition & 0 deletions test/exclude/skip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
t.fail('this file should have been skipped');
1 change: 1 addition & 0 deletions test/exclude/skip2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
t.fail('this file should have been skipped');
24 changes: 21 additions & 3 deletions test/ignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,38 @@ var vm = require('vm');

test('ignore', function (t) {
t.plan(1);

var b = browserify();
b.add(__dirname + '/ignore/main.js');
b.ignore( __dirname + '/ignore/skip.js');

b.bundle(function (err, src) {
if (err) t.fail(err);
vm.runInNewContext(src, { t: t });
});
});

test('ignore array', function(t) {
t.plan(2);

var b = browserify();
b.add(__dirname + '/ignore/array.js');
b.ignore([
__dirname + '/ignore/skip.js',
__dirname + '/ignore/skip2.js'
]);

b.bundle(function (err, src) {
if (err) {
t.fail(err);
}
vm.runInNewContext(src, { t: t });
});
});

test('ignore by package or id', function (t) {
t.plan(3);

var b = browserify();
b.add(__dirname + '/ignore/by-id.js');
b.ignore('events');
Expand Down
2 changes: 2 additions & 0 deletions test/ignore/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
t.deepEqual(require('./skip.js'), {});
t.deepEqual(require('./skip2.js'), {});
1 change: 1 addition & 0 deletions test/ignore/skip2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
t.fail('this file should have been skipped');