简体   繁体   中英

How to remove comments with BundleTransformer YuiJsMinifier

I am using BundleTransformer to minify css and js resources

        <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.

I red that YUI js compressor removes comments by default . 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.

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

C-style comments starting with /*!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? Google page speed strongly recommended me to do that.

YUI compressor does not support removing of important comments.

I recommend you to install BundleTransformer.MicrosoftAjax package. Thereafter register MicrosoftAjaxCssMinifier and MicrosoftAjaxJsMinifier as default minifiers, and add to the Web.config file the following configuration settings:

<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.

If you are on linux, you can use sed command to replace /*! with /* in the file before running it through 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)

s - substitute (regular expression flag)

-i - "inplace" command flag which means replacing on the fly (applying changes on the same file)

Next step: just run the yuicompressor as usual and voilà!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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