BundleTable.EnableOptimizations true breaks jquery-ui all.css

元气小坏坏 提交于 2019-12-30 23:51:30

问题


In an Asp.Net MVC 5 application, I am creating a style bundle in my egisterBundles method.
I'm using jquery-ui.
Instead of listing all of the jquery-ui css files individually I'm using all.css, which imports all the rest.
The code looks like this:

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

And all.css contains two lines:

@import "base.css";
@import "theme.css";

This works, but only when I set

BundleTable.EnableOptimizations = false.

When I set

BundleTable.EnableOptimizations = true

then none of the jquery css loads.

Of course there is an easy workaround; I can individually add the jquery-ui css files to the bundle.
But I am curious: why does all.css break when the css files are bundled and minified?
This does not appear to be browser-specific, as I have the same problem in both IE9 and Chrome 39.


回答1:


According to this answer the default minifier simply does not support the @import directive:

MVC4 bundling CSS failed Unexpected token, found '@import'

Also, the jquery-ui css files contain relative paths to images, so the virtual path of the bundle must allow the browser to find the relative path to the images, for example:

bundles.Add(new StyleBundle("~/Content/themes/base/jqueryui")
   .Include("~/Content/themes/base/core.css" [and other desired css files]));

And on the cshtml page:

@Styles.Render("~/Content/themes/base/jqueryui")

See this link for explication: MVC4 StyleBundle not resolving images



来源:https://stackoverflow.com/questions/27569633/bundletable-enableoptimizations-true-breaks-jquery-ui-all-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!