Geeks With Blogs


What I do:

Identity Mine

MVVM Light


What I am:

Microsoft Most Valuable Professional, Client Application Development

Microsoft Certified Technology Specialist, Windows Presentation Foundation

WPF disciples


View my profile on LinkedIn

Creative Commons License
Diary of a Code Trotter by Laurent Bugnion is licensed under a Creative Commons Attribution 3.0 Unported License

All source code on this blog is licensed under the MIT license.

Copyright (c) 2006 - 2011 GalaSoft Laurent Bugnion

Laurent Bugnion (GalaSoft) Diary of a Code Trotter
A few years ago, I translated Douglas Crockford's JsMin from C to C#, because we wanted to use this functionality in our build process. JsMin is a code minimizer for JavaScript. It will remove all comments, empty lines, turn tabs into single spaces, etc... The goal is to reduce the size of JavaScript files to the minimum necessary, without modifying the functionality.
Additionally to reducing the script's size, JsMin also makes it more difficult to read, however it is not an obfuscator. Objects, variables, methods names are not modified. While such a functionality wouldn't be very difficult to add, we believe that it is pointless to obfuscate JavaScript files.
Note: This is a direct translation from C (procedural) to C#. I didn't refactor the object too much, so the design is far from perfect, but it works well.
Note 2: I just found out that Douglas translated JsMin from C to C# too, his version is available here, so pick the one you prefer ;-)
Live demo
This page has a form allowing the user to send a JavaScript file to the web server. The file will be minimized and returned to the user.
Methods list
JsMin has only a default constructor, but it also has a few public properties used to set options:
  • string BakFileExtension: The extension used for the backup file. Note: The default file will be kept only if the method Go( string, bool ) is used, and the second parameter set to true. Default: "bak".
  • string[] FirstLines: An array of strings which will be added on top of the minimized file. Each entry in the array is one line in the target file. This can be used to specify copyright information, author name, etc...
  • int KeepFirstLines: A number specifying how many lines in the original file will be copied as is in the target file. For example, this is useful to preserve copyright information, author name... present in the original file.
The C# version of JsMin provided here offers 3 public methods to minimize JavaScript files:
  • Go( string filePath ): Minimizes a script file and saves it under the same name as the original file. No backup is kept.
  • Go( string filePath, bool keepBakFile ): Minimizes a script file and saves it under the same name as the original file. Depending on the value of the parameter keepBakFile, a backup file will be kept or not.
  • Go( StreamReader reader, StreamWriter writer ): Minimizes the content of the "reader" stream and saves it to the "writer" stream. This method is useful when no file is available. For example, in ASP.NET, this method can be used to minimize script files contained in resources dynamically. Another use is described here under: The user of this webpage selects a local script file and sends it to the web server. The file content is minimized, and returned to the user as an attachment, so that the user can choose to open or save the file.
Code example: ASP.NET
private void bnSubmit_Click( object sender, EventArgs e ) { try { // Clear the response to avoid any unwanted // additions to the file. Response.Clear(); Response.BufferOutput = true; // Instantiate the minimizer. JsMin jsMin = new JsMin(); // taFirstLines is a text area allowing the // user to enter first lines to be added to // the minimized file. if ( taFirstLines.Text.Length > 0 ) { string[] firstLines = taFirstLines.Text.Split( new string[] { "\r\n" }, StringSplitOptions.None ); jsMin.FirstLines = firstLines; } // tfKeepLines is a text field, where the user can // specify how many lines of the original file // must be written as is to the minimized file. if ( tfKeepLines.Text.Length > 0 ) { try { int keepLines = Int32.Parse( tfKeepLines.Text ); jsMin.KeepFirstLines = keepLines; } catch { } } using ( StreamReader input = new StreamReader( fupScriptFile.FileContent ) ) { using ( StreamWriter output = new StreamWriter( Response.OutputStream ) ) { // Call the minimizing method. jsMin.Go( input, output ); // Specify the content as text to trigger // the "open/save" dialog. Response.ContentType = "text/javascript"; // Specify that the content should be returned // as an attachment. Response.AddHeader( "Content-disposition", "attachment; filename=" + fupScriptFile.FileName ); } } } catch ( Exception ) { // There was an error --> clear the response // and return a HTML formatted error message. Response.Clear(); Response.ClearHeaders(); Response.ContentType = "text/html"; Response.Write( "We're sorry, there was an error." ); } finally { // Flush the response and close it. Response.Flush(); Response.Close(); // Prevent that anything more is added to the Response Response.End(); } }
Receiving, minimizing and sending a script file in ASP.NET
Posted on Friday, February 23, 2007 4:47 PM Technical stuff , JavaScript , .NET , ASP.NET 2.0 | Back to top

Comments on this post: JavaScript code minimizer (not obfuscator!)

# re: JavaScript code minimizer (not obfuscator!)
Requesting Gravatar...
In code example comments still exists :)
Left by Mike Chaliy on Feb 26, 2007 11:52 AM

# re: JavaScript code minimizer (not obfuscator!)
Requesting Gravatar...
The code example shows how to integrate the JsMin object in an ASP.NET page in order to provide the minimizing functionality to web users. It doesn't show minimized JavaScript code. To test the functionality (and see that all comments are removed), simply submit a JavaScript file using the form on the linked page.

Left by Laurent on Feb 26, 2007 12:06 PM

Comments have been closed on this topic.
Copyright © Laurent Bugnion | Powered by: