Skip to content

Instantly share code, notes, and snippets.

@probil
Created December 9, 2021 11:00
Show Gist options
  • Save probil/d7cdd7da14f4f4b27fdead85da437714 to your computer and use it in GitHub Desktop.
Save probil/d7cdd7da14f4f4b27fdead85da437714 to your computer and use it in GitHub Desktop.
A small function to determine minification of the JavaScript file. Borrowed from Chrome DevTools
// https://github.com/ChromeDevTools/devtools-frontend/blob/0dc36933e485a7009c380f41564848d70cafb429/front_end/models/text_utils/TextUtils.ts#L336
export function isMinified(text) {
const kMaxNonMinifiedLength = 500;
let linesToCheck = 10;
let lastPosition = 0;
do {
let eolIndex = text.indexOf('\n', lastPosition);
if (eolIndex < 0) {
eolIndex = text.length;
}
if (eolIndex - lastPosition > kMaxNonMinifiedLength && text.substr(lastPosition, 3) !== '//#') {
return true;
}
lastPosition = eolIndex + 1;
} while (--linesToCheck >= 0 && lastPosition < text.length);
// Check the end of the text as well
linesToCheck = 10;
lastPosition = text.length;
do {
let eolIndex = text.lastIndexOf('\n', lastPosition);
if (eolIndex < 0) {
eolIndex = 0;
}
if (lastPosition - eolIndex > kMaxNonMinifiedLength && text.substr(lastPosition, 3) !== '//#') {
return true;
}
lastPosition = eolIndex - 1;
} while (--linesToCheck >= 0 && lastPosition > 0);
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment