sourcemap.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 'use strict';
  2. var test = require('tape-catch');
  3. var assert = require('assert');
  4. var Buffer = require('safe-buffer').Buffer;
  5. var Vinyl = require('vinyl');
  6. var SourceListMap = require('source-list-map').SourceListMap;
  7. var fromStringWithSourceMap = require('source-list-map')
  8. .fromStringWithSourceMap;
  9. var td = require('testdouble');
  10. var minify = require('../lib/minify');
  11. test('sourcemaps should be merged', function(t) {
  12. var testContents1Input =
  13. '(function(first, second) {\n console.log(first + second);\n}(5, 10));\n';
  14. var testFile = new Vinyl({
  15. cwd: '/home/terin/broken-promises/',
  16. base: '/home/terin/broken-promises/test',
  17. path: '/home/terin/broken-promises/test/test1.js',
  18. contents: Buffer.from(testContents1Input)
  19. });
  20. var originalMap = new SourceListMap();
  21. originalMap.add(testContents1Input, 'test1.js', testContents1Input);
  22. testFile.sourceMap = originalMap.toStringWithSourceMap({
  23. file: 'test1.js'
  24. }).map;
  25. var outMap = fromStringWithSourceMap(
  26. 'foobar',
  27. testFile.sourceMap
  28. ).toStringWithSourceMap({file: 'test1.js'});
  29. var uglify = td.object(['minify']);
  30. var logger = td.object(['logger']);
  31. td.when(
  32. uglify.minify(
  33. {
  34. 'test1.js': testContents1Input
  35. },
  36. {
  37. output: {},
  38. sourceMap: {
  39. filename: 'test1.js',
  40. includeSources: true,
  41. content: testFile.sourceMap
  42. }
  43. }
  44. )
  45. ).thenReturn({
  46. code: 'foobar',
  47. map: JSON.stringify(outMap.map)
  48. });
  49. var subject = minify(uglify, logger)({});
  50. var file = subject(testFile);
  51. assert.ok(file instanceof Vinyl);
  52. assert.ok(Buffer.isBuffer(file.contents), 'file contents are a buffer');
  53. assert.equal(String(file.contents), 'foobar');
  54. assert.ok(file.sourceMap, 'has a source map');
  55. assert.equal(file.sourceMap.version, 3, 'source map has expected version');
  56. assert.ok(
  57. Array.isArray(file.sourceMap.sources),
  58. 'source map has sources array'
  59. );
  60. assert.ok(Array.isArray(file.sourceMap.names), 'source maps has names array');
  61. assert.ok(file.sourceMap.mappings, 'source map has mappings');
  62. td.reset();
  63. t.end();
  64. });
  65. test('sourcemaps should merge when concatted', function(t) {
  66. var inMap = new SourceListMap();
  67. inMap.add('foo\n', 'foo.js', 'foo\n');
  68. inMap.add('bar\n', 'bar.js', 'bar\n');
  69. var testFile = new Vinyl({
  70. cwd: '/home/terin/broken-promises/',
  71. base: '/home/terin/broken-promises/test',
  72. path: '/home/terin/broken-promises/test/test1.js',
  73. contents: Buffer.from(String(inMap))
  74. });
  75. testFile.sourceMap = inMap.toStringWithSourceMap({file: 'test1.js'}).map;
  76. var outMap = new SourceListMap();
  77. outMap.add(' ', 'foo.js', 'foo\n');
  78. outMap.add(' ', 'bar.js', 'bar\n');
  79. outMap = outMap.toStringWithSourceMap({file: 'test1.js'});
  80. var uglify = td.object(['minify']);
  81. var logger = td.object(['warn']);
  82. td.when(
  83. uglify.minify(
  84. {
  85. 'test1.js': String(inMap)
  86. },
  87. {
  88. output: {},
  89. sourceMap: {
  90. filename: 'test1.js',
  91. includeSources: true,
  92. content: testFile.sourceMap
  93. }
  94. }
  95. )
  96. ).thenReturn({
  97. code: 'send a PR changing this to the best La Croix flavor',
  98. map: JSON.stringify(outMap.map)
  99. });
  100. var subject = minify(uglify, logger)({});
  101. var file = subject(testFile);
  102. assert.ok(file instanceof Vinyl);
  103. assert.ok(Buffer.isBuffer(file.contents), 'file contents are a buffer');
  104. assert.equal(
  105. String(file.contents),
  106. 'send a PR changing this to the best La Croix flavor'
  107. );
  108. assert.ok(file.sourceMap, 'has a source map');
  109. assert.equal(file.sourceMap.version, 3, 'source map has expected version');
  110. assert.ok(
  111. Array.isArray(file.sourceMap.sources),
  112. 'source map has sources array'
  113. );
  114. assert.deepEqual(
  115. file.sourceMap.sources,
  116. ['foo.js', 'bar.js'],
  117. 'sources array has the inputs'
  118. );
  119. assert.ok(Array.isArray(file.sourceMap.names), 'source maps has names array');
  120. assert.ok(file.sourceMap.mappings, 'source map has mappings');
  121. td.reset();
  122. t.end();
  123. });