sourcemap.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. 'use strict';
  2. var test = require('tape'),
  3. Vinyl = require('vinyl'),
  4. gulpUglify = require('../'),
  5. uglifyjs = require('uglify-js'),
  6. concat = require('gulp-concat'),
  7. sourcemaps = require('gulp-sourcemaps');
  8. var testContents1Input = '(function(first, second) {\n console.log(first + second);\n}(5, 10));\n';
  9. var testContents1Expected = uglifyjs.minify(testContents1Input, {fromString: true}).code;
  10. var testContents2Input = '(function(alert) {\n alert(5);\n}(alert));\n';
  11. var testContents2Expected = uglifyjs.minify(testContents2Input, {fromString: true}).code;
  12. var testConcatExpected = uglifyjs.minify(testContents1Expected + testContents2Input, {fromString: true}).code;
  13. test('should minify files', function(t) {
  14. t.plan(11);
  15. var testFile1 = new Vinyl({
  16. cwd: "/home/terin/broken-promises/",
  17. base: "/home/terin/broken-promises/test",
  18. path: "/home/terin/broken-promises/test/test1.js",
  19. contents: new Buffer(testContents1Input)
  20. });
  21. var sm = sourcemaps.init();
  22. var mangled = sm.pipe(gulpUglify());
  23. mangled.on('data', function(newFile) {
  24. t.ok(newFile, 'emits a file');
  25. t.ok(newFile.path, 'file has a path');
  26. t.ok(newFile.relative, 'file has relative path information');
  27. t.ok(newFile.contents, 'file has contents');
  28. t.ok(newFile.contents instanceof Buffer, 'file contents are a buffer');
  29. t.equals(String(newFile.contents), testContents1Expected);
  30. t.ok(newFile.sourceMap, 'has a source map');
  31. t.equals(newFile.sourceMap.version, 3, 'source map has expected version');
  32. t.ok(Array.isArray(newFile.sourceMap.sources), 'source map has sources array');
  33. t.ok(Array.isArray(newFile.sourceMap.names), 'source maps has names array');
  34. t.ok(newFile.sourceMap.mappings, 'source map has mappings');
  35. });
  36. sm.write(testFile1);
  37. sm.end();
  38. });
  39. test('should merge source maps correctly', function(t) {
  40. t.plan(12);
  41. var testFile1 = new Vinyl({
  42. cwd: "/home/terin/broken-promises/",
  43. base: "/home/terin/broken-promises/test",
  44. path: "/home/terin/broken-promises/test/test1.js",
  45. contents: new Buffer(testContents1Input)
  46. });
  47. var testFile2 = new Vinyl({
  48. cwd: "/home/terin/broken-promises/",
  49. base: "/home/terin/broken-promises/test",
  50. path: "/home/terin/broken-promises/test/test2.js",
  51. contents: new Buffer(testContents2Input)
  52. });
  53. var sm = sourcemaps.init();
  54. var ct = sm.pipe(concat('all.js'));
  55. var mangled = ct.pipe(gulpUglify());
  56. mangled.on('data', function(newFile) {
  57. t.ok(newFile, 'emits a file');
  58. t.ok(newFile.path, 'file has a path');
  59. t.ok(newFile.relative, 'file has relative path information');
  60. t.ok(newFile.contents, 'file has contents');
  61. t.ok(newFile.contents instanceof Buffer, 'file contents are a buffer');
  62. t.equals(String(newFile.contents), testConcatExpected);
  63. t.ok(newFile.sourceMap, 'has a source map');
  64. t.equals(newFile.sourceMap.version, 3, 'source map has expected version');
  65. t.ok(Array.isArray(newFile.sourceMap.sources), 'source map has sources array');
  66. t.deepEquals(newFile.sourceMap.sources, ['test1.js', 'test2.js'], 'sources array has the inputs');
  67. t.ok(Array.isArray(newFile.sourceMap.names), 'source maps has names array');
  68. t.ok(newFile.sourceMap.mappings, 'source map has mappings');
  69. });
  70. sm.write(testFile1);
  71. sm.write(testFile2);
  72. sm.end();
  73. });
  74. test('should not remember source maps across files', function(t) {
  75. t.plan(26);
  76. var testFile1 = new Vinyl({
  77. cwd: "/home/terin/broken-promises/",
  78. base: "/home/terin/broken-promises/test",
  79. path: "/home/terin/broken-promises/test/test1.js",
  80. contents: new Buffer(testContents1Input)
  81. });
  82. testFile1.sourceMap = {
  83. version: 3,
  84. file: 'test1.js',
  85. sourceRoot: '',
  86. sources: [ 'test1.ts' ],
  87. sourcesContent: ['(function(first, second) { console.log(first + second) }(5, 10))'],
  88. names: [],
  89. mappings: 'AAAA,CAAC,UAAS,KAAK,EAAE,MAAM;IAAI,OAAO,CAAC,GAAG,CAAC,KAAK,GAAG,MAAM,CAAC;AAAC,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC'
  90. };
  91. var testFile1SourcesContent = [].slice.call(testFile1.sourceMap.sourcesContent);
  92. var testFile2 = new Vinyl({
  93. cwd: "/home/terin/broken-promises/",
  94. base: "/home/terin/broken-promises/test",
  95. path: "/home/terin/broken-promises/test/test2.js",
  96. contents: new Buffer(testContents2Input)
  97. });
  98. testFile2.sourceMap = {
  99. version: 3,
  100. file: 'test2.js',
  101. sourceRoot: '',
  102. sources: [ 'test2.ts' ],
  103. sourcesContent: ['(function(alert) { alert(5); }(alert))'],
  104. names: [],
  105. mappings: 'AAAA,CAAC,UAAS,KAAK;IAAI,KAAK,CAAC,CAAC,CAAC;AAAE,CAAC,CAAC,KAAK,CAAC,CAAC'
  106. };
  107. var testFile2SourcesContent = [].slice.call(testFile2.sourceMap.sourcesContent);
  108. var mangled = gulpUglify();
  109. mangled.on('data', function(newFile) {
  110. t.ok(newFile, 'emits a file');
  111. t.ok(newFile.path, 'file has a path');
  112. t.ok(newFile.relative, 'file has relative path information');
  113. t.ok(newFile.contents, 'file has contents');
  114. t.ok(newFile.contents instanceof Buffer, 'file contents are a buffer');
  115. if (/test1\.js/.test(newFile.path)) {
  116. t.equals(String(newFile.contents), testContents1Expected);
  117. t.deepEquals(newFile.sourceMap.sources, ['test1.ts']);
  118. t.deepEquals(testFile1SourcesContent, newFile.sourceMap.sourcesContent);
  119. } else if (/test2\.js/.test(newFile.path)) {
  120. t.equals(String(newFile.contents), testContents2Expected);
  121. t.deepEquals(newFile.sourceMap.sources, ['test2.ts']);
  122. t.deepEquals(testFile2SourcesContent, newFile.sourceMap.sourcesContent);
  123. }
  124. t.ok(newFile.sourceMap, 'has a source map');
  125. t.equals(newFile.sourceMap.version, 3, 'source map has expected version');
  126. t.ok(Array.isArray(newFile.sourceMap.sources), 'source map has sources array');
  127. t.ok(Array.isArray(newFile.sourceMap.names), 'source maps has names array');
  128. t.ok(newFile.sourceMap.mappings, 'source map has mappings');
  129. });
  130. mangled.write(testFile1);
  131. mangled.write(testFile2);
  132. mangled.end();
  133. });