14

I have 4 files:

  • a.css
  • a.min.css
  • b.css
  • b.min.css

they are added to bundle in following way:

bundles.Add(new StyleBundle("~/Content/acss").Include("~/Content/a.css", "~/Content/b.css"));

When running application in debug all is rendered properly:

 <link href="/Content/a.css" rel="stylesheet"/>
 <link href="/Content/b.css" rel="stylesheet"/>

However when running in release I have it rendered in following way:

 <link href="/Content/acss?v=mUdXE7_fXKjICzE_XteIB1Igy6TekX1QFh-BtY6fFUw1" rel="stylesheet"/>

And inside I'm founding:

/* Minification failed. Returning unminified contents. (24,708): run-time error CSS1030: Expected identifier, found '.'

So I have two questions:

  1. Why it is not working according to docs online? All information says that it chooses min file if available for release version?
  2. How to make it working according to documentation?
Marcin
  • 3,232
  • 4
  • 31
  • 48
  • "Why it is not working according to docs online?" "How to make it working according to documentation?" - Which docs are you looking at? – Pluc Oct 19 '12 at 11:51
  • _CSS1030: Expected identifier, found '.'_ Looks like you have an error in your css, which can fail the minification. – ZippyV Oct 19 '12 at 13:41
  • @ZippyV Minification is not working with some browser specific tags. And the question is not related to the error, but to why bundling is not using min file, that is already in folder. – Marcin Oct 19 '12 at 14:36
  • @Pluc, I missed this question, and answered below your answer. – Marcin Oct 19 '12 at 14:37

3 Answers3

38

I had the exact same issue you had, my solution contained css/js files accompanied by their .min files that I had used Web Essentials to minimize.

If I used the bundles in debug mode, everything would work correctly and all of the individual non-minimized files would be loaded in my app. However, if I set BundleTable.EnableOptimizations = true; then I would get errors because it had trouble minimizing my files.

Based on http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification where it states:

For ASP.NET MVC 4, this means with a debug configuration, the file jquery-1.7.1.js will be added to the bundle. In a release configuration, jquery-1.7.1.min.js will be added. 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.

I expected it to simply load up my already minimized files and just bundle them. What I believe is implied, but missing in the documentation, is that it will also again minimize my already minimized files, which wasn't working and causing errors in the output.

I found http://aspnetoptimization.codeplex.com/workitem/56 which mentions:

You can skip minification simply by creating bundles with no transform, i.e. don't create ScriptBundles, just normal Bundles.

This turned out to be the answer to my issue. By setting both my ScriptBundle and StyleBundle to just type Bundle, I now get the correct bundling without the minimizing.

In debug, all of my regular css/jss files are loaded indidivually. When I set it to non-debug, everything is bundled and it automatically chooses all of the .min files.

Matt
  • 575
  • 5
  • 10
  • 2
    changing ScriptBundle and StyleBundle to regular Bundle did it for me. But why would microsoft not by default use Bundle in a default mvc project when it's loading both jquery-{version}.js and jquery-{version}-min.js? – Mark Homans Feb 05 '14 at 14:31
  • Thanks! Seems like the the main quirk is that it is minimizing the .min files. – Oskar Sjöberg Jun 29 '16 at 12:28
  • What to do if you would like a single bundle using existing min files where available and otherwise minimize with ASP.NET:s minification? – Oskar Sjöberg Jun 29 '16 at 12:39
1

Your actual question is not answerable because it is already acting as documentation states (which is why I asked what documentation you were looking it, but you have chosen to ignore my comment).

If you want to keep the same behavior in release mode, use BundleTable.EnableOptimizations = false; in your Global.asax. This will turn off the bundling and minification that is active in debug mode only. Then to use your own minified css / js files, simply point your bundles path to the minified versions.

According to documentation, you could also use "~/Content/a{version}.css" which would use non-minified version in debug mode and minified version for release. However, I have not tried that.

Pluc
  • 2,909
  • 1
  • 22
  • 36
  • I didn't ignore your comment. I don't see any one from you with question. The only one I can see is related to dots in file names. Maybe I'm blind but cannot see any question. And answering it now, [here is] (http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification) where I looked. There is folloing statement: _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._ – Marcin Oct 19 '12 at 14:29
  • Please read the 5 lines after the quote you posted on the link you provided. This is also where I found my information to post this reply but you are not doing what is mentionned on that tutorial. – Pluc Oct 19 '12 at 15:13
  • I changed my file to pattern: a-1.css, a-1.min.css and replace bundle with a-{version}.css and behaviour is even worse. Becuase it is not rendering at all neither debug nor release. – Marcin Oct 20 '12 at 08:56
0

Have you looked a Bundle Transformer, it's 1.6.5 version introduced a property called usePreMinifiedFiles which enables/disables usage of pre-minified files.

Have you set BundleTable.EnableOptimizations = true;

From the documentation

Note: Unless EnableOptimizations is true or the debug attribute in the compilation Element in the Web.config file is set to false, files will not be bundled or minified. Additionally, the .min version of files will not be used, the full debug versions will be selected. EnableOptimizations overrides the debug attribute in the compilation Element in the Web.config file

dove
  • 20,469
  • 14
  • 82
  • 108
  • 1
    No not yet, before I will start using 3rd party tools I would like the get an answer why Bundling is not working according to documentation. – Marcin Oct 19 '12 at 10:24
  • could you try with longer named css, example apple.css and banana.css. Just wondering if there is a clash with some minified variable a or b. Granted this is complete guess and maybe well off the mark with that. – dove Oct 19 '12 at 10:30
  • this is just example. my files in fact are called: _myfile.common.css_, _myfile.layout.all.css_, _myfile.common.min.css_, _myfile.layout.all.min.css_ – Marcin Oct 19 '12 at 10:42
  • ah, that might explain it. try renaming to exclude the '.'. That is renaming to myfileCommmon.css and myfileCommon.min.css and likewise for layout – dove Oct 19 '12 at 10:46
  • 1
    a dash might work as well, which might read better for you (myfile-common etc) but try above first. – dove Oct 19 '12 at 10:47
  • changing names doesn't help, also after installing bundle tarnsformer I still have the same issue. It still minifies files. – Marcin Oct 19 '12 at 11:24
  • using dots is standard... a LOT of libraries does that, including jQuery UI and other big popular ones.... – Pluc Oct 19 '12 at 11:56
  • @Pluc you're right, that '.' in error most likely refers to compile error in css file – dove Oct 19 '12 at 12:11
  • @Marcin updated answer with BundleTable.EnableOptimizations = true; note – dove Oct 19 '12 at 12:14
  • But I don't have issue with debug version. Debug is proper, I have issue with relase version which is minifying itself instead of using provided min file. I don't want to use normal version. I want to bundle use my min file that is provided – Marcin Oct 19 '12 at 12:18
  • ah, i get you, it is optimizing otherwise you wouldn't get that issue. it does seem strange. can you check your bin\release folder and confirm that the .min files have been copied there? – dove Oct 19 '12 at 12:41