UNPKG

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