[英]MVC4 bundling CSS failed Unexpected token, found '@import'
我正在嘗試使用捆綁來組合和縮小一些CSS文件。 在我的Global.aspx.cs Application_Start
我有以下內容:
var jsBundle = new Bundle("~/JSBundle", new JsMinify());
jsBundle.AddDirectory("~/Scripts/", "*.js", false);
jsBundle.AddFile("~/Scripts/KendoUI/jquery.min.js");
jsBundle.AddFile("~/Scripts/KendoUI/kendo.web.min.js");
BundleTable.Bundles.Add(jsBundle);
var cssBundle = new Bundle("~/CSSBundle", new CssMinify());
cssBundle.AddDirectory("~/Content/", "*.css", false);
cssBundle.AddDirectory("~/Content/themes/base/", "*.css", false);
cssBundle.AddFile("~/Styles/KendoUI/kendo.common.min.css");
cssBundle.AddFile("~/Styles/KendoUI/kendo.default.min.css");
BundleTable.Bundles.Add(cssBundle);
在我的.cshtml文件中,我有以下內容:
<link href="/CSSBundle" rel="stylesheet" type="text/css" />
<script src="/JSBundle" type="text/javascript"></script>
但是,當我查看我的bundle CSS文件的源代碼時,它具有以下內容:
/* Minification failed. Returning unminified contents.
(40,1): run-time error CSS1019: Unexpected token, found '@import'
(40,9): run-time error CSS1019: Unexpected token, found '"jquery.ui.base.css"'
......還有更多
關於如何解決這個問題的任何想法?
我確實將它縮小到以下行:
cssBundle.AddDirectory("~/Content/themes/base/", "*.css", false);
如果我只有那行代碼,我會得到相同的錯誤。
這里有一些問題:
css問題是由於包含jquery.ui.all.css,因為默認的minifier不支持后續導入,這不是你想要做的,因為它會包括所有的jquery ui css文件。 所以你要做的不是使用* .css,而是明確列出你想要包含的jquery ui文件:
bundles.Add(new StyleBundle("~/Content/themes/base/css").Include( "~/Content/themes/base/jquery.ui.core.css", "~/Content/themes/base/jquery.ui.resizable.css", "~/Content/themes/base/jquery.ui.selectable.css", "~/Content/themes/base/jquery.ui.accordion.css", "~/Content/themes/base/jquery.ui.autocomplete.css", "~/Content/themes/base/jquery.ui.button.css", "~/Content/themes/base/jquery.ui.dialog.css", "~/Content/themes/base/jquery.ui.slider.css", "~/Content/themes/base/jquery.ui.tabs.css", "~/Content/themes/base/jquery.ui.datepicker.css", "~/Content/themes/base/jquery.ui.progressbar.css", "~/Content/themes/base/jquery.ui.theme.css"));
其次,您希望使用Script / Styles.Render方法,而不是在執行時顯式引用bundle url,因為在調試模式下,幫助程序將自動不捆綁/縮小並呈現對每個腳本/樣式資產的單獨引用,並且還會將捆綁內容的指紋附加到URL中,以便瀏覽器緩存可以正常工作。
@Scripts.Render("~/JSBundle") and @Styles.Render("~/CSSBundle")
你也可以使用StyleBundle / ScriptBundle,它只是合成糖而不必傳遞新的Css / JsMinify。
您還可以查看本教程以獲取更多信息: Bundling Tutorial
或者你可以做的是為CssMinify編寫自己的BundleTransform,當然你需要這樣的靈活性。 因此,例如,BundleConfig.cs中的代碼如下所示:
using System;
using System.Web.Optimization;
using StyleBundle = MyNamespace.CustomStyleBundle;
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new StyleBundle("~/Content/themes/base/css")
.IncludeDirectory("~/Content/themes/base", "*.css"));
}
}
然后你需要添加的是:
public class CustomStyleBundle : Bundle
{
public CustomStyleBundle(string virtualPath, IBundleTransform bundleTransform = null)
: base(virtualPath, new IBundleTransform[1]
{
bundleTransform ?? new CustomCssMinify()
})
{
}
public CustomStyleBundle(string virtualPath, string cdnPath, IBundleTransform bundleTransform = null)
: base(virtualPath, cdnPath, new IBundleTransform[1]
{
bundleTransform ?? new CustomCssMinify()
})
{
}
}
public class CustomCssMinify : IBundleTransform
{
private const string CssContentType = "text/css";
static CustomCssMinify()
{
}
public virtual void Process(BundleContext context, BundleResponse response)
{
if (context == null)
throw new ArgumentNullException("context");
if (response == null)
throw new ArgumentNullException("response");
if (!context.EnableInstrumentation)
{
var minifier = new Minifier();
FixCustomCssErrors(response);
string str = minifier.MinifyStyleSheet(response.Content, new CssSettings()
{
CommentMode = CssComment.None
});
if (minifier.ErrorList.Count > 0)
GenerateErrorResponse(response, minifier.ErrorList);
else
response.Content = str;
}
response.ContentType = CssContentType;
}
/// <summary>
/// Add some extra fixes here
/// </summary>
/// <param name="response">BundleResponse</param>
private void FixCustomCssErrors(BundleResponse response)
{
response.Content = Regex.Replace(response.Content, @"@import[\s]+([^\r\n]*)[\;]", String.Empty, RegexOptions.IgnoreCase | RegexOptions.Multiline);
}
private static void GenerateErrorResponse(BundleResponse bundle, IEnumerable<object> errors)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("/* ");
stringBuilder.Append("CSS Minify Error").Append("\r\n");
foreach (object obj in errors)
stringBuilder.Append(obj.ToString()).Append("\r\n");
stringBuilder.Append(" */\r\n");
stringBuilder.Append(bundle.Content);
bundle.Content = stringBuilder.ToString();
}
}
如果您需要更多修復/錯誤,可以在FixCustomCssErrors方法中擴展此邏輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.