Image for post
Image for post

Weird Minification Behaviour in ASP.NET Core

I recently came across some strange behaviour in our ASP.NET Core 2.2 web application. A colleague of mine who was working on some new functionality, had checked in several Javascript files. These were 3rd party Javascript files to add support for drag & drop. The majority of the files for this 3rd party library were already minified, with the exception of one.

For some reason this one particular Javascript file was not minified. So we added the file to bundleconfig.json in Visual Studio so that our build process would minify the file. The bundleconfig.json minifies several Javascript files and outputs the aggragated file as site.min.js. Whilst I was testing the latest version of the app I was getting all sorts of errors in the browser as many of the Javascriptfunctions were not being found. This seemed strange, as everything had been working perfectly, and all we had done was check in a few Javascript files.

Looking at the site.min.js file that was on the build and test servers, it became apparent that the site.min.js file contained only the contents of the un-minified 3rd party Javascript file. All of the other files we were minifying had somehow been removed from the resultant site.min.js file.

After much investigation I narrowed down the issue to the following command in our build pipeline.

Hide Copy Code

dotnet publish -c release

This command was recreating the site.min.js file, but failing to include all the files specified in the bundleconfig.json with the exception of the un-minified 3rd party Javascript file. I excluded this step from the build process to check, and sure enough, the culprit was definitely this build command.

I managed to solve the problem by manually minifying the culprit Javascript file and adding it to the project in its minified form. I then excluded it from the bundleconfig.json minification process. This has now solved the problem, and everything works perfectly again.

So basically, if you’re including 3rd party Javascript files, make sure you add them to your Visual Studio project in minified form (unless you’re using a CDN of course). Don’t attempt to minify 3rd party files in your build process. Only minify your own Javascriptfiles in your build process. It took me a few hours to diagnose and fix the problem, so hopefully by reading this, I may save someone else the same pain I went through fixing the problem.

A father, cyclist, vegetarian, atheist, geek and multiple award winning technical author. Loves real ale, fine wine and good music. All round decent chap.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store