如何使用BundleTransformer YuiJsMinifier删除注释

时间:2013-12-30 00:13:12

标签: javascript asp.net-mvc-5 bundling-and-minification yui-compressor bundletransformer

我正在使用BundleTransformer来缩小cssjs资源

        <yui>
            <css compressionType="Standard" removeComments="true" lineBreakPosition="-1" />
            <js compressionType="Standard" obfuscateJavascript="true" preserveAllSemicolons="false" disableOptimizations="false" ignoreEval="false" severity="0" lineBreakPosition="-1" encoding="UTF8" threadCulture="en-us" />
        </yui>

正如您在css所看到的,可以指定removeComments="true" 但在js中没有这样的选择。

我认为YUI是压缩机removes comments by default。是的,有点删除,但它仍然留下这样的评论:

/* NUGET: BEGIN LICENSE TEXT
 *
 *Bla bla bla
 *
 * NUGET: END LICENSE TEXT */

/*!
 * Bla
 * Licensed under http://www.apache.org/licenses/LICENSE-2.0
 */

看起来没有办法强迫YIU js minifier删除评论。

https://github.com/yui/yuicompressor

  

以/ *开头的C风格评论!保存完好。这很有用   包含版权/许可证信息的评论

使用BundleTransformer完全删除捆绑的缩小输出文件中的所有类型的注释,我能做些什么吗? Google page speed强烈建议我这样做。

2 个答案:

答案 0 :(得分:1)

YUI压缩器不支持删除重要评论。

我建议您安装BundleTransformer.MicrosoftAjax包。然后将MicrosoftAjaxCssMinifierMicrosoftAjaxJsMinifier注册为默认minifiers,并将以下配置设置添加到Web.config文件中:

<configuration>
    …
    <bundleTransformer xmlns="http://tempuri.org/BundleTransformer.Configuration.xsd">
        …
        <microsoftAjax>
            <css commentMode="None" />
            <js preserveImportantComments="false" />
        </microsoftAjax>
        …
    </bundleTransformer>
    …
</configuration>

答案 1 :(得分:0)

这是yuicompressor版本2.4.8,但问题仍然存在。

如果您使用的是Linux,则可以先通过 sed 命令将文件中的/*!替换为/*,然后再通过yuicompressor运行它。

我刚刚测试过的现实工作示例:

sed -i -e "s/\/\*\!/\/\*/g" script.js

\/-转义符号/

\*-转义符号*

\!-转义符号!

g-全局(正则表达式标志)

s-替代(正则表达式标志)

-i-“在位”命令标志,表示即时替换(将更改应用于同一文件)

下一步:像往常一样运行yuicompressor,瞧瞧!

java -jar /path/to/yuicompressor-2.4.8.jar script.js -o script.min.js --charset utf-8
相关问题