detect.js 871 B

1234567891011121314151617181920212223242526272829303132
  1. var parser = require('../');
  2. var test = require('tap').test;
  3. var JSONStream = require('JSONStream');
  4. var packer = require('browser-pack');
  5. var path = require('path');
  6. test('detect', function (t) {
  7. t.plan(1);
  8. var p = parser({
  9. detect: function (source) {
  10. var rx = /require\(["'](.*?)["']\)/g;
  11. var m, deps = [];
  12. while (m = rx.exec(source)) {
  13. deps.push(m[1]);
  14. }
  15. return deps;
  16. }
  17. });
  18. p.end(path.join(__dirname, '/files/main.js'));
  19. p.on('error', t.fail.bind(t));
  20. var pack = packer();
  21. p.pipe(JSONStream.stringify()).pipe(pack);
  22. var src = '';
  23. pack.on('data', function (buf) { src += buf });
  24. pack.on('end', function () {
  25. Function('console', src)({
  26. log: function (s) { t.equal(s, 'main: 1055') }
  27. });
  28. });
  29. });