我能够通过创建从IBundleBuilder
继承的类来移除注释。这是为Microsoft ASP.NET Web Optimization Framework 1.1.3这是更新2014年2月20日写的:
public class ScriptBundleBuilder : 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));
Microsoft.Ajax.Utilities.CodeSettings settings = new Microsoft.Ajax.Utilities.CodeSettings();
settings.RemoveUnneededCode = true;
settings.StripDebugStatements = true;
settings.PreserveImportantComments = false;
settings.TermSemicolons = true;
var minifier = new Microsoft.Ajax.Utilities.Minifier();
content.Append(minifier.MinifyJavaScript(Read(f), settings));
}
return content.ToString();
}
private string Read(FileInfo file)
{
using (var r = file.OpenText())
{
return r.ReadToEnd();
}
}
}
public class StyleBundleBuilder : 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));
Microsoft.Ajax.Utilities.CssSettings settings = new Microsoft.Ajax.Utilities.CssSettings();
settings.CommentMode = Microsoft.Ajax.Utilities.CssComment.None;
var minifier = new Microsoft.Ajax.Utilities.Minifier();
content.Append(minifier.MinifyStyleSheet(Read(f), settings));
}
return content.ToString();
}
private string Read(FileInfo file)
{
using (var r = file.OpenText())
{
return r.ReadToEnd();
}
}
}
然后告诉捆绑使用这种生成器。本实施例中为StyleBundle:
public static void RegisterBundles(BundleCollection bundles)
{
var bundle = new StyleBundle("~/Content/themes/base/css");
bundle.Builder = new StyleBundleBuilder();
bundle.Include("~/Content/themes/base/jquery.ui.core.css",
"~/Content/themes/base/jquery.ui.resizable.css",
//etc
);
bundles.Add(bundle);
var scriptBundle = new ScriptBundle("~/bundles/modernizr");
scriptBundle.Builder = new ScriptBundleBuilder();
scriptBundle.Include("~/Scripts/modernizr-*");
bundles.Add(scriptBundle);
BundleTable.EnableOptimizations = true; //for testing
}
此测试/通过改变用户代理来Eureka/1.0
确认在镀铬。
对于至少一些先前版本的Web优化框架(1.0和之前我认为),唯一的区别是最后一个参数。所以它看起来像public virtual string BuildBundleContent(Bundle bundle, BundleContext context, IEnumerable<FileInfo> files)
,只需稍作修改即可完成工作......尽管您只需更新即可。
关于这个问题,有人提出in another recent SO post关于在缩小过程中许可信息如何被剥离...我做了a NuGet Package来解决这些问题。
捆绑和缩小是优化技术,而不是安全措施。评论真的是你的问题吗? –
是的。我不希望以任何方式在css和js文件中看到注释。如何实现这一点? – Andrus
@安德鲁斯我不知道这个“特征”......我想知道他们为什么甚至会把它放在那里? – MikeSmithDev