asp.net mvc - Javascript bundle obfuscation and Bundletransformer -
i trying obfuscate output of our javascript bundles on fly , came across bundletransformer (a bundling extension uses yui compressor), had no success implementing it! far reading provided documentation i've written following code in 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 following code in 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 bundle output can infer not it's not obfuscated compression not taking effect @ all! not find samples online , tried changing few settings here , there no luck! i'm pretty clueless , solutions or suggestions appreciated. in advance!
bahador,
actually, need simple code:
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); } } }
and settings in web.config
file:
<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>
rather, problem caused fact, web application run in debug mode.
switch web application release mode using following settings in web.config
file:
<configuration> … <system.web> <compilation debug="false" … /> … </system.web> … </configuration>
or add app_start\bundleconfig.cs
file following code:
bundletable.enableoptimizations = true;
before read documentation bundle transformer, recommend read chapter “bundling , minification” of asp.net mvc 4 tutorial.
Comments
Post a Comment