DCSIMG
Javascript and Css files compressor - new { Name = ”Shay Jacoby” }

new { Name = ”Shay Jacoby” }

Maximum separation, minimum Dependencies, No Injections.

Javascript and Css files compressor

 I was looking for a tool to compress js+css files that i can integrate into project post build event or MSBUILD.

One of the popular tools is  Yui, It works  fine but requieres installed JVM on DEV machine and it's not simple to write a wrapper for it.

I tried also the JSMin, It does the job but works for js files only.

 The best solution that I found is the .NET version of Yahoo.Yui.Compressor.

I integrated the assembly into a console app. and wrote a method that:

1. Gets the js scripts + css folder names as parameters.

2. Finds all files that contains *.debug in name, ignores all other files.

3. Reads file content as string and minify it - returns as minified string.

4. Saves the minified string to file named *.min instead of *.debug.

 

My web project post build event code (Format: "path to console app exe" "js files folder" "css files folder" ):

"$(SolutionDir)Reference Assemblies\tools\ShopEngine.Tools.exe" "$(SolutionDir)Web\Scripts" "$(SolutionDir)Web\Style"

The console app code:

namespace ShopEngine.Tools

{

    #region Using directives

 

    using System;

    using System.IO;

    using Yahoo.Yui.Compressor;

 

    #endregion

 

    /// <summary>

    /// Js + Css Minifier, Assembly taken from:

    /// http://yuicompressor.codeplex.com/

    /// </summary>

    public static class Minifier

    {

        #region Constants

 

        /// <summary>

        /// Debug file

        /// </summary>

        private const string Debug = "debug";

 

        /// <summary>

        /// Minified file

        /// </summary>

        private const string Min = "min";

 

        #endregion Constants

 

        /// <summary>

        /// Main method.

        /// </summary>

        /// <param name="args">The arguments.</param>

        public static void Main(string[] args)

        {

            if (args.Length == 0)

            {

                Console.WriteLine("invalid arguments, no path was specified.");

                return;

            }

 

            var di = new DirectoryInfo(args[0]);

            var files = di.GetFiles(string.Format("*.{0}.js", Debug), SearchOption.AllDirectories);

            var i = 0;

            foreach (var fi in files)

            {

                i++;

                string src = fi.FullName.ToLower();

                string srcText = File.ReadAllText(src);

                string dst = src.Replace(Debug, Min);

 

                string dstText = !string.IsNullOrEmpty(srcText) ? JavaScriptCompressor.Compress(srcText, false) : string.Empty;

 

                using (var streamWriter = new StreamWriter(dst))

                {

                    streamWriter.Write(dstText);

                }

            }

 

            Console.WriteLine("{0} Js Files minified.", i);

 

            // Check for Css folder

            if (args.Length > 1)

            {

                di = new DirectoryInfo(args[1]);

                files = di.GetFiles(string.Format("*.{0}.css", Debug), SearchOption.AllDirectories);

                i = 0;

                foreach (var fi in files)

                {

                    i++;

                    string src = fi.FullName.ToLower();

                    string srcText = File.ReadAllText(src);

                    string dst = src.Replace(Debug, Min);

 

                    string dstText = !string.IsNullOrEmpty(srcText) ? CssCompressor.Compress(srcText) : string.Empty;

                    using (var streamWriter = new StreamWriter(dst))

                    {

                        streamWriter.Write(dstText);

                    }

                }

 

                Console.WriteLine("{0} Css Files minified.", i);

            }

        }

    }

}

 

 

Good luck!

 

תוכן התגובה

Basil Goldman כתב/ה:

Yep. YUI Compressor is a good tool but sometimes can't compress JavaScript, because "we are programmers" forget to insert ";" in the end of line or after prototype scope :-).

# November 14, 2009 8:41 PM

Pure Krome כתב/ה:

Ha kewl :) someone else who is using the port :) Glad it helps you :) :)

# April 5, 2010 2:07 PM
שלח תגובה

(שדה חובה)  

(שדה חובה)  

(אופציונלי)

(שדה חובה) 

Please add 3 and 7 and type the answer here:


Enter the numbers above: