Last active
August 29, 2015 14:07
-
-
Save aolde/17236c0a22adeef81490 to your computer and use it in GitHub Desktop.
Strip console debug statements with ASP.NET Bundling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var appJs = new ScriptBundle("~/assets/app-js") | |
.Include( | |
"~/assets/scripts/main.js" | |
); | |
appJs.Transforms.Clear(); | |
appJs.Transforms.Add(new ConfigurableJsMinify | |
{ | |
CodeSettings = new CodeSettings | |
{ | |
EvalTreatment = EvalTreatment.MakeImmediateSafe, | |
PreserveImportantComments = false, | |
StripDebugStatements = true, | |
DebugLookupList = "console" | |
} | |
}); | |
bundles.Add(appJs); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ConfigurableJsMinify : IBundleTransform | |
{ | |
private static string JsContentType = "text/javascript"; | |
public ConfigurableJsMinify() | |
{ | |
CodeSettings = new CodeSettings | |
{ | |
EvalTreatment = EvalTreatment.MakeImmediateSafe, | |
PreserveImportantComments = false | |
}; | |
} | |
public CodeSettings CodeSettings { get; set; } | |
/// <summary> | |
/// Transforms the bundle contents by applying javascript minification. | |
/// </summary> | |
/// <param name="context">The context associated with the bundle.</param><param name="response">The <see cref="T:System.Web.Optimization.BundleResponse"/>.</param> | |
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(); | |
string str = minifier.MinifyJavaScript(response.Content, CodeSettings); | |
if (minifier.ErrorList.Count > 0) | |
{ | |
GenerateErrorResponse(response, (IEnumerable<object>)minifier.ErrorList); | |
} | |
else | |
{ | |
response.Content = str; | |
} | |
} | |
response.ContentType = JsContentType; | |
} | |
private static void GenerateErrorResponse(BundleResponse bundle, IEnumerable<object> errors) | |
{ | |
var stringBuilder = new StringBuilder(); | |
stringBuilder.Append("/* "); | |
stringBuilder.Append("Minification of JavaScript failed:").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 = ((object)stringBuilder).ToString(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var debug = false; | |
function app() { | |
debug && {}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var debug = false; | |
function app() { | |
///#DEBUG | |
alert('Debugging something here!'); | |
///#ENDDEBUG | |
console.log('logging some values', document); | |
debug && console.group("init modules inside"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment