告诉YUI压缩器删除特定的非注释代码

whe*_*hys 7 javascript comments yui-compressor

在我的javascript中,我有一些专门用于调试的代码,我不希望在实时站点中包含这些代码.有没有办法我可以对这些代码进行半注释,以便它们正常运行,但yui压缩器认为它们是注释并删除它们?

例如

for(key in modules) {
  try { 
     MyApp[key].init(modules[key].params);
  } catch (e) {
     console.log("Module " + key + " threw an error");
     break;
  }
}
Run Code Online (Sandbox Code Playgroud)

我希望能够在压缩以部署到实际站点时自动注释掉console.log位.所以也许把代码包装成类似的东西

   //yuiIgnore
         console.log("Module " + key + " threw an error");
   //endyuiIgnore
Run Code Online (Sandbox Code Playgroud)

bbg*_*bbg 8

特别关注console.log声明:

我使用sed,以取代"console""//console"启动压缩机前:

sed -e "s/console/\/\/console/g" originalWithConsoleStatements.js > noConsoleStatements.js
Run Code Online (Sandbox Code Playgroud)

该语句位于shell脚本中,然后启动压缩器.

  • 假设所有console.log语句都在一行上,否则你会得到语法错误,我想...... (9认同)