always.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. var test = require('tape')
  2. var mdeps = require('module-deps');
  3. var bpack = require('browser-pack');
  4. var insert = require('../');
  5. var concat = require('concat-stream');
  6. var vm = require('vm');
  7. test('always true insert', function (t) {
  8. t.plan(10);
  9. var s = mdeps({
  10. modules: {
  11. buffer: require.resolve('buffer/'),
  12. timers: require.resolve('timers-browserify')
  13. }
  14. });
  15. s.pipe(bpack({ raw: true })).pipe(concat(function (src) {
  16. var c = {
  17. t: t,
  18. process: 'sandbox process',
  19. Buffer: 'sandbox Buffer',
  20. __filename: 'sandbox __filename',
  21. __dirname: 'sandbox __dirname',
  22. custom: 'sandbox custom',
  23. self: { xyz: 555 }
  24. };
  25. vm.runInNewContext(src, c);
  26. }));
  27. s.write({ transform: inserter({ always: true }), global: true });
  28. s.end(__dirname + '/always/main.js');
  29. });
  30. test('always true insert custom globals without defaults', function (t) {
  31. t.plan(7);
  32. var s = mdeps({
  33. modules: {
  34. buffer: require.resolve('buffer/'),
  35. timers: require.resolve('timers-browserify')
  36. }
  37. });
  38. s.pipe(bpack({ raw: true })).pipe(concat(function (src) {
  39. var c = {
  40. t: t,
  41. process: 'sandbox process',
  42. Buffer: 'sandbox Buffer',
  43. __filename: 'sandbox __filename',
  44. __dirname: 'sandbox __dirname',
  45. custom: 'sandbox custom',
  46. self: { xyz: 555 }
  47. };
  48. vm.runInNewContext(src, c);
  49. }));
  50. s.write({
  51. transform: inserter({ always: true, vars: {
  52. global: {},
  53. process: undefined,
  54. Buffer: undefined,
  55. __filename: undefined,
  56. __dirname: undefined,
  57. custom: function() { return '"inserted custom"' }
  58. }}),
  59. global: true
  60. });
  61. s.end(__dirname + '/always/custom_globals_without_defaults.js');
  62. });
  63. test('always truthy-but-not-true insert hidden from quick test is not really inserted; true is', function (t) {
  64. t.plan(2);
  65. var testit = function(always, expected) {
  66. var s = mdeps({
  67. modules: {
  68. buffer: require.resolve('buffer/'),
  69. timers: require.resolve('timers-browserify')
  70. }
  71. });
  72. s.pipe(bpack({ raw: true })).pipe(concat(function (src) {
  73. var c = {
  74. t: t,
  75. process: 'sandbox process',
  76. Buffer: 'sandbox Buffer',
  77. __filename: 'sandbox __filename',
  78. __dirname: 'sandbox __dirname',
  79. custom: 'sandbox custom',
  80. expected: expected,
  81. self: { xyz: 555 }
  82. };
  83. vm.runInNewContext(src, c);
  84. }));
  85. s.write({
  86. transform: inserter({ always: always, vars: {
  87. custom: function() { return '"inserted custom"' }
  88. }}),
  89. global: true
  90. })
  91. s.end(__dirname + '/always/hidden_from_quick_test.js');
  92. };
  93. var always = 'truthy', expected = 'sandbox custom';
  94. testit(always, expected);
  95. always = true; expected = 'inserted custom';
  96. testit(always, expected);
  97. });
  98. test('inserted names do not cause const name collisions', function (t) {
  99. t.plan(1);
  100. var s = mdeps({
  101. modules: {
  102. buffer: require.resolve('buffer/'),
  103. timers: require.resolve('timers-browserify')
  104. }
  105. });
  106. s.on('error', t.fail);
  107. s.pipe(bpack({ raw: true })).pipe(concat(function (src) {
  108. var c = {
  109. t: t,
  110. Buffer: 'sandbox Buffer'
  111. };
  112. vm.runInNewContext(src, c);
  113. }));
  114. s.write({
  115. transform: inserter({
  116. always: true,
  117. vars: {
  118. Buffer: function() { return '"sandbox Buffer"' }
  119. }
  120. }),
  121. global: true
  122. });
  123. s.end(__dirname + '/always/collision.js');
  124. });
  125. function inserter (opts) {
  126. return function (file) {
  127. return insert(file, opts);
  128. };
  129. }