JAVASCRIPT包混淆和包变形器

我试图动态地混淆我们的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>

从包输出可以推断,它不仅没有被混淆,而且压缩根本没有生效!我在网上找不到任何样本,尝试在这里和那里更改一些设置,但没有成功!所以我很无知,任何解决方案或建议都将不胜感激。提前感谢!


解决方案

巴哈多,

实际上,我们只需要非常简单的代码:

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_StartBundleConfig.cs文件:

BundleTable.EnableOptimizations = true;

在阅读Bundle Transformer的文档之前,我建议您先阅读ASP.NET MVC 4 tutorial的"Bundling and Minification章。

相关文章