require_expose.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. var browserify = require('../');
  2. var test = require('tap').test;
  3. var vm = require('vm');
  4. test('require expose external module', function (t) {
  5. t.plan(2);
  6. var b = browserify({ basedir: __dirname });
  7. b.require('beep', { expose: 'bip' });
  8. b.bundle(function (err, src) {
  9. t.ifError(err);
  10. var c = { };
  11. vm.runInNewContext(src, c);
  12. t.equal(c.require('bip'), 'boop');
  13. })
  14. });
  15. test('renaming builtin', function (t) {
  16. t.plan(2);
  17. var b = browserify({ basedir: __dirname });
  18. b.require('os', { expose: 'bone' });
  19. b.bundle(function (err, src) {
  20. t.ifError(err);
  21. var c = { };
  22. vm.runInNewContext(src, c);
  23. t.equal(c.require('bone').platform(), 'browser');
  24. })
  25. });
  26. test('exposed modules do not leak across bundles', function (t) {
  27. var bundle1, bundle2;
  28. bundle1 = browserify();
  29. bundle1.add(__dirname + '/require_expose/main.js');
  30. bundle1.require(__dirname + '/require_expose/some_dep.js', { expose: 'foo' });
  31. bundle1.bundle(function (err, src) {
  32. if (err) t.fail(err);
  33. var c = {};
  34. vm.runInNewContext(src, c);
  35. t.equal(c.foo, 'some_dep');
  36. bundle2 = browserify();
  37. bundle2.add(__dirname + '/require_expose/main.js');
  38. bundle2.bundle(function (err) {
  39. t.ok(err && err.message.match(/Cannot find module 'foo'/), 'should fail with missing module');
  40. t.end();
  41. });
  42. });
  43. });