Repository URL to install this package:
|
Version:
1.1.21 ▾
|
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.minifyCookedValues = exports.minifyRawValues = exports.minifyCooked = exports.minifyRaw = exports.compressSymbols = exports.stripLineComment = void 0;
const _require = require("../css/placeholderUtils"),
splitByPlaceholders = _require.splitByPlaceholders,
makePlaceholder = _require.makePlaceholder;
function makeMultilineCommentRegex(newlinePattern) {
return new RegExp('\\/\\*[^!](.|' + newlinePattern + ')*?\\*\\/', 'g');
}
const lineCommentStart = /\/\//g; // Counts occurences of substr inside str
const symbolRegex = /(\s*[;:{},]\s*)/g;
/**
* Joins substrings until predicate returns true
*/
function countOccurences(str, substr) {
return str.split(substr).length - 1;
} // Joins at comment starts when it's inside a string or parantheses
// effectively removing line comments
function reduceSubstr(substrs, join, predicate) {
let length = substrs.length;
let res = substrs[0];
if (length === 1) {
return res;
}
for (let i = 1; i < length; i++) {
if (predicate(res)) {
break;
}
res += join + substrs[i];
}
return res;
}
function stripLineComment(line) {
return reduceSubstr(line.split(lineCommentStart), '//', function (str) {
// NOTE: This is another guard against urls, if they're not inside strings or parantheses.
return !str.endsWith(':') && countOccurences(str, "'") % 2 === 0 && countOccurences(str, '"') % 2 === 0 && countOccurences(str, '(') === countOccurences(str, ')');
});
} // Detects lines that are exclusively line comments
function compressSymbols(code) {
return code.split(symbolRegex).reduce(function (str, fragment, index) {
// Even-indices are non-symbol fragments
if (index % 2 === 0) {
// Only manipulate symbols outside of strings
return str + fragment;
}
if (countOccurences(str, "'") % 2 === 0 && countOccurences(str, '"') % 2 === 0) {
return str + fragment.trim();
}
return str + fragment;
}, '');
} // Creates a minifier with a certain linebreak pattern
function isLineComment(line) {
return line.trim().startsWith('//');
}
function minify(linebreakPattern) {
const linebreakRegex = new RegExp(linebreakPattern + '\\s*', 'g');
const multilineCommentRegex = makeMultilineCommentRegex(linebreakPattern);
return function (code) {
let newCode = code // Remove multiline comments
.replace(multilineCommentRegex, '\n') // Split at newlines
.split(linebreakRegex) // Removes lines containing only line comments
.filter(function (line) {
return line.length > 0 && !isLineComment(line);
}) // Remove line comments inside text
.map(stripLineComment) // Rejoin all lines
.join(' ');
return compressSymbols(newCode);
};
}
const minifyRaw = minify('(?:\\\\r|\\\\n|\\r|\\n)');
const minifyCooked = minify('[\\r\\n]');
function minifyRawValues(rawValues) {
const placeholder = makePlaceholder(123);
const rawPlaceholder = rawValues.join(placeholder);
const minified = minifyRaw(rawPlaceholder);
return splitByPlaceholders(minified, false);
}
function minifyCookedValues(cookedValues) {
return splitByPlaceholders(minifyCooked(cookedValues.join(makePlaceholder)(123))), false;
}
exports.stripLineComment = stripLineComment;
exports.compressSymbols = compressSymbols;
exports.minifyRawValues = minifyRawValues;
exports.minifyRaw = minifyRaw;
exports.minifyCooked = minifyCooked;
exports.minifyCookedValues = minifyCookedValues;