0

My current bundle config concatenates all the script files and minifies it and generates a single file.but is not removing the console.log lines from the js files,how to add configuration to remove console.log() lines from the files.my current config setting is

       bundles.Add(
            new Bundle(
                "~/Scripts/js-bundle",
                new IBundleTransform[] {new JsMinify() }).Include(
            "~/Scripts/script1.js",
            "~/Scripts/script2.js",
            "~/Scripts/script3.js"))

I tried adding LogRemoverTransform() but i am getting some error

enter image description here

yasarui
  • 6,209
  • 8
  • 41
  • 75
  • Possible duplicate of [How to remove JS logging calls in our prod build of our mvc3 web app?](https://stackoverflow.com/questions/10889237/how-to-remove-js-logging-calls-in-our-prod-build-of-our-mvc3-web-app) – adiga Oct 03 '17 at 11:56
  • when i include LogRemoverTransform() i am getting call not found error – yasarui Oct 03 '17 at 11:58
  • you need to implement `IBundleTransform` https://stackoverflow.com/questions/18509506/search-and-replace-in-javascript-before-bundling – adiga Oct 03 '17 at 12:09
  • i have implemented IBundleTransform. new jsMinify() and new BabelTransform() was working fine. when i added new LogRemoverTransform i am getting the error. – yasarui Oct 03 '17 at 12:14
  • didn't see the update. What is the error? – adiga Oct 03 '17 at 13:06
  • @adiga pls find the update – yasarui Oct 03 '17 at 13:32
  • if you have `LogRemoverTransform.cs` in your porject, then you should see the option to add the `namespace` for that class in "*Show Potential Fixes*" link on that error. – adiga Oct 03 '17 at 13:38
  • @adiga i dont know how the code should be for LogRemoverTransform.cs. can you pls give me sample how it should be – yasarui Oct 03 '17 at 14:02

1 Answers1

4

You need to create your own Bundle Transformation, by implementing IBundleTransform. You can use this regex (borrowed from this question) to get the instances of console.log, console.info etc and replace them with empty strings.

public class LogRemoverTransform : IBundleTransform
{
    public void Process(BundleContext context, BundleResponse response)
    {
        var compiled = string.Empty;
        var pattern = "console.(log|debug|info|warn|error|assert|dir|dirxml|trace|group|groupEnd|time|timeEnd|profile|profileEnd|count)\\((.*)\\);?";

        foreach (var file in response.Files)
        {
           // read text from js file, and replace the the matched parts with empty string
            compiled += Regex.Replace(File.ReadAllText(HttpContext.Current.Server.MapPath(file.IncludedVirtualPath)), pattern, string.Empty);
        }

        response.Content = compiled;
        response.ContentType = "text/javascript";
    }
}

And then in your BundleConfig:

bundles.Add(new Bundle("~/Scripts/js-bundle",  new LogRemoverTransform(), new JsMinify())
                  .Include("~/Scripts/script1.js",
                           "~/Scripts/script2.js",
                           "~/Scripts/script3.js"));

If the js files are very big, use StreamReader instead of File.ReadAllText().

adiga
  • 34,372
  • 9
  • 61
  • 83