我试图动态地混淆JavaScript包的输出并遇到bundletransformer(使用YUI Compressor的捆绑扩展),但没有成功实现它!到目前为止,通过阅读提供的documentation,我在RegisterBundles方法中编写了以下代码:
var nullBuilder = new NullBuilder();
var cssTransformer = new CssTransformer();
var yuiSettings = new BundleTransformer.Yui.Configuration.YuiSettings();
yuiSettings.JsMinifier.ObfuscateJavascript = true;
var jsTransformer = new JsTransformer();
var nullOrderer = new NullOrderer();
var scriptBundle = new CustomScriptBundle("~/jscbundle/").Include(
"~/Assets/Scripts/jquery.js",
"~/Assets/Scripts/jquery-ui.js");
scriptBundle.Builder = nullBuilder;
scriptBundle.Orderer = nullOrderer;
scriptBundle.Transforms.Add(jsTransformer);
bundles.Add(scriptBundle);
以及web.config中的以下代码:
<bundleTransformer xmlns="http://tempuri.org/BundleTransformer.Configuration.xsd">
<core>
<css>
<minifiers>
<add name="NullMinifier" type="BundleTransformer.Core.Minifiers.NullMinifier, BundleTransformer.Core" />
<add name="YuiCssMinifier" type="BundleTransformer.Yui.Minifiers.YuiCssMinifier, BundleTransformer.Yui" />
</minifiers>
<translators>
<add name="NullTranslator" type="BundleTransformer.Core.Translators.NullTranslator, BundleTransformer.Core" enabled="false" />
</translators>
</css>
<js defaultMinifier="YuiJsMinifier" usePreMinifiedFiles="true">
<minifiers>
<add name="NullMinifier" type="BundleTransformer.Core.Minifiers.NullMinifier, BundleTransformer.Core" />
<add name="YuiJsMinifier" type="BundleTransformer.Yui.Minifiers.YuiJsMinifier, BundleTransformer.Yui" />
</minifiers>
<translators>
<add name="NullTranslator" type="BundleTransformer.Core.Translators.NullTranslator, BundleTransformer.Core" enabled="false" />
</translators>
</js>
</core>
<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>
根据捆绑输出判断,我可以推断出它不仅没有被混淆,而且压缩根本没有生效!我在网上找不到任何样品,并尝试在这里和那里更改一些设置,没有运气!所以我很无能为力,任何解决方案或建议都会非常感激。提前谢谢!
答案 0 :(得分:3)
Bahador,
实际上,我们只需要非常简单的代码:
namespace TestYui
{
using System.Web.Optimization;
using BundleTransformer.Core.Bundles;
using BundleTransformer.Core.Orderers;
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
var nullOrderer = new NullOrderer();
var scriptBundle = new CustomScriptBundle("~/jscbundle/");
scriptBundle.Include(
"~/Assets/Scripts/jquery.js",
"~/Assets/Scripts/jquery-ui.js");
scriptBundle.Orderer = nullOrderer;
bundles.Add(scriptBundle);
}
}
}
Web.config
文件中的设置:
<configuration>
…
<bundleTransformer xmlns="http://tempuri.org/BundleTransformer.Configuration.xsd">
<core>
<css>
<minifiers>
<add name="NullMinifier" type="BundleTransformer.Core.Minifiers.NullMinifier, BundleTransformer.Core" />
<add name="YuiCssMinifier" type="BundleTransformer.Yui.Minifiers.YuiCssMinifier, BundleTransformer.Yui" />
</minifiers>
<translators>
<add name="NullTranslator" type="BundleTransformer.Core.Translators.NullTranslator, BundleTransformer.Core" enabled="false" />
</translators>
</css>
<js defaultMinifier="YuiJsMinifier">
<minifiers>
<add name="NullMinifier" type="BundleTransformer.Core.Minifiers.NullMinifier, BundleTransformer.Core" />
<add name="YuiJsMinifier" type="BundleTransformer.Yui.Minifiers.YuiJsMinifier, BundleTransformer.Yui" />
</minifiers>
<translators>
<add name="NullTranslator" type="BundleTransformer.Core.Translators.NullTranslator, BundleTransformer.Core" enabled="false" />
</translators>
</js>
</core>
</bundleTransformer>
…
</configuration>
相反,问题是由事实引起的,即Web应用程序以调试模式运行。
使用Web.config
文件中的以下设置将Web应用程序切换到发布模式:
<configuration>
…
<system.web>
<compilation debug="false" … />
…
</system.web>
…
</configuration>
或者在App_Start\BundleConfig.cs
文件中添加以下代码:
BundleTable.EnableOptimizations = true;
在您阅读Bundle Transformer的文档之前,我建议您阅读“Bundling and Minification的第ASP.NET MVC 4 tutorial章。