resolve.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var fs = require('fs');
  2. var path = require('path');
  3. var mdeps = require('../');
  4. var test = require('tap').test;
  5. var nodeResolve = require('resolve');
  6. var browserResolve = require('browser-resolve');
  7. var missing = path.join(__dirname, '/missing');
  8. var files = {
  9. foo: path.join(__dirname, '/files/resolve/foo/foo.js'),
  10. bar: path.join(__dirname, '/files/resolve/bar/bar.js'),
  11. bar2: path.join(__dirname, '/files/resolve/bar/bar2.js'),
  12. baz: path.join(__dirname, '/files/resolve/foo/baz/baz.js')
  13. };
  14. var sources = Object.keys(files)
  15. .reduce(function (acc, file) {
  16. acc[file] = fs.readFileSync(files[file], 'utf8');
  17. return acc;
  18. }, {});
  19. var expectedRows = [
  20. {
  21. "deps": {},
  22. "file": files.baz,
  23. "id": files.baz,
  24. "source": sources.baz
  25. },
  26. {
  27. "deps": {},
  28. "file": files.bar2,
  29. "id": files.bar2,
  30. "source": sources.bar2
  31. },
  32. {
  33. "deps": {
  34. "./bar2": files.bar2
  35. },
  36. "file": files.bar,
  37. "id": files.bar,
  38. "source": sources.bar
  39. },
  40. {
  41. "deps": {
  42. "../bar/bar.js": files.bar,
  43. "./baz/baz.js": files.baz
  44. },
  45. "entry": true,
  46. "file": files.foo,
  47. "id": "foo",
  48. "source": sources.foo
  49. }
  50. ];
  51. test('browser resolve - missing', function (t) {
  52. t.plan(1);
  53. var d = mdeps({resolve: browserResolve});
  54. d.end({id: 'missing', file: missing, entry: true});
  55. d.on('end', function () {
  56. t.fail('errored');
  57. });
  58. d.on('error', function (err) {
  59. t.match(
  60. String(err),
  61. /Cannot find module .*/
  62. );
  63. });
  64. });
  65. test('node resolve - missing', function (t) {
  66. t.plan(1);
  67. var d = mdeps({resolve: nodeResolve});
  68. d.end({id: 'missing', file: missing, entry: true});
  69. d.on('end', function () {
  70. t.fail('errored');
  71. });
  72. d.on('error', function (err) {
  73. t.match(
  74. String(err),
  75. /Cannot find module .*/
  76. );
  77. });
  78. });
  79. test('browser resolve', function (t) {
  80. t.plan(1);
  81. var d = mdeps({resolve: browserResolve});
  82. d.end({id: 'foo', file: files.foo, entry: true});
  83. var rows = [];
  84. d.on('data', function (row) {rows.push(row)});
  85. d.on('end', function () {
  86. t.same(rows, expectedRows);
  87. });
  88. d.on('error', function () {
  89. t.fail('errored');
  90. });
  91. });
  92. test('node resolve', function (t) {
  93. t.plan(1);
  94. var d = mdeps({resolve: nodeResolve});
  95. d.end({id: 'foo', file: files.foo, entry: true});
  96. var rows = [];
  97. d.on('data', function (row) {rows.push(row)});
  98. d.on('end', function () {
  99. t.same(rows, expectedRows);
  100. });
  101. d.on('error', function () {
  102. t.fail('errored');
  103. });
  104. });