I was trying to obfuscate the output of our JavaScript bundles on the fly and came across the bundletransformer (a bundling extension that uses YUI Compressor), but had no success implementing it! So far by reading the provided documentation I've written the following code in the RegisterBundles method:
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);
and the following code in the 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>
Judging from the bundle output I can infer that not only it's not obfuscated but the compression is not taking effect at all! I could not find any samples online and tried changing a few settings here and there with no luck! So I'm pretty clueless and any solutions or suggestions would be much appreciated. Thanks in advance!
Bahador,
Actually, we only need very simple code:
And settings in the
Web.config
file:Rather, problem is caused by fact, that the web application run in debug mode.
Switch a web application to release mode by using the following settings in the
Web.config
file:Or add to the
App_Start\BundleConfig.cs
file the following code:Before you read the documentation for Bundle Transformer, i recommend to read chapter “Bundling and Minification” of the ASP.NET MVC 4 tutorial.