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

时间:2010-05-13 10:30:33

标签: 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;
  }
}

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

这样的东西
   //yuiIgnore
         console.log("Module " + key + " threw an error");
   //endyuiIgnore

1 个答案:

答案 0 :(得分:8)

特别关注console.log陈述:

我在启动压缩器之前使用sed"console"替换为"//console"

sed -e "s/console/\/\/console/g" originalWithConsoleStatements.js > noConsoleStatements.js

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