cache_partial_expose.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. var parser = require('../');
  2. var test = require('tap').test;
  3. var fs = require('fs');
  4. var path = require('path');
  5. var xtend = require('xtend');
  6. var files = {
  7. abc: path.join(__dirname, '/expose/lib/abc.js'),
  8. xyz: path.join(__dirname, '/expose/lib/xyz.js'),
  9. foo: path.join(__dirname, '/expose/foo.js'),
  10. bar: path.join(__dirname, '/expose/bar.js'),
  11. main: path.join(__dirname, '/expose/main.js')
  12. };
  13. var sources = Object.keys(files).reduce(function (acc, file) {
  14. acc[file] = fs.readFileSync(files[file], 'utf8');
  15. return acc;
  16. }, {});
  17. var cache = {};
  18. cache[files.abc] = {
  19. source: sources.abc,
  20. deps: {}
  21. };
  22. cache[files.xyz] = {
  23. source: sources.xyz,
  24. deps: {'../foo': files.foo}
  25. };
  26. cache[files.foo] = {
  27. source: sources.foo,
  28. deps: {'./lib/abc': files.abc}
  29. };
  30. cache[files.bar] = {
  31. source: sources.bar,
  32. deps: {xyz: files.xyz}
  33. };
  34. cache[files.main] = {
  35. source: sources.main,
  36. deps: {
  37. abc: files.abc,
  38. xyz: files.xyz,
  39. './bar': files.bar
  40. }
  41. };
  42. test('preserves expose and entry with partial cache', function(t) {
  43. t.plan(1);
  44. var partialCache = xtend(cache);
  45. delete partialCache[files.bar];
  46. var p = parser({ cache: partialCache });
  47. p.write({ id: 'abc', file: files.abc, expose: 'abc' });
  48. p.write({ id: 'xyz', file: files.xyz, expose: 'xyz' });
  49. p.end({ id: 'main', file: files.main, entry: true });
  50. var rows = [];
  51. p.on('data', function (row) { rows.push(row); });
  52. p.on('end', function () {
  53. t.same(rows.sort(cmp), [
  54. {
  55. id: files.bar,
  56. file: files.bar,
  57. source: sources.bar,
  58. deps: {xyz: files.xyz}
  59. },
  60. {
  61. file: files.foo,
  62. id: files.foo,
  63. source: sources.foo,
  64. deps: {'./lib/abc': files.abc}
  65. },
  66. {
  67. id: 'abc',
  68. file: files.abc,
  69. source: sources.abc,
  70. deps: {},
  71. entry: true,
  72. expose: 'abc'
  73. },
  74. {
  75. id: 'main',
  76. file: files.main,
  77. source: sources.main,
  78. deps: {
  79. './bar': files.bar,
  80. abc: files.abc,
  81. xyz: files.xyz
  82. },
  83. entry: true
  84. },
  85. {
  86. id: 'xyz',
  87. file: files.xyz,
  88. source: sources.xyz,
  89. deps: {'../foo': files.foo},
  90. entry: true,
  91. expose: 'xyz'
  92. }
  93. ].sort(cmp));
  94. });
  95. });
  96. function cmp (a, b) { return a.id < b.id ? -1 : 1 }