I'm programmatically creating javascript files from a .NET web app and would like to minify it before passing it to the client. Is there an efficient technique for doing this?
-
1http://www.hanselman.com/blog/TheImportanceAndEaseOfMinifyingYourCSSAndJavaScriptAndOptimizingPNGsForYourBlogOrWebsite.aspx – Oded Dec 19 '11 at 20:28
6 Answers
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.

- 33,834
- 5
- 90
- 130

- 6,590
- 29
- 23
-
3
-
2In case anyone is wondering the DLL reference that you need to add in order to use `Microsoft.Ajax.Utilities.Minifier` the MSI by default installs to `C:\Program Files (x86)\MSBuild\Microsoft\MicrosoftAjax` – Spencer Ruport Jun 18 '13 at 18:56
-
1It's 2016 now but I've still come back to this whilst trying to find a .net library to compress a string containing javascript code. I've compared the outputs of both YUI Compressor and the MS Ajax Minifier, and the MS library not only seems to compress much smaller (replaces 'if' statements with logic statements for example), but the YUI Compressor is actually failing for me whilst trying to compress a script containing, amongst other things, the jquery 2.2.3 library. I simply used both with the more-or-less default settings, so could be user error. – Zac May 03 '16 at 12:35
-
The last link (`YUI`) changed to: _https://github.com/YUICompressor-NET/YUICompressor.NET_ – Washington Guedes Jun 08 '17 at 17:41
-
1In case anyone comes to this thread, the Microsoft Ajax minifier is end of life. The project was forked and is currently supported here: https://github.com/madskristensen/BundlerMinifier – theBoringCoder Jan 24 '19 at 17:35
-
I need somthing like this minifer. But minification isn't enough for me. I want to uglify and obfuscate my javascript (or ecmascript) string that generated dynamicaly. What should I do? – alite Mar 24 '20 at 09:22
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.

- 3,546
- 2
- 21
- 31
-
Thanks Mike- I took a look and that looks nice. I'm not sure how much javascript has changed, but it looked a little dated. Not sure if you've used it recently... – Hairgami_Master Dec 20 '11 at 13:12
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.

- 650
- 5
- 15
-
Thanks Chris- I was looking for an automated solution but that looks interesting. On my link list. – Hairgami_Master Dec 20 '11 at 13:13
Well, I would think there are three things you need to do to minify a 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).
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.
-
Thanks Shark, but I'm not that talented, and I don't have that much time... :) I was looking for a library or dll where someone had already done this work. Perhaps you could make one??? PLZ? – Hairgami_Master Dec 20 '11 at 13:13
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.

- 933
- 11
- 13
-
-
There are many times where an approach like this would be fine - if the page is an MVC Action, for instance, Output caching can be used to reduce the frequency of full page rendering, while still allowing configuration and other run-time dynamic behavior. – Adam Tolley Oct 30 '12 at 15:20