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:
<>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); }<>
to always ensure minified files are in sync with corresponding JavaScript files.
public static void MinifyDirectory(string path, string minExtension, bool recursive)
Parameters
path
The path where files are to be minfied
minExtension
The extension for the minified files (ie. .min.js). Include leading dot!
recursive
Determines whether nested directories are also included
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.
See also:
Class JavaScriptMinifier© West Wind Technologies, 1996-2016 • Updated: 12/12/15
Comment or report problem with topic