file_cache.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. var mdeps = require('../');
  2. var test = require('tap').test;
  3. var path = require('path');
  4. var through = require('through2');
  5. var files = {
  6. foo: path.join(__dirname, '/files/foo.js'),
  7. bar: path.join(__dirname, '/files/bar.js')
  8. };
  9. var sources = {
  10. foo: 'require("./bar"); var tongs;',
  11. bar: 'notreal tongs'
  12. };
  13. var fileCache = {};
  14. fileCache[files.foo] = sources.foo;
  15. fileCache[files.bar] = sources.bar;
  16. var specialReplace = function(input) {
  17. return input.replace(/tongs/g, 'tangs');
  18. };
  19. test('uses file cache', function (t) {
  20. t.plan(1);
  21. var p = mdeps({
  22. fileCache: fileCache,
  23. transform: function (file) {
  24. return through(function (buf, enc, next) {
  25. this.push(specialReplace(String(buf)));
  26. next();
  27. });
  28. },
  29. transformKey: [ 'browserify', 'transform' ]
  30. });
  31. p.end({ id: 'foo', file: files.foo, entry: false });
  32. var rows = [];
  33. p.on('data', function (row) { rows.push(row) });
  34. p.on('end', function () {
  35. t.same(rows.sort(cmp), [
  36. {
  37. id: 'foo',
  38. file: files.foo,
  39. source: specialReplace(sources.foo),
  40. deps: { './bar': files.bar }
  41. },
  42. {
  43. id: files.bar,
  44. file: files.bar,
  45. source: specialReplace(sources.bar),
  46. deps: {}
  47. }
  48. ].sort(cmp));
  49. });
  50. });
  51. function cmp (a, b) { return a.id < b.id ? -1 : 1 }