index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.minifyCookedValues = exports.minifyRawValues = exports.minifyCooked = exports.minifyRaw = exports.compressSymbols = exports.stripLineComment = void 0;
  6. var _difference = _interopRequireDefault(require("lodash/difference"));
  7. var _placeholderUtils = require("../css/placeholderUtils");
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. var injectUniquePlaceholders = function injectUniquePlaceholders(strArr) {
  10. var i = 0;
  11. return strArr.reduce(function (str, val, index, arr) {
  12. return str + val + (index < arr.length - 1 ? (0, _placeholderUtils.makePlaceholder)(i++) : '');
  13. }, '');
  14. };
  15. var makeMultilineCommentRegex = function makeMultilineCommentRegex(newlinePattern) {
  16. return new RegExp('\\/\\*[^!](.|' + newlinePattern + ')*?\\*\\/', 'g');
  17. };
  18. var lineCommentStart = /\/\//g;
  19. var symbolRegex = /(\s*[;:{},]\s*)/g; // Counts occurences of substr inside str
  20. var countOccurences = function countOccurences(str, substr) {
  21. return str.split(substr).length - 1;
  22. }; // Joins substrings until predicate returns true
  23. var reduceSubstr = function reduceSubstr(substrs, join, predicate) {
  24. var length = substrs.length;
  25. var res = substrs[0];
  26. if (length === 1) {
  27. return res;
  28. }
  29. for (var i = 1; i < length; i++) {
  30. if (predicate(res)) {
  31. break;
  32. }
  33. res += join + substrs[i];
  34. }
  35. return res;
  36. }; // Joins at comment starts when it's inside a string or parantheses
  37. // effectively removing line comments
  38. var stripLineComment = function stripLineComment(line) {
  39. return reduceSubstr(line.split(lineCommentStart), '//', function (str) {
  40. return !str.endsWith(':') && // NOTE: This is another guard against urls, if they're not inside strings or parantheses.
  41. countOccurences(str, "'") % 2 === 0 && countOccurences(str, '"') % 2 === 0 && countOccurences(str, '(') === countOccurences(str, ')');
  42. });
  43. };
  44. exports.stripLineComment = stripLineComment;
  45. var compressSymbols = function compressSymbols(code) {
  46. return code.split(symbolRegex).reduce(function (str, fragment, index) {
  47. // Even-indices are non-symbol fragments
  48. if (index % 2 === 0) {
  49. return str + fragment;
  50. } // Only manipulate symbols outside of strings
  51. if (countOccurences(str, "'") % 2 === 0 && countOccurences(str, '"') % 2 === 0) {
  52. return str + fragment.trim();
  53. }
  54. return str + fragment;
  55. }, '');
  56. }; // Detects lines that are exclusively line comments
  57. exports.compressSymbols = compressSymbols;
  58. var isLineComment = function isLineComment(line) {
  59. return line.trim().startsWith('//');
  60. }; // Creates a minifier with a certain linebreak pattern
  61. var minify = function minify(linebreakPattern) {
  62. var linebreakRegex = new RegExp(linebreakPattern + '\\s*', 'g');
  63. var multilineCommentRegex = makeMultilineCommentRegex(linebreakPattern);
  64. return function (code) {
  65. var newCode = code.replace(multilineCommentRegex, '\n') // Remove multiline comments
  66. .split(linebreakRegex) // Split at newlines
  67. .filter(function (line) {
  68. return line.length > 0 && !isLineComment(line);
  69. }) // Removes lines containing only line comments
  70. .map(stripLineComment) // Remove line comments inside text
  71. .join(' '); // Rejoin all lines
  72. var eliminatedExpressionIndices = (0, _difference.default)(code.match(_placeholderUtils.placeholderRegex), newCode.match(_placeholderUtils.placeholderRegex)).map(function (x) {
  73. return parseInt(x.match(/\d+/)[0], 10);
  74. });
  75. return [compressSymbols(newCode), eliminatedExpressionIndices];
  76. };
  77. };
  78. var minifyRaw = minify('(?:\\\\r|\\\\n|\\r|\\n)');
  79. exports.minifyRaw = minifyRaw;
  80. var minifyCooked = minify('[\\r\\n]');
  81. exports.minifyCooked = minifyCooked;
  82. var minifyRawValues = function minifyRawValues(rawValues) {
  83. return (0, _placeholderUtils.splitByPlaceholders)(minifyRaw(injectUniquePlaceholders(rawValues)), false);
  84. };
  85. exports.minifyRawValues = minifyRawValues;
  86. var minifyCookedValues = function minifyCookedValues(cookedValues) {
  87. return (0, _placeholderUtils.splitByPlaceholders)(minifyCooked(injectUniquePlaceholders(cookedValues)), false);
  88. };
  89. exports.minifyCookedValues = minifyCookedValues;