global.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. var test = require('tape');
  2. var vm = require('vm');
  3. var concat = require('concat-stream');
  4. var path = require('path');
  5. var insert = require('../');
  6. var bpack = require('browser-pack');
  7. var mdeps = require('module-deps');
  8. test('insert globals', function (t) {
  9. var expected = [ 'global' ];
  10. t.plan(2 + expected.length);
  11. var deps = mdeps()
  12. var pack = bpack({ raw: true });
  13. deps.pipe(pack);
  14. pack.pipe(concat(function (src) {
  15. var c = {
  16. t : t,
  17. a : 555,
  18. };
  19. c.self = c;
  20. vm.runInNewContext(src, c);
  21. }));
  22. deps.write({
  23. transform: function (file) {
  24. var tr = inserter(file)
  25. tr.on('global', function (name) {
  26. t.equal(name, expected.shift());
  27. });
  28. return tr;
  29. },
  30. global: true
  31. });
  32. deps.end(__dirname + '/global/main.js');
  33. });
  34. test('__filename and __dirname', function (t) {
  35. t.plan(2);
  36. var file = path.join(__dirname, 'global', 'filename.js');
  37. var deps = mdeps()
  38. var pack = bpack({ raw: true });
  39. deps.pipe(pack);
  40. pack.pipe(concat(function (src) {
  41. var c = {};
  42. vm.runInNewContext('require=' + src, c);
  43. var x = c.require(file);
  44. t.equal(x.filename, '/filename.js');
  45. t.equal(x.dirname, '/');
  46. }));
  47. deps.write({ transform: inserter, global: true });
  48. deps.end(file);
  49. });
  50. function inserter (file) {
  51. return insert(file, { basedir: __dirname + '/global' });
  52. }