multi_bundle_unique.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var browserify = require('../');
  2. var vm = require('vm');
  3. var fs = require('fs');
  4. var path = require('path');
  5. var test = require('tap').test;
  6. var prelude = fs.readFileSync(path.join(__dirname, 'multi_bundle', '_prelude.js'), 'utf8');
  7. test('unique require', function (t) {
  8. t.plan(6);
  9. var core = browserify({
  10. externalRequireName: 'unique_require',
  11. prelude: prelude
  12. });
  13. core.require(__dirname + '/multi_bundle/b.js', { expose: './b' });
  14. var app = browserify(
  15. [__dirname + '/multi_bundle/a.js'],
  16. { prelude: prelude }
  17. );
  18. // inform this bundle that b exists in another bundle
  19. app.external('./b');
  20. core.bundle(function (err, src) {
  21. var c = {
  22. console: console,
  23. t : t,
  24. baton: {
  25. times: 0
  26. }
  27. };
  28. // loading core will cause no require to run
  29. vm.runInNewContext(src, c);
  30. t.equal(c.baton.times, 0);
  31. // loading the app will require
  32. app.bundle(function (err, src) {
  33. vm.runInNewContext(src, c);
  34. // b required for the first time
  35. t.equal(c.baton.times, 1);
  36. // running the file again
  37. // because it is using the same b, no reloading
  38. vm.runInNewContext(src, c);
  39. // b should not have been required again
  40. // because it was part of the core bundle
  41. t.equal(c.baton.times, 1);
  42. t.equal(c.unique_require('./b'), 'foo');
  43. });
  44. });
  45. });