[英]How to remove comments with BundleTransformer YuiJsMinifier
我正在使用BundleTransformer來縮小css
和js
資源
<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 js 壓縮器默認會刪除注釋。 是的,它是一種刪除,但它仍然是這樣的評論:
/* 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來完全刪除捆綁的縮小輸出文件中的所有類型的注釋嗎? 谷歌頁面速度強烈建議我這樣做。
YUI 壓縮器不支持刪除重要注釋。
我建議你安裝BundleTransformer.MicrosoftAjax包。 此后將MicrosoftAjaxCssMinifier
和MicrosoftAjaxJsMinifier
注冊為默認縮小器,並將以下配置設置添加到 Web.config 文件中:
<configuration>
…
<bundleTransformer xmlns="http://tempuri.org/BundleTransformer.Configuration.xsd">
…
<microsoftAjax>
<css commentMode="None" />
<js preserveImportantComments="false" />
</microsoftAjax>
…
</bundleTransformer>
…
</configuration>
它是 yuicompressor 版本 2.4.8,但問題仍然存在。
如果你在 linux 上,你可以使用sed命令來替換/*!
在通過 yuicompressor 運行文件之前,在文件中加上/*
。
我剛剛測試過的真實工作示例:
sed -i -e "s/\/\*\!/\/\*/g" script.js
\\/
- 轉義符號/
\\*
- 轉義符號*
\\!
- 逃脫的符號!
g
- 全局(正則表達式標志)
s
- 替代(正則表達式標志)
-i
- "inplace" 命令標志,表示即時替換(在同一文件上應用更改)
下一步:像往常一樣運行 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.