Navigate back home
GalaSoft Laurent Bugnion
JavaScript code minimizer (not obfuscator!)
Introduction:

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 ;-)

Download
Live demo






Options

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

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
History

Date Version Description
19.02.2007 V1.1.0
  • First published