tr_deps.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. var parser = require('../');
  2. var through = require('through2');
  3. var test = require('tap').test;
  4. var fs = require('fs');
  5. var path = require('path');
  6. var files = {
  7. transformdeps: path.join(__dirname, '/files/transformdeps.js'),
  8. foo: path.join(__dirname, '/files/foo.js'),
  9. bar: path.join(__dirname, '/files/bar.js')
  10. };
  11. var sources = Object.keys(files).reduce(function (acc, file) {
  12. acc[file] = fs.readFileSync(files[file], 'utf8');
  13. return acc;
  14. }, {});
  15. test('deps added by transforms', function (t) {
  16. t.plan(1);
  17. var p = parser();
  18. p.write({ transform: transform, options: {} });
  19. p.end({ file: files.transformdeps, entry: true });
  20. function transform (file) {
  21. if (file === files.transformdeps) return through(function(chunk, enc, cb) {
  22. cb(null, chunk);
  23. }, function (cb) {
  24. this.emit('dep', './foo');
  25. cb();
  26. });
  27. return through();
  28. }
  29. var rows = [];
  30. p.on('data', function (row) { rows.push(row) });
  31. p.on('end', function () {
  32. t.same(rows.sort(cmp), [
  33. {
  34. id: files.transformdeps,
  35. file: files.transformdeps,
  36. source: sources.transformdeps,
  37. entry: true,
  38. deps: { './foo': files.foo }
  39. },
  40. {
  41. id: files.foo,
  42. file: files.foo,
  43. source: sources.foo,
  44. deps: { './bar': files.bar }
  45. },
  46. {
  47. id: files.bar,
  48. file: files.bar,
  49. source: sources.bar,
  50. deps: {}
  51. }
  52. ].sort(cmp));
  53. });
  54. });
  55. function cmp (a, b) { return a.id < b.id ? -1 : 1 }