bare.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. var test = require('tap').test;
  2. var spawn = require('child_process').spawn;
  3. var browserify = require('../');
  4. var path = require('path');
  5. var concat = require('concat-stream');
  6. var vm = require('vm');
  7. var fs = require('fs');
  8. var through = require('through2');
  9. var temp = require('temp');
  10. temp.track();
  11. var tmpdir = temp.mkdirSync({prefix: 'browserify-test'});
  12. test('bare', function (t) {
  13. t.plan(4);
  14. var cwd = process.cwd();
  15. process.chdir(__dirname);
  16. var ps = spawn(process.execPath, [
  17. path.resolve(__dirname, '../bin/cmd.js'),
  18. '-', '--bare'
  19. ]);
  20. ps.stdout.pipe(concat(function (body) {
  21. vm.runInNewContext(body, {
  22. Buffer: function (s) { return s.toLowerCase() },
  23. console: {
  24. log: function (msg) { t.equal(msg, 'abc') }
  25. }
  26. });
  27. vm.runInNewContext(body, {
  28. Buffer: Buffer,
  29. console: {
  30. log: function (msg) {
  31. t.ok(Buffer.isBuffer(msg));
  32. t.equal(msg.toString('utf8'), 'ABC')
  33. }
  34. }
  35. });
  36. }));
  37. ps.stdin.end('console.log(Buffer("ABC"))');
  38. ps.on('exit', function (code) {
  39. t.equal(code, 0);
  40. });
  41. });
  42. test('bare api', function (t) {
  43. t.plan(3);
  44. var input = through();
  45. var b = browserify(input, { bare: true });
  46. b.bundle().pipe(concat(function (body) {
  47. vm.runInNewContext(body, {
  48. Buffer: function (s) { return s.toLowerCase() },
  49. console: {
  50. log: function (msg) { t.equal(msg, 'abc') }
  51. }
  52. });
  53. vm.runInNewContext(body, {
  54. Buffer: Buffer,
  55. console: {
  56. log: function (msg) {
  57. t.ok(Buffer.isBuffer(msg));
  58. t.equal(msg.toString('utf8'), 'ABC')
  59. }
  60. }
  61. });
  62. }));
  63. input.end('console.log(Buffer("ABC"))');
  64. });
  65. test('bare inserts __filename,__dirname but not process,global,Buffer', function (t) {
  66. t.plan(2);
  67. var file = path.resolve(__dirname, 'bare/main.js');
  68. var ps = spawn(process.execPath, [
  69. path.resolve(__dirname, '../bin/cmd.js'),
  70. file,
  71. '--bare'
  72. ]);
  73. ps.stdout.pipe(concat(function (body) {
  74. vm.runInNewContext(body, {
  75. require: require,
  76. __dirname: process.cwd(),
  77. console: {
  78. log: function (msg) {
  79. t.same(msg, [
  80. path.join(__dirname, 'bare'),
  81. path.join(__dirname, 'bare/main.js'),
  82. 'undefined',
  83. 'undefined',
  84. 'undefined'
  85. ]);
  86. }
  87. }
  88. });
  89. }));
  90. ps.stdin.end();
  91. ps.on('exit', function (code) {
  92. t.equal(code, 0);
  93. });
  94. });
  95. test('bare inserts dynamic __filename,__dirname', function (t) {
  96. t.plan(2);
  97. var file = path.join(tmpdir, 'dirname-filename.js');
  98. fs.writeFileSync(
  99. file,
  100. fs.readFileSync(path.resolve(__dirname, 'bare/dirname-filename.js'))
  101. );
  102. var ps = spawn(process.execPath, [
  103. path.resolve(__dirname, '../bin/cmd.js'),
  104. file,
  105. '--bare'
  106. ]);
  107. ps.stdout.pipe(concat(function (body) {
  108. vm.runInNewContext(body, {
  109. require: require,
  110. __dirname: process.cwd(),
  111. console: {
  112. log: function (msg) {
  113. t.same(msg, [
  114. path.dirname(file),
  115. file
  116. ]);
  117. }
  118. }
  119. });
  120. }));
  121. ps.stdin.end();
  122. ps.on('exit', function (code) {
  123. t.equal(code, 0);
  124. });
  125. });
  126. test('bare inserts dynamic __filename,__dirname with basedir', function (t) {
  127. t.plan(2);
  128. var file = 'dirname-filename.js';
  129. var ps = spawn(process.execPath, [
  130. path.resolve(__dirname, '../bin/cmd.js'),
  131. file,
  132. '--bare',
  133. '--basedir=' + path.join(__dirname, 'bare')
  134. ]);
  135. ps.stdout.pipe(concat(function (body) {
  136. vm.runInNewContext(body, {
  137. require: require,
  138. __dirname: process.cwd(),
  139. console: {
  140. log: function (msg) {
  141. t.same(msg, [
  142. __dirname,
  143. path.join(__dirname, file)
  144. ]);
  145. }
  146. }
  147. });
  148. }));
  149. ps.stdin.end();
  150. ps.on('exit', function (code) {
  151. t.equal(code, 0);
  152. });
  153. });