Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elegantly minify dynamically generated javascript in a .NET environment? [closed]

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?

like image 573
Hairgami_Master Avatar asked Dec 19 '11 20:12

Hairgami_Master


People also ask

How do I minify JavaScript code?

Go to minifycode.com and click the CSS minifier tab. Then paste the CSS code into the input box and click the Minify CSS button. After the new minified code is generated, copy the code. Then go back to the css file of your website and replace the code with the new minified version.

Should you minify JavaScript?

It is important to minify your CSS and minimise JavaScript files so they can load faster on your web pages. There are many reasons why you should minify your CSS and JavaScript: Reduce file size: The more code there is in a file, the larger it will be. Minified code is usually much smaller than the original version.

What is the point of minify?

Minification is the process of minimizing code and markup in your web pages and script files. It's one of the main methods used to reduce load times and bandwidth usage on websites. Minification dramatically improves site speed and accessibility, directly translating into a better user experience.

What is Minifying used for in JavaScript?

Minification, also known as minimization, is the process of removing all unnecessary characters from JavaScript source code without altering its functionality. This includes the removal of whitespace, comments, and semicolons, along with the use of shorter variable names and functions.


1 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.

like image 81
Matt Wrock Avatar answered Oct 01 '22 15:10

Matt Wrock