How to remove comments with BundleTransformer YuiJsMinifier
Asked Answered
O

2

5

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.

Ostensorium answered 30/12, 2013 at 0:13 Comment(0)
S
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>
Stunt answered 11/1, 2014 at 10:1 Comment(0)
S
0

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
Sourdough answered 8/1, 2020 at 22:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.