reverse_multi_bundle.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * To be able to lazy load bundles with script loaders the loaded bundles
  3. * must have access to modules exposed by previous bundles.
  4. *
  5. * In effect this is the same as adding the bundles in reverse order
  6. **/
  7. var browserify = require('../');
  8. var vm = require('vm');
  9. var test = require('tap').test;
  10. test('reverse multi bundle', function (t) {
  11. t.plan(5);
  12. // Main app bundle has the main app code and the shared library code
  13. var app = browserify([__dirname + '/reverse_multi_bundle/app.js'])
  14. .external(__dirname + '/reverse_multi_bundle/lazy.js')
  15. .require(__dirname + '/reverse_multi_bundle/shared.js', { expose: true })
  16. .require(__dirname + '/reverse_multi_bundle/arbitrary.js', {expose: 'not/real'});
  17. // Lazily loaded bundle has only its own code even it uses code from the
  18. // shared library.
  19. var lazy = browserify({
  20. filter: function (id) {
  21. return id !== 'not/real';
  22. }
  23. })
  24. .require(__dirname + '/reverse_multi_bundle/lazy.js', { expose: true })
  25. .external(__dirname + '/reverse_multi_bundle/shared.js')
  26. .external('not/real');
  27. app.bundle(function (err, appSrc) {
  28. if (err) throw err;
  29. lazy.bundle(function(err, lazySrc) {
  30. if (err) throw err;
  31. var src = appSrc + ';' + lazySrc;
  32. var c = {
  33. setTimeout: setTimeout,
  34. clearTimeout: clearTimeout,
  35. t: t
  36. };
  37. vm.runInNewContext(src, c);
  38. });
  39. });
  40. });