Exclude debug JavaScript code during minification

后端 未结 11 1995
庸人自扰
庸人自扰 2020-11-28 07:16

I\'m looking into different ways to minify my JavaScript code including the regular JSMin, Packer, and YUI solutions. I\'m really interested in the new Google Closure Compil

相关标签:
11条回答
  • 2020-11-28 07:25

    Adding logic to every place in your code where you are logging to the console makes it harder to debug and maintain.

    If you are already going to add a build step for your production code, you could always add another file at the top that turns your console methods into noop's.

    Something like:

    console.log = console.debug = console.info = function(){};
    

    Ideally, you'd just strip out any console methods, but if you are keeping them in anyway but not using them, this is probably the easiest to work with.

    0 讨论(0)
  • 2020-11-28 07:27

    If you're using UglifyJS2, you can use the drop_console argument to remove console.* functions.

    0 讨论(0)
  • 2020-11-28 07:32

    A good solution in this case might be js-build-tools which supports 'conditional compilation'.

    In short you can use comments such as

    // #ifdef debug
    var trace = debug.getTracer("easyXDM.Rpc");
    trace("constructor");
    // #endif
    

    where you define a pragma such as debug.

    Then when building it (it has an ant-task)

    //this file will not have the debug code
    <preprocess infile="work/easyXDM.combined.js" outfile="work/easyXDM.js"/>
    //this file will        
    <preprocess infile="work/easyXDM.combined.js" outfile="work/easyXDM.debug.js" defines="debug"/>
    
    0 讨论(0)
  • 2020-11-28 07:34

    here's the (ultimate) answer for closure compiler :

    /** @const */
    var LOG = false;
    ...
    LOG && log('hello world !'); // compiler will remove this line
    ...
    

    this will even work with SIMPLE_OPTIMIZATIONS and no --define= is necessary !

    0 讨论(0)
  • 2020-11-28 07:34

    Even though its an old question. I stumbled upon the same issue today and found that it can be achieved using CompilerOptions.

    I followed this thread.

    We run the compiler, from Java, on our server before sending the code to the client. This worked for us in Simple mode.

    private String compressWithClosureCompiler(final String code) {
        final Compiler compiler = new Compiler();
        final CompilerOptions options = new CompilerOptions();
        Logger.getLogger("com.google.javascript.jscomp").setLevel(Level.OFF);
        if (compressRemovesLogging) {
            options.stripNamePrefixes = ImmutableSet.of("logger");
            options.stripNameSuffixes = ImmutableSet.of("debug", "dev", "info", "error",
                    "warn", "startClock", "stopClock", "dir");
        }
        CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
    
        final JSSourceFile extern = JSSourceFile.fromCode("externs.js", "");
        final JSSourceFile input = JSSourceFile.fromCode("input.js", code);
        compiler.compile(extern, input, options);
        return compiler.toSource();
    }
    

    It will remove all the calls to logger.debug, logger.dev...etc.etc

    0 讨论(0)
  • 2020-11-28 07:34

    I am with @marcel-korpel. Isn't perfect but works. Replace the debug instructions before minification. The regular expression works in many places. Watch out unenclosed lines.

    /console\.[^;]*/gm
    

    Works on:

    ;;;     console.log("Starting process");
    console.log("Starting process");
    console.dir("Starting process");;;;;
    console.log("Starting "+(1+2)+" processes"); iamok('good');
    console.log('Message ' +
        'with new line'
    );
    console.group("a");
    console.groupEnd();
    swtich(input){
        case 1 : alert('ok'); break;
        default: console.warn("Fatal error"); break;
    }
    

    Don't works:

    console.log("instruction without semicolon")
    console.log("semicolon in ; string");
    
    0 讨论(0)
提交回复
热议问题