-->

How to remove comments with BundleTransformer YuiJ

2019-06-21 21:39发布

问题:

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.

回答1:

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>