是否有用于缩小 Javascript 的 .NET 库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8567070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a .NET library for minifying Javascript?
提问by Hairgami_Master
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?
我正在以编程方式从 .NET Web 应用程序创建 javascript 文件,并且想在将其传递给用户之前将其缩小?是否有库或技术可以即时执行此操作?
Thank
谢谢
回答by Matt Wrock
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 Minifieror 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:
如果您只是希望能够在将 C# 中的 javascript 字符串保存到文件之前对其进行缩小,我将使用MS Ajax Minifier或用于 .net的YUI 压缩器。这两者都公开了一个允许您执行此操作的 API。这是一个使用 ajax minifier 的示例:
var minifier = new Microsoft.Ajax.Utilities.Minifier();
var minifiedString = minifier.MinifyJavaScript(unMinifiedString);
Using the YUI Compressor for .net:
使用 .net 的 YUI 压缩器:
var minifiedString = JavaScriptCompressor.Compress(unMinifiedString);
Both the ajax minifier and and YUI Compressor libraries are available via Nuget.
ajax minifier 和 YUI Compressor 库都可以通过 Nuget 获得。
回答by Hairgami_Master
Why not to use javascript-written minifier directly in .NET (try if it works as JScript code). Uglify.js comes to mind...
为什么不直接在 .NET 中使用 javascript 编写的 minifier(尝试它是否作为 JScript 代码工作)。想到了 Uglify.js...
回答by Mike Ruhlin
We use the C# port of JSMIN: http://www.koders.com/csharp/fidC8F76D32D2FB3B213046C30CD8B362820FFFD604.aspx?s=file#L15
我们使用JSMIN的C#端口:http: //www.koders.com/csharp/fidC8F76D32D2FB3B213046C30CD8B362820FFFD604.aspx?s =file#L15
It works pretty well.
它工作得很好。
回答by Chris Brickhouse
i use this manually.
我手动使用它。
http://dean.edwards.name/packer/
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.
我压缩文件,上传,然后撤消包,所以我有完整的源代码。我只打包生产代码。
回答by Eliseu Monar dos Santos
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.
您可以使用Closure Compiler,但我不建议您在每次用户访问您的网站时都缩小文件。在部署新提交之前构建所有文件要好得多。看看这篇关于工具的文章。
回答by Eliseu Monar dos Santos
Well, I would think there are three things you need to do to minifya script file:
好吧,我认为您需要做三件事来缩小脚本文件:
- Shorten long variables
- Remove comments
- 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).
在运行时替换这些都相对简单,但需要编写一些代码。对于变量缩短,在其范围内找到比 2 个字母长的变量。然后缩写并在代码块(变量的范围)中进行。
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.
删除评论很简单。删除空格也很容易。对于单行,查找//
并删除直到出现回车/换行符。空格,用空格替换制表符,用空格替换多个空格,用空格替换回车/换行符。