jsmin.sourcemap.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. var jsmin = require('jsmin2'),
  2. SourceMapFileCollector = require('source-map-index-generator');
  3. function Collector(params) {
  4. // Generate and save our SourceMapFileCollector
  5. var fileCollector = new SourceMapFileCollector(params);
  6. this.fileCollector = fileCollector;
  7. // Save a line offset for addition handling
  8. this.lineOffset = 0;
  9. // Save an array for code concatenation
  10. this.codeArr = [];
  11. }
  12. Collector.prototype = {
  13. /**
  14. * Add a file to minify
  15. * @param {Object} params
  16. * @param {String} params.code Code to minify
  17. * @param {String} params.src Filepath to original src
  18. */
  19. 'addFile': function (params) {
  20. var input = params.code,
  21. codeObj = jsmin(input),
  22. code = codeObj.code,
  23. codeMap = codeObj.codeMap,
  24. lineOffset = this.lineOffset,
  25. fileCollector = this.fileCollector;
  26. // If there is no leading line break
  27. if (code.charAt(0) !== '\n') {
  28. // Insert a leading line break
  29. // DEV: This prevents conflicts between pieces of code.
  30. // DEV: Normally `jsmin` inserts one at this start automatically but "use strict" has discovered this edge case.
  31. code = '\n' + code;
  32. // Offset all codeMap to account for new character
  33. var indices = Object.getOwnPropertyNames(codeMap),
  34. i = 0,
  35. len = indices.length;
  36. for (; i < len; i++) {
  37. codeMap[indices[i]] += 1;
  38. }
  39. }
  40. // Count all the lines in code
  41. var lines = code.match(/\n/g),
  42. lineCount = 0;
  43. if (lines) {
  44. lineCount = lines.length;
  45. }
  46. // Add to our fileCollector
  47. fileCollector.addIndexMapping({
  48. 'input': input,
  49. 'output': code,
  50. 'map': codeMap,
  51. 'src': params.src,
  52. 'lineOffset': lineOffset
  53. });
  54. // Save the new code
  55. this.codeArr.push(code);
  56. // Update the line offset
  57. this.lineOffset = lineOffset + lineCount;
  58. // Return this for a fluent interface
  59. return this;
  60. },
  61. /**
  62. * Export function for the collector
  63. * @return {Object} retObj
  64. * @return {String} retObj.code Minified JavaScript
  65. * @return {Object} retObj.sourcemap Sourcemap of input to minified JavaScript
  66. */
  67. 'export': function () {
  68. // Output the source map and code
  69. // DEV: JSMin automatically puts a line break at the top of each statement so we can use an empty string for our join =D
  70. // DEV: This was true until we ran into global `"use strict";`
  71. var fileCollector = this.fileCollector,
  72. srcMap = fileCollector.toString(),
  73. codeArr = this.codeArr,
  74. code = codeArr.join(''),
  75. retObj = {
  76. 'code': code,
  77. 'sourcemap': srcMap
  78. };
  79. return retObj;
  80. }
  81. };
  82. /**
  83. * JSMin + source-map
  84. * @param {Object} params Parameters to minify and generate sourcemap with
  85. * @param {String} [params.dest="undefined.js"] Destination for your JavaScript (used inside of sourcemap map)
  86. * @param {String} [params.srcRoot] Optional root for all relative URLs
  87. *
  88. * SINGLE FILE FORMAT
  89. * @param {String} params.src File path to original JavaScript (seen when an error is thrown)
  90. * @param {String} params.code JavaScript to minify
  91. *
  92. * MULTI FILE FORMAT
  93. * @param {Object[]} params.input Array of objects) to minify
  94. * @param {String} params.input[n].src File path to original JavaScript (seen when an error is thrown)
  95. * @param {String} params.input[n].code JavaScript to minify
  96. *
  97. * @return {Object} retObj
  98. * @return {String} retObj.code Minified JavaScript
  99. * @return {Object} retObj.sourcemap Sourcemap of input to minified JavaScript
  100. */
  101. module.exports = function (params) {
  102. var dest = params.dest || 'undefined.js',
  103. srcRoot = params.srcRoot,
  104. input = params.input,
  105. collectorParams = {'file': dest};
  106. // If there is a sourceRoot, use it
  107. if (srcRoot) {
  108. collectorParams.sourceRoot = srcRoot;
  109. }
  110. // Generate our collector
  111. var collector = new Collector(collectorParams);
  112. // If there is no input, build one
  113. if (!input) {
  114. input = [{'src': params.src, 'code': params.code}];
  115. } else if (!Array.isArray(input)) {
  116. // Otherwise, if input is not an array, cast it to one
  117. input = [input];
  118. }
  119. // Add each of the files
  120. var addFile = collector.addFile.bind(collector);
  121. input.forEach(addFile);
  122. // Retun the retObj
  123. var retObj = collector['export']();
  124. return retObj;
  125. };