当前位置: 动力学知识库 > 问答 > 编程问答 >

Is there a .NET library for minifying Javascript?

问题描述:

I'm programatically creating javascript files from a .NET web app, and would like to minify it before passing it on to the user? Is there a library or technique for doing this on the fly?

Thank

网友答案:

If you simply want to be able to minify a javascript string in C# before saving it to a file, I would use either the MS Ajax Minifier or the YUI compressor for .net. Both of these expose an API that allows you to do this. Here is a sample using the ajax minifier:

var minifier = new Microsoft.Ajax.Utilities.Minifier();
var minifiedString = minifier.MinifyJavaScript(unMinifiedString);

Using the YUI Compressor for .net:

var minifiedString = JavaScriptCompressor.Compress(unMinifiedString);

Both the ajax minifier and and YUI Compressor libraries are available via Nuget.

网友答案:

Yes, you can check squishit. Here is the link.

网友答案:

Why not to use javascript-written minifier directly in .NET (try if it works as JScript code). Uglify.js comes to mind...

网友答案:

We use the C# port of JSMIN: http://www.koders.com/csharp/fidC8F76D32D2FB3B213046C30CD8B362820FFFD604.aspx?s=file#L15

It works pretty well.

网友答案:

i use this manually.

http://dean.edwards.name/packer/

i compact the files, upload, then undo the pack so i have the source code intact. i pack production code only.

网友答案:

Well, I would think there are three things you need to do to minify a script file:

  1. Shorten long variables
  2. Remove comments
  3. Remove needless whitespaces (tabs, spaces, carriage returns)

Those are all relatively simple to replace at runtime, but will take a bit of code writing. For the variable shortening, find like variables in their scope that are longer than, say 2 letters. Then abbreviate and have that follow through in the code block (the scope of the variable).

Removing comments will be simple. Removing whitespaces are also easy. For singleline, find // and delete until a carriage return/newline feed. Whitespaces, replace tabs with a space, multiple spaces with a space, and carriage returns/newline feeds with a space.

网友答案:

You can use the Closure Compiler, but I wouldn't recommend you minifying files everytime a user visits your website. It's much better to build all the files before deploying new commits. Take a look at this article about tools.

分享给朋友:
您可能感兴趣的文章:
随机阅读: