asp.net - How can I preserve comments that matter in MVC 4 style bundling? -
i have found link:
http://giddyrobot.com/preserving-important-comments-in-mvc-4-bundles/
it shows how same thing javascript , have used make attempt stylebundles, i'm unsure if it's doing things correctly on backend.
is source code available? if not know if seems right? want keep comments start /*!
licenses open source projects normalize included in production.
here have far:
public static void registerbundles(bundlecollection bundles) { // allows keep /*! comments 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 js concatenation token not sure if 1 should have 1 //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(); } }
all of wrapped in public class bundleconfig
, gets called global.asax
.
i'm wondering if csscomment.important
have negative effects , remove , if seems doing want to? when have tested seems correct styling wise, doesn't hurt eyes seeing useful lot of other asp.net devs use open source libraries.
i don't think you've done incorrectly. though approach using ibundlebuilder interface, keep regular comments out of production prying eyes switch user agent, specified in how prevent user-agent: eureka/1 return source code. show steps on how test against in related blog post.
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 } }
i made nuget package (including version scripts) - https://www.nuget.org/packages/licensedbundler/
Comments
Post a Comment