/// <summary>
        /// Minifies all JavaScript files in a given directory and writes out the
        /// minified files to a new file extensions (.min.js for example).
        ///
        /// This method can be integrated into the build process, or as part of an
        /// application's startup to always create new minified scripts as needed.
        /// Scripts are only minified if the minified files don't already exist and are
        ///  older than the corresponding JavaScript file.
        ///
        /// A common usage scenario is to call this static method from
        /// Application_Start:
        ///
        /// &lt;&lt;code lang="C#"&gt;&gt;void Application_Start(object sender,
        /// EventArgs e)
        /// {
        ///     // creates minify scripts if don't exist or are changed
        ///     // NOTE: REQUIRES that IIS/ASP.NET Account has writes to write
        /// here!
        ///
        /// Westwind.Web.Controls.JavaScriptMinifier.MinifyDirectory(Server.MapPath("~/
        /// scripts"), ".min.js", true);
        /// }&lt;&lt;/code&gt;&gt;
        ///
        /// to always ensure minified files are in sync with corresponding JavaScript
        /// files.
        /// <seealso>Class JavaScriptMinifier                                                        </seealso>
        /// </summary>
        /// <param name="path">
        /// The path where files are to be minfied
        /// </param>
        /// <param name="minExtension">
        /// The extension for the minified files (ie. .min.js). Include leading dot!
        /// </param>
        /// <param name="recursive">
        /// Determines whether nested directories are also included
        /// </param>
        /// <remarks>
        /// Note that if you use this script from within an ASP.NET application it's
        /// best to hook it to a Application_Start or a static constructor so it only
        /// fires once.
        ///
        /// When called from ASP.NET this routine REQUIRES that the server account that
        ///  ASP.NET/IIS AppPool runs under (NETWORK SERVICE by default) has rights to
        /// write out the file to the folder specified. Otherwise an exception occurs.
        /// </remarks>
        public static void MinifyDirectory(string path, string minExtension, bool recursive)
        {
            JavaScriptMinifier min = new JavaScriptMinifier();

            minExtension = minExtension.ToLower();
            if (!minExtension.StartsWith("."))
            {
                minExtension = "." + minExtension;
            }


            string[] files = null;
            try
            {
                files = Directory.GetFiles(path);
            }
            catch
            {
                throw new InvalidOperationException("Invalid or inaccessible path to create Min Scripts in: " + path);
            }

            try
            {
                foreach (string file in files)
                {
                    string extension = Path.GetExtension(file).ToLower();

                    if (extension == ".js" && !file.EndsWith(minExtension))
                    {
                        string   minFile   = file.Replace(".js", minExtension);
                        DateTime fileAT    = File.GetLastWriteTimeUtc(file);
                        DateTime minFileAT = File.GetLastWriteTimeUtc(minFile);

                        if (!File.Exists(minFile) || fileAT > minFileAT)
                        {
                            min.Minify(file, file.Replace(".js", minExtension));
                        }
                    }
                }

                if (recursive)
                {
                    string[] dirs = Directory.GetDirectories(path);
                    foreach (string dir in dirs)
                    {
                        if (!dir.StartsWith("."))
                        {
                            MinifyDirectory(dir, minExtension, true);
                        }
                    }
                }
            }
            catch
            {
                throw new AccessViolationException("Couldn't create Min Scripts in: " + path + ". Make sure ASP.NET has permissions to write in this path.");
            }
        }
 /// <summary>
 /// Very basic script optimization to reduce size:
 /// Remove any leading white space and any lines starting
 /// with //. 
 /// </summary>
 /// <param name="Script"></param>
 /// <returns></returns>
 public static string OptimizeScript(string Script)
 {
     JavaScriptMinifier min = new JavaScriptMinifier();
     return min.MinifyString(Script);
 }
        /// <summary>
        /// Minifies all JavaScript files in a given directory and writes out the 
        /// minified files to a new file extensions (.min.js for example).
        /// 
        /// This method can be integrated into the build process, or as part of an 
        /// application's startup to always create new minified scripts as needed. 
        /// Scripts are only minified if the minified files don't already exist and are
        ///  older than the corresponding JavaScript file.
        /// 
        /// A common usage scenario is to call this static method from 
        /// Application_Start:
        /// 
        /// &lt;&lt;code lang="C#"&gt;&gt;void Application_Start(object sender, 
        /// EventArgs e)
        /// {
        ///     // creates minify scripts if don't exist or are changed
        ///     // NOTE: REQUIRES that IIS/ASP.NET Account has writes to write 
        /// here!
        ///     
        /// Westwind.Web.Controls.JavaScriptMinifier.MinifyDirectory(Server.MapPath("~/
        /// scripts"), ".min.js", true);
        /// }&lt;&lt;/code&gt;&gt;
        /// 
        /// to always ensure minified files are in sync with corresponding JavaScript 
        /// files.
        /// <seealso>Class JavaScriptMinifier                                                        </seealso>
        /// </summary>
        /// <param name="path">
        /// The path where files are to be minfied
        /// </param>
        /// <param name="minExtension">
        /// The extension for the minified files (ie. .min.js). Include leading dot!
        /// </param>
        /// <param name="recursive">
        /// Determines whether nested directories are also included
        /// </param>
        /// <remarks>
        /// Note that if you use this script from within an ASP.NET application it's 
        /// best to hook it to a Application_Start or a static constructor so it only 
        /// fires once.
        /// 
        /// When called from ASP.NET this routine REQUIRES that the server account that
        ///  ASP.NET/IIS AppPool runs under (NETWORK SERVICE by default) has rights to 
        /// write out the file to the folder specified. Otherwise an exception occurs.
        /// </remarks>
        public static void MinifyDirectory(string path, string minExtension, bool recursive)
        {
            JavaScriptMinifier min = new JavaScriptMinifier();

            minExtension = minExtension.ToLower();
            if (!minExtension.StartsWith("."))
                minExtension = "." + minExtension;


            string[] files = null;
            try
            {
                files = Directory.GetFiles(path);
            }
            catch
            {
                throw new InvalidOperationException("Invalid or inaccessible path to create Min Scripts in: " + path);
            }

            try
            {
                foreach (string file in files)
                {
                    string extension = Path.GetExtension(file).ToLower();

                    if (extension == ".js" && !file.EndsWith(minExtension))
                    {
                        string minFile = file.Replace(".js", minExtension);
                        DateTime fileAT = File.GetLastWriteTimeUtc(file);
                        DateTime minFileAT = File.GetLastWriteTimeUtc(minFile);

                        if (!File.Exists(minFile) || fileAT > minFileAT)
                            min.Minify(file, file.Replace(".js", minExtension));
                    }
                }

                if (recursive)
                {
                    string[] dirs = Directory.GetDirectories(path);
                    foreach (string dir in dirs)
                    {
                        if (!dir.StartsWith("."))
                            MinifyDirectory(dir, minExtension, true);
                    }
                }
            }
            catch
            {
                throw new AccessViolationException("Couldn't create Min Scripts in: " + path + ". Make sure ASP.NET has permissions to write in this path.");
            }
        }
Example #4
0
        /// <summary>
        /// Very basic script optimization to reduce size:
        /// Remove any leading white space and any lines starting
        /// with //.
        /// </summary>
        /// <param name="Script"></param>
        /// <returns></returns>
        public static string OptimizeScript(string Script)
        {
            JavaScriptMinifier min = new JavaScriptMinifier();

            return(min.MinifyString(Script));
        }