ASP.NET bundling minifies pre-minified files

2015-12-26 in, bundling

ASP.NET solves the issue of having too many loose resource files by providing a built-in tool to automatically bundle script and stylesheet files. In theory, if you add a script that has both a .js and .min.js file to a bundle, then in debug mode the .js file will be included on the page, while in release the .min.js file (and every other file in the bundle) will be combined into a single file. Unfortunately the reality is different, and you can see ASP.NET trying to re-minify the .min.js file instead of just concatenating it.

This problem manifests with odd issues, where the generated files say things like /* Minification failed. Returning unminified contents. */ - this is a failure due to it attempting to re-minify an already minified file. You can see this problem in both stylesheet and script bundles. The solution is easy: simply change the bundle registration from ScriptBundle or StyleBundle to Bundle. The rest of the syntax is exactly the same. Now the bundle will use the correct behavior, including the .js files as separate script tags when in debug mode and concatenating the .min.js files with post-processing when in release mode.