uglify-js(2)中有一项Conditional compilation,效果如下
源代码:
/** @const */ var DEBUG = false;
(function () {
    "use strict";
    var longStr = "hello ";
    longStr += "world";
    if (DEBUG) {
        console.log(longStr);
    }
})();
编译后:
var DEBUG=!1;!function(){"use strict";var r="hello ";r+="world"}();
当设置了一个全局变量DEBUG,并且JSDOC标识为CONST(ES6之后则为const DEBUG = false),进行压缩时会将if(DEBUG){}及其中的代码去除掉。
因为只有DEBUG为true时,其中的代码块才能执行,若为false则与直接注释此块代码并无差异。所以我希望能当DEBUG为true时,再将其所在的代码块注释掉,请问该如何操作?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
认证高级PHP讲师