export function couldBeMinifiedCode()

in src/utils.js [281:341]


export function couldBeMinifiedCode(code) {
  // Fast exit if `code` is empty. Could happen in tests, but also in real
  // files.
  if (!code) {
    return false;
  }

  // If there's a source map reference it's very certainly minified code.
  if (SOURCE_MAP_RE.test(code)) {
    return true;
  }

  // Number of lines to look at, taken from the head of the code.
  const sampleSize = 30;

  // Threshold in percent of indented lines to mark a file as not
  // minified.
  const indentCountThreshold = 20; // percentage

  // Length of a line that looks suspicious of being minified
  const hugeLinesLength = 500;

  // Number of huge lines to also mark a file as potentially minified
  // Hint: Minified AngularJS has 12 lines, jQuery 4
  const hugeLinesThreshold = 4;

  let lineEndIndex = 0;
  let lineStartIndex = 0;
  let lines = 1;
  let indentCount = 0;
  let hugeLinesCount = 0;

  // Strip comments.
  const normalizedCode = code.replace(/\/\*[\S\s]*?\*\/|\/\/.+/g, '');

  while (lines < sampleSize) {
    lineEndIndex = normalizedCode.indexOf('\n', lineStartIndex);

    if (lineEndIndex === -1) {
      break;
    }

    const currentLine = normalizedCode.slice(lineStartIndex, lineEndIndex);

    if (/^\s+/.test(currentLine)) {
      indentCount++;
    }

    if (currentLine.length >= hugeLinesLength) {
      hugeLinesCount++;
    }

    lineStartIndex = lineEndIndex + 1;
    lines++;
  }

  return (
    (indentCount / lines) * 100 < indentCountThreshold ||
    hugeLinesCount > hugeLinesThreshold
  );
}