开发者

How to detect if a JS is packed already

开发者 https://www.devze.com 2023-01-17 22:25 出处:网络
Hey guys.. I am writing a Windows application in C# that minifies CSS files and packs JS files as a batch job. One hurdle for the application is, what if the user selects a JavaScript file that has al

Hey guys.. I am writing a Windows application in C# that minifies CSS files and packs JS files as a batch job. One hurdle for the application is, what if the user selects a JavaScript file that has already been packed? It will end up increasing the file size, defeating my purpose entirely!

Is opening the file a开发者_高级运维nd looking for the string eval(function(p,a,c,k,e,d) enough? My guess is no, as there are other JS packing methods out there. Help me out!


One might suggest that you compare the size of the pre and post packed JS and return/use the smaller of the two.

UPDATE based on question in comment by GPX on Sep 30 at 1:02

The following is a very simple way to tell. There may be different, or more accurate, ways of determining this, but this should get you going in the right direction:

var unpackedJs = File.ReadAllText(...)
var unpackedSize = jsContent.Length;
var packedJs = ... // Your Packaging routine
File.WriteAllText(pathToFile, unpackedSize < packedJs.Length ? unpackedJs : packedJs)


I would check file size and lines of code (e.g.: average line length). These two information should be enough to know if the code is sufficiently compact.

Try this demo.


I direct you to a post that suggests packing is bad.

http://ejohn.org/blog/library-loading-speed/

Rather use minification. Google Closure compiler can do this via a REST web service. Only use a .min.js extension for minified (not packed).

Gzip will do a better job and will be uncompressed by the browser. Its best to switch on zip compression on the server which will zip a minified file down further.

Of course this raises the question 'How can I tell if my Javascript is already minified!'


When you create/save a minified file, use the standard file name convention of "Filename.min.js". Then when they select the file, you can check for that as a reliable indicator.

I do not think it is wise to go overboard on the dummy-proofing. If a user (who is a developer, at that), is dumb enough to double-pack a file, they should experience problems. I know you should give them the benefit of the doubt, but in this case it does not seem worth the overhead.


If you're using a safe minimization routine, your output should be the same as the input. I would not recommend the routine you mention. MS's Ajax Minifier is a good tool and even provides dll's to use in your project. This would make your concern a non-issue.


I would suggest adding a '.min' prefix to the extension of the packed file, something like 'script.min.js'. Then just check the file name.

Other than that, I would suggest checking how long the lines are, and how many spaces are used. Minified/packed JS typically has almost no spaces (typically in strings) and very long lines.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号