comments.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use strict';
  2. var test = require('tape'),
  3. Vinyl = require('vinyl'),
  4. gulpUglify = require('../');
  5. test('should preserve all comments', function(t) {
  6. t.plan(3);
  7. var testFile1 = new Vinyl({
  8. cwd: "/home/terin/broken-promises/",
  9. base: "/home/terin/broken-promises/test",
  10. path: "/home/terin/broken-promises/test/test1.js",
  11. contents: new Buffer('/* comment one *//*! comment two *//* comment three */')
  12. });
  13. var stream = gulpUglify({ preserveComments: 'all' });
  14. stream.on('data', function(newFile) {
  15. var contents = newFile.contents.toString();
  16. t.ok(/one/.test(contents), 'has comment one');
  17. t.ok(/two/.test(contents), 'has comment two');
  18. t.ok(/three/.test(contents), 'has comment three');
  19. });
  20. stream.write(testFile1);
  21. stream.end();
  22. });
  23. test('should preserve important comments', function(t) {
  24. t.plan(3);
  25. var testFile1 = new Vinyl({
  26. cwd: "/home/terin/broken-promises/",
  27. base: "/home/terin/broken-promises/test",
  28. path: "/home/terin/broken-promises/test/test1.js",
  29. contents: new Buffer('/* comment one *//*! comment two *//* comment three */')
  30. });
  31. var stream = gulpUglify({ preserveComments: 'some' });
  32. stream.on('data', function(newFile) {
  33. var contents = newFile.contents.toString();
  34. t.false(/one/.test(contents), 'does not have comment one');
  35. t.ok(/two/.test(contents), 'has comment two');
  36. t.false(/three/.test(contents), 'does not have comment three');
  37. });
  38. stream.write(testFile1);
  39. stream.end();
  40. });
  41. test('should preserve comments that fn returns true for', function(t) {
  42. t.plan(3);
  43. var testFile1 = new Vinyl({
  44. cwd: "/home/terin/broken-promises/",
  45. base: "/home/terin/broken-promises/test",
  46. path: "/home/terin/broken-promises/test/test1.js",
  47. contents: new Buffer('/* comment one *//*! comment two *//* comment three */')
  48. });
  49. var stream = gulpUglify({
  50. preserveComments: function(node, comment) {
  51. return /three/.test(comment.value);
  52. }
  53. });
  54. stream.on('data', function(newFile) {
  55. var contents = newFile.contents.toString();
  56. t.false(/one/.test(contents), 'does not have comment one');
  57. t.false(/two/.test(contents), 'does not have comment two');
  58. t.true(/three/.test(contents), 'has comment three');
  59. });
  60. stream.write(testFile1);
  61. stream.end();
  62. });