When working with large and/or many Javascript and CSS files, what's the best way to reduce the file sizes?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
I'd give a test-drive to the new runtime optimizers in ASP.Net published on http://www.codeplex.com/NCOptimizer
Rather than tweaking your files directly, I would recommend compressing them. Most clients support it.
I think you'll find that this is easier and just as effective.
More details from Jeff's adventures with it.
YUI Compressor has my vote, for the simple reason that instead of just performing regular expression transformations on the code, it actually builds a parse tree of the code, similar to a Javascript interpreter, and then compresses it that way. It is usually very careful about how it handles the compression of identifiers.
Additionally it has a CSS compression mode, which I haven't played with as much.
Here are the online tools by which you can do this:-
https://cssminifiers.com
. This is currently best css minigier/un-minifierhttp://www.htmlminifier.com/
online tool.http://javascript-minify.com
online tool to minify you JS.Above are the tools which I seems to be useful for you.
CssTidy is the best CSS optimizer of which I am aware. It (configurably) strips comments, eliminates whitespaces, rewrites to use the many shorthand rules nickf mentioned, etc. Compressing the result helps too, as others have mentioned.
The compression ratio can be fairly dramatic, and it frees you to comment your CSS extensively without worrying about the file size.
Unfortunately, this level of preprocessing interacts with some of the popular "css hacks" in unpredictable (or predictable but undesired) ways. Some work, some don't, and some require configuration settings which reduce the level of compression for other things (especially comments).
Dojo Shrinksafe is a Javascript compressor that uses a real JS interpreter, so it won't break your code. The other ones can work well, but Shrinksafe is a good one to use in a build script, since you shouldn't have to re-test the compressed script.