0

Is it possible using the .NET minification / bundling mechanism to create a bundle that will search for the minified files in a subdirectory:

Uncompressed:

/css/jquery-ui.css
/css/fullcalendar.css
/css/bootstrap.css

Minified:

/css/minified/jquery-ui.min.css
/css/minified/fullcalendar.min.css
/css/minified/bootstrap.css

I have about 12 separate stylesheets and 20+ scripts that I have and with the compressed versions that is doubled. I would like to keep them separated just for a bit of organization. Is this possible?

Dismissile
  • 32,564
  • 38
  • 174
  • 263

1 Answers1

2

The way that bundling and minification is done in Asp.Net MVC is through dynamic processing at runtime. When you create a bundle, it can be injected into any Asp.Net page. If you are in Debug Mode, the bundler will send out each item in the bundle in their full form, for ease of debug. If you are in Release Mode, the bundler automatically parses the contents of the individual files in the bundle into a single in memory filestream, which it then minifies. There is no need to explicitly include both the full and minified versions of your css/javascript files in the project. However, as per the ASP.Net Documentation:

The bundling framework follows several common conventions such as:

  • Selecting “.min” file for release when “FileX.min.js” and “FileX.js” exist.
  • Selecting the non “.min” version for debug.
  • Ignoring “-vsdoc” files (such as jquery-1.7.1-vsdoc.js), which are used only by IntelliSense.

Basically, if you have both the full and minified versions, then the framework will prefer the use of the pre-minified version over minifying the file itself.

Upon loading your page in Release Mode, the effect of bundling and minification presents itself as a single css or js file stream for the entire bundle, represented by single call to the server, e.g. http://localhost/MvcBM_time/bundles/AllMyScripts?v=r0sLDicvP58AIXN_mc3QdyVvVj5euZNzdsa2N1PKvb81.

Effectively, a unique route is created matching the name of the bundle (AllMyScripts in this example). The query string v has a value token that is a unique identifier used for caching. As long as the bundle doesn't change, the ASP.NET application will request the AllMyScripts bundle using this token. If any file in the bundle changes, the ASP.NET optimization framework will generate a new token, guaranteeing that browser requests for the bundle will get the latest bundle.

As for including files from multiple directories, the easiest method would be to use wildcard pattern matching. The virtual path specified in the Include method and the search pattern in the IncludeDirectory method can accept one * wildcard character as a prefix or suffix to in the last path segment. The search string is case insensitive. The IncludeDirectory method has the option of searching subdirectories.

Claies
  • 22,124
  • 4
  • 53
  • 77
  • I know all of this. I just want the bundler to use my .min files if they exist, but I want them to be in another directory from the main one. – Dismissile Oct 13 '13 at 22:50
  • yes, as I stated at the end, the `IncludeDirectory` method has the option of searching subdirectories. for example, `bundles.Add(new StyleBundle("~/allStyles").IncludeDirectory("~/Styles", "*.css", true));` Would add everything in the `/Styles` directory and all of it's subdirectories – Claies Oct 13 '13 at 23:00
  • I guess that sort of gets what I want. – Dismissile Oct 13 '13 at 23:33
  • You could always write your own bundle that looks in another directory. – Simon Halsey Oct 14 '13 at 02:05