简体   繁体   English

如何使用 BundleTransformer YuiJsMinifier 删除评论

[英]How to remove comments with BundleTransformer YuiJsMinifier

I am using BundleTransformer to minify css and js resources我正在使用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>

As you can see for css it is possible to specify removeComments="true" But in js there is no such option.正如您在css中看到的那样,可以指定removeComments="true"但在js中没有这样的选项。

I red that YUI js compressor removes comments by default .我认为 YUI js 压缩器默认会删除注释 Yes it is kind of removes, but it is still leave comments like that:是的,它是一种删除,但它仍然是这样的评论:

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

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

Looks like there is no way to force YIU js minifier to remove comments.看起来没有办法强制 YIU js minifier 删除评论。

https://github.com/yui/yuicompressor : https://github.com/yui/yuicompressor

C-style comments starting with /*!以 /*! 开头的 C 风格注释are preserved.被保留下来。 This is useful with comments containing copyright/license information这对于包含版权/许可信息的注释很有用

Is there anything I can do by using BundleTransformer to completely remove all kind of comments in bundled minified output files?我可以通过使用BundleTransformer来完全删除捆绑的缩小输出文件中的所有类型的注释吗? Google page speed strongly recommended me to do that.谷歌页面速度强烈建议我这样做。

YUI compressor does not support removing of important comments. YUI 压缩器不支持删除重要注释。

I recommend you to install BundleTransformer.MicrosoftAjax package.我建议你安装BundleTransformer.MicrosoftAjax包。 Thereafter register MicrosoftAjaxCssMinifier and MicrosoftAjaxJsMinifier as default minifiers, and add to the Web.config file the following configuration settings:此后将MicrosoftAjaxCssMinifierMicrosoftAjaxJsMinifier注册为默认缩小器,并将以下配置设置添加到 Web.config 文件中:

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

It's yuicompressor version 2.4.8, but the issue is still there.它是 yuicompressor 版本 2.4.8,但问题仍然存在。

If you are on linux, you can use sed command to replace /*!如果你在 linux 上,你可以使用sed命令来替换/*! with /* in the file before running it through yuicompressor.在通过 yuicompressor 运行文件之前,在文件中加上/*

Real-life working example that I've just tested:我刚刚测试过的真实工作示例:

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

\\/ - escaped symbol / \\/ - 转义符号/

\\* - escaped symbol * \\* - 转义符号*

\\! - escaped symbol ! - 逃脱的符号!

g - global (regular expression flag) g - 全局(正则表达式标志)

s - substitute (regular expression flag) s - 替代(正则表达式标志)

-i - "inplace" command flag which means replacing on the fly (applying changes on the same file) -i - "inplace" 命令标志,表示即时替换(在同一文件上应用更改)

Next step: just run the yuicompressor as usual and voilà!下一步:像往常一样运行 yuicompressor,瞧!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM