Identifying a js file is minified or not
This is an effort to find out a threshold value, which can be used to determine whether a file has minified content or not. This logic is being used in the sonarwhal minified-js rule
We will generate an improvement index value for the file content and if the improvement index is greater than the threshold we identified from the below results, the file might be unminified.
Improvement index is generated by by comparing the number of tokens(from the abstract syntax tree generated from the code in the file) with the total content length of a file.
const tokenRatio = tokenLength / contentLength; const improvementIndex = Math.round((1 - tokenRatio) * 100);
I ran this test with a couple of popular libaries. Both unminified and minified versions. Here are the results.
75 seems like a good threshold based on this numbers. Files with an improvement index value greater than 75 can be safely assumed as unminified file.
Getting an Ideal threshold for your scripts
While 75 seems like a reasonable value to be used as the threshold, you might want to tweak it for your environment. If you want to find the ideal number for your scripts, you may run the tests with your script by following the steps
- Clone/copy this repository.
- Add the uniminified version of your script to the
- Add the minified version of your script to the
testfilesdirectory with the sufficx
- Add the uniminified file name to the
- Start the server by running
node server.jscommand on the root of the repository. This will start the webserver at port 3000.
http://localhost:3000/in a browser. You will be able to see the generated results for all the test files including yours