Skip to content

Commit

Permalink
Merge pull request #1769 from browserify/pr-1218
Browse files Browse the repository at this point in the history
 Array support for b.exclude() and b.ignore()
  • Loading branch information
goto-bus-stop committed Nov 17, 2017
2 parents a1def79 + 12f5634 commit 9fad0c2
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 3 deletions.
14 changes: 14 additions & 0 deletions index.js
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
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
@@ -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
@@ -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
@@ -0,0 +1 @@
t.fail('this file should have been skipped');
1 change: 1 addition & 0 deletions test/exclude/skip2.js
@@ -0,0 +1 @@
t.fail('this file should have been skipped');
24 changes: 21 additions & 3 deletions test/ignore.js
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
@@ -0,0 +1,2 @@
t.deepEqual(require('./skip.js'), {});
t.deepEqual(require('./skip2.js'), {});
1 change: 1 addition & 0 deletions test/ignore/skip2.js
@@ -0,0 +1 @@
t.fail('this file should have been skipped');

0 comments on commit 9fad0c2

Please sign in to comment.