Asp.net
如何保留 MVC 4 樣式捆綁中重要的註釋?
我找到了這個連結:
http://giddyrobot.com/preserving-important-comments-in-mvc-4-bundles/
它展示瞭如何為 JavaScript 做同樣的事情,我已經用它來嘗試 StyleBundles,但我不確定它是否在後端正確地做事。
原始碼可用嗎?如果沒有,有人知道這是否正確嗎?我想要保留的只是開頭的註釋,
/*!以便像 normalize 這樣的開源項目的許可證可以正確地包含在生產中。這是我到目前為止所擁有的:
public static void RegisterBundles(BundleCollection bundles) { // Allows us to keep /*! comments for licensing purposes var cssBundleSettings = new CssSettings { CommentMode = CssComment.Important }; } public class ConfigurableStyleBundle : Bundle { public ConfigurableStyleBundle(string virtualPath, CssSettings cssSettings) : this(virtualPath, cssSettings, null) { } public ConfigurableStyleBundle(string virtualPath, CssSettings cssSettings, string cdnPath) : base(virtualPath, cdnPath, new[] { new ConfigurableCSSTransform(cssSettings) }) { // commented out from js concatenation token not sure if this one should have one //base.ConcatenationToken = ";"; } } [ExcludeFromCodeCoverage] public class ConfigurableCSSTransform : IBundleTransform { private readonly CssSettings _cssSettings; public ConfigurableCSSTransform(CssSettings cssSettings) { _cssSettings = cssSettings; } public 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(); var content = minifier.MinifyStyleSheet(response.Content, _cssSettings); if (minifier.ErrorList.Count > 0) { GenerateErrorResponse(response, minifier.ErrorList); } else { response.Content = content; } } response.ContentType = "text/css"; } internal static void GenerateErrorResponse(BundleResponse bundle, IEnumerable<object> errors) { var content = new StringBuilder(); content.Append("/* "); content.Append("CSS MinifyError").Append("\r\n"); foreach (object current in errors) { content.Append(current.ToString()).Append("\r\n"); } content.Append(" */\r\n"); content.Append(bundle.Content); bundle.Content = content.ToString(); } }所有這些都包含在
public class BundleConfig並從Global.asax.我只是想知道是否
CssComment.Important會產生負面影響並去除太多,如果這似乎正在做我想要的?當我對其進行測試時,所有的樣式看起來似乎都是正確的,但是讓一些人看到它並沒有什麼壞處,因為這對於許多使用開源庫的其他 ASP.NET 開發人員來說可能很有用。
我不認為你做錯了什麼。雖然我會使用 IBundleBuilder 介面來處理它,因為這也會使正常註釋不會被切換使用者代理的人窺探,就像如何防止使用者代理:Eureka/1 返回原始碼中所指定的那樣。我在這篇相關的部落格文章中展示了一些關於如何對此進行測試的步驟。
public class ConfigurableStyleBuilder : IBundleBuilder { public virtual string BuildBundleContent(Bundle bundle, BundleContext context, IEnumerable<BundleFile> files) { var content = new StringBuilder(); foreach (var file in files) { FileInfo f = new FileInfo(HttpContext.Current.Server.MapPath(file.VirtualFile.VirtualPath)); CssSettings settings = new CssSettings(); settings.CommentMode = Microsoft.Ajax.Utilities.CssComment.Important; var minifier = new Microsoft.Ajax.Utilities.Minifier(); string readFile = Read(f); string res = minifier.MinifyStyleSheet(readFile, settings); if (minifier.ErrorList.Count > 0) { res = PrependErrors(readFile, minifier.ErrorList); content.Insert(0, res); } else { content.Append(res); } } return content.ToString(); } private string PrependErrors(string file, ICollection<ContextError> errors ) { var content = new StringBuilder(); content.Append("/* "); content.Append("CSS MinifyError").Append("\r\n"); foreach (object current in errors) { content.Append(current.ToString()).Append("\r\n"); } content.Append("Minify Error */\r\n"); content.Append(file); return content.ToString(); } private string Read(FileInfo file) { using (var r = file.OpenText()) { return r.ReadToEnd(); } } } public class BundleConfig { public static void RegisterBundles(BundleCollection bundles) { var cssBundle = new ConfigurableStyleBundle("~/Content/css"); cssBundle.Include("~/Content/stylesheet1.css"); cssBundle.Include("~/Content/stylesheet2.css"); bundles.Add(cssBundle); //etc } }我為此製作了一個 NuGet 包(包括腳本版本) - https://www.nuget.org/packages/LicensedBundler/