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

Fix expose field for external modules and builtins #1202

Merged
1 commit merged into from Apr 9, 2015
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
19 changes: 11 additions & 8 deletions index.js
Expand Up @@ -143,14 +143,17 @@ Browserify.prototype.require = function (file, opts) {
}));
return this;
}

var row = typeof file === 'object'
? xtend(file, opts)
: (isExternalModule(file)
? xtend(opts, { id: expose || file })
: xtend(opts, { file: file })
)
;

var row;
if (typeof file === 'object') {
row = xtend(file, opts);
} else if (isExternalModule(file)) {
// external module or builtin
row = xtend(opts, { id: expose || file, file: file });
} else {
row = xtend(opts, { file: file });
};

if (!row.id) {
row.id = expose || file;
}
Expand Down
26 changes: 26 additions & 0 deletions test/require_expose.js
Expand Up @@ -2,6 +2,32 @@ var browserify = require('../');
var test = require('tap').test;
var vm = require('vm');

test('require expose external module', function (t) {
t.plan(2);

var b = browserify({ basedir: __dirname });
b.require('beep', { expose: 'bip' });
b.bundle(function (err, src) {
t.ifError(err);
var c = { };
vm.runInNewContext(src, c);
t.equal(c.require('bip'), 'boop');
})
});

test('renaming builtin', function (t) {
t.plan(2);

var b = browserify({ basedir: __dirname });
b.require('os', { expose: 'bone' });
b.bundle(function (err, src) {
t.ifError(err);
var c = { };
vm.runInNewContext(src, c);
t.equal(c.require('bone').platform(), 'browser');
})
});

test('exposed modules do not leak across bundles', function (t) {
var bundle1, bundle2;

Expand Down