EnableDefaultBundles
method and the DynamicFolderBundle
class are no longer recommended). There were no major changes in the final version of B / M, but one very useful feature was added - this is CDN support.Global.asax
file to the BundleConfig
class and place this class in the App_Start
directory. In the Global.asax
file, you only need to leave the RegisterBundles
method of the BundleConfig
class: using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; namespace BundleTransformer.Example.Mvc { public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); // bundle` BundleConfig.RegisterBundles(BundleTable.Bundles); } } }
BundleConfig
class: using System.Web.Optimization; using BundleTransformer.Core.Orderers; using BundleTransformer.Core.Transformers; namespace BundleTransformer.Example.Mvc { public class BundleConfig { public static void RegisterBundles(BundleCollection bundles) { var cssTransformer = new CssTransformer(); var jsTransformer = new JsTransformer(); var nullOrderer = new NullOrderer(); var commonStylesBundle = new Bundle("~/Bundles/CommonStyles"); commonStylesBundle.Include( "~/Content/Site.css", "~/Content/BundleTransformer.css", "~/AlternativeContent/css/TestCssComponentsPaths.css", "~/Content/themes/base/jquery.ui.core.css", "~/Content/themes/base/jquery.ui.theme.css", "~/Content/themes/base/jquery.ui.resizable.css", "~/Content/themes/base/jquery.ui.button.css", "~/Content/themes/base/jquery.ui.dialog.css", "~/Content/TestTranslators.css", "~/Content/TestLess.less", "~/Content/TestSass.sass", "~/Content/TestScss.scss"); commonStylesBundle.Transforms.Add(cssTransformer); commonStylesBundle.Orderer = nullOrderer; bundles.Add(commonStylesBundle); var modernizrBundle = new Bundle("~/Bundles/Modernizr"); modernizrBundle.Include("~/Scripts/modernizr-2.*"); modernizrBundle.Transforms.Add(jsTransformer); modernizrBundle.Orderer = nullOrderer; bundles.Add(modernizrBundle); var commonScriptsBundle = new Bundle("~/Bundles/CommonScripts"); commonScriptsBundle.Include("~/Scripts/MicrosoftAjax.js", "~/Scripts/jquery-{version}.js", "~/Scripts/jquery-ui-{version}.js", "~/Scripts/jquery.validate.js", "~/Scripts/jquery.validate.unobtrusive.js", "~/Scripts/jquery.unobtrusive-ajax.js", "~/Scripts/knockout-2.*", "~/Scripts/TestCoffeeScript.coffee"); commonScriptsBundle.Transforms.Add(jsTransformer); commonScriptsBundle.Orderer = nullOrderer; bundles.Add(commonScriptsBundle); var jqueryUiStylesDirectoryBundle = new Bundle("~/Bundles/JqueryUiStylesDirectory"); jqueryUiStylesDirectoryBundle.IncludeDirectory("~/Content/themes/base/", "*.css"); jqueryUiStylesDirectoryBundle.Transforms.Add(new CssTransformer( new[] { "*.all.css", "jquery.ui.base.css" })); bundles.Add(jqueryUiStylesDirectoryBundle); var scriptsDirectoryBundle = new Bundle("~/Bundles/ScriptsDirectory"); scriptsDirectoryBundle.IncludeDirectory("~/Scripts/", "*.js"); scriptsDirectoryBundle.Transforms.Add(new JsTransformer( new[] { "*.all.js", "references.js" })); bundles.Add(scriptsDirectoryBundle); } } }
Include
and IncludeDirectory
methods are now used to add files and directories (instead of the AddFile
and AddDirectory
). In addition, adding transformations is now not done through the Bundle
class constructor, but through its Transforms
property (in the code above, you can see this in the example of adding instances of the CssTransformer
and JsTransformer
).It is worth noting that, in addition to theBundle
class, for creating bundles, its subclassesStyleBundle
(for styles) andScriptBundle
(for scripts) can also be used. Transformations are already added to these classes that are responsible for minimizing the code (instances of theCssMinify
andJsMinify
). In our case, we cannot use these subclasses, because we need to add transformations from the Bundle Transformer (instances of theCssTransformer
andJsTransformer
) instead of instances of theCssMinify
andJsMinify
classes.
_Layout.cshtml
file as an _Layout.cshtml
: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>@ViewBag.Title - Bundle Transformer Example MVC Application</title> <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" /> <meta name="viewport" content="width=device-width" /> @Styles.Render("~/Bundles/CommonStyles") @Scripts.Render("~/Bundles/Modernizr") ... </head> <body> ... @Scripts.Render("~/Bundles/CommonScripts") @RenderSection("scripts", required: false) </body> </html>
Render
method ( Styles.Render
used for CSS, and Scripts.Render
for JavaScript). Using the Render
method not only makes adding links to bundles more convenient, but also solves the main problem with earlier versions of B / M - debug mode support.debug
attribute of the compilation
element from the Web.config
file ( true
- debug mode; false
- release mode);BundleTable.EnableOptimizations
property, which can be specified in the BundleConfig
class ( true
- release mode; false
- debug mode). Moreover, the value of the BundleTable.EnableOptimizations
property takes precedence over the settings from the Web.config
file. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Home Page - Bundle Transformer Example MVC Application</title> <link href="/favicon.ico" rel="shortcut icon" type="image/x-icon" /> <meta name="viewport" content="width=device-width" /> <link href="/Bundles/CommonStyles?v=IWN75r8IOIWnlehQ6JVPUgrb7UER075iobpzbjYTtRQ1" rel="stylesheet"/> <script src="/Bundles/Modernizr?v=1dm47T0PPFmcdy8ssp2EZ4h8yT2SjNhVvtdbc0MyDAs1"></script> ... </head> <body> ... <script src="/Bundles/CommonScripts?v=qWsyReB8UFAt-HPS-6MCkeDDTs2lQgYMdyCUd2V9O4o1"></script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Home Page - Bundle Transformer Example MVC Application</title> <link href="/favicon.ico" rel="shortcut icon" type="image/x-icon" /> <meta name="viewport" content="width=device-width" /> <link href="/Content/Site.css" rel="stylesheet"/> <link href="/Content/BundleTransformer.css" rel="stylesheet"/> <link href="/AlternativeContent/css/TestCssComponentsPaths.css" rel="stylesheet"/> <link href="/Content/themes/base/jquery.ui.core.css" rel="stylesheet"/> <link href="/Content/themes/base/jquery.ui.theme.css" rel="stylesheet"/> <link href="/Content/themes/base/jquery.ui.resizable.css" rel="stylesheet"/> <link href="/Content/themes/base/jquery.ui.button.css" rel="stylesheet"/> <link href="/Content/themes/base/jquery.ui.dialog.css" rel="stylesheet"/> <link href="/Content/TestTranslators.css" rel="stylesheet"/> <link href="/Content/TestLess.less" rel="stylesheet"/> <link href="/Content/TestSass.sass" rel="stylesheet"/> <link href="/Content/TestScss.scss" rel="stylesheet"/> <script src="/Scripts/modernizr-2.5.3.js"></script> ... </head> <body> ... <script src="/Scripts/MicrosoftAjax.debug.js"></script> <script src="/Scripts/jquery-1.8.1.js"></script> <script src="/Scripts/jquery-ui-1.8.23.js"></script> <script src="/Scripts/jquery.validate.js"></script> <script src="/Scripts/jquery.validate.unobtrusive.js"></script> <script src="/Scripts/jquery.unobtrusive-ajax.js"></script> <script src="/Scripts/knockout-2.1.0.debug.js"></script> <script src="/Scripts/TestCoffeeScript.coffee"></script> </body> </html>
.less
, .sass
, .scss
and .coffee
. If HTTP handlers are not registered in the Web.config
file for these extensions, then the contents of these files will be unprocessed and errors may occur on the client side.Web.config
file automatically: <?xml version="1.0" encoding="utf-8"?> <configuration> ... <system.web> ... <httpHandlers> <!-- Declaration of Bundle Transformer HTTP-handlers --> <add path="*.less" verb="GET" type="BundleTransformer.LessLite.HttpHandlers.LessAssetHandler, BundleTransformer.LessLite" /> <add path="*.sass" verb="GET" type="BundleTransformer.SassAndScss.HttpHandlers.SassAndScssAssetHandler, BundleTransformer.SassAndScss" /> <add path="*.scss" verb="GET" type="BundleTransformer.SassAndScss.HttpHandlers.SassAndScssAssetHandler, BundleTransformer.SassAndScss" /> <add path="*.coffee" verb="GET" type="BundleTransformer.CoffeeScript.HttpHandlers.CoffeeScriptAssetHandler, BundleTransformer.CoffeeScript" /> <!-- /Declaration of Bundle Transformer HTTP-handlers --> </httpHandlers> </system.web> <system.webServer> ... <handlers> ... <!-- Declaration of Bundle Transformer HTTP-handlers --> <add name="LessAssetHandler" path="*.less" verb="GET" type="BundleTransformer.LessLite.HttpHandlers.LessAssetHandler, BundleTransformer.LessLite" resourceType="File" preCondition="" /> <add name="SassAssetHandler" path="*.sass" verb="GET" type="BundleTransformer.SassAndScss.HttpHandlers.SassAndScssAssetHandler, BundleTransformer.SassAndScss" resourceType="File" preCondition="" /> <add name="ScssAssetHandler" path="*.scss" verb="GET" type="BundleTransformer.SassAndScss.HttpHandlers.SassAndScssAssetHandler, BundleTransformer.SassAndScss" resourceType="File" preCondition="" /> <add name="CoffeeScriptAssetHandler" path="*.coffee" verb="GET" type="BundleTransformer.CoffeeScript.HttpHandlers.CoffeeScriptAssetHandler, BundleTransformer.CoffeeScript" resourceType="File" preCondition="" /> <!-- /Declaration of Bundle Transformer HTTP-handlers --> </handlers> </system.webServer> ... </configuration>
.less
extension. But you always have the opportunity to manually replace it with the HTTP LessAssetHandler
from BundleTransformer.Less./configuration/bundleTransformer/core/assetHandler
from the Web.config
file. The following code shows the default HTTP handler settings: <configuration> ... <bundleTransformer xmlns="http://tempuri.org/BundleTransformer.Configuration.xsd"> <core> ... <assetHandler clientCacheDurationInDays="365" enableCompression="true" useLastModifiedHeader="true" useETagHeader="true" serverCacheDurationInMinutes="15" useServerCacheSlidingExpiration="false" disableClientCacheInDebugMode="true" disableCompressionInDebugMode="true" /> ... </core> </bundleTransformer> ... </configuration>
clientCacheDurationInDays="()"
. Sets the duration for finding the text content of the processed file in the browser cache (in days).enableCompression="(true|false)"
. Enables GZIP / Deflate compression of the processed file.useLastModifiedHeader="(true|false)"
. Allows the use of the Last-Modified
HTTP header to notify the browser of changes to the processed file.useETagHeader="(true|false)"
. Allows the use of the ETag
HTTP header to notify the browser about changes to the processed file.serverCacheDurationInMinutes="()"
. Sets the duration of the text content of the processed file in the server cache (in minutes).useServerCacheSlidingExpiration="(true|false)"
. Enables the use of rolling time obsolescence of the server cache item.disableClientCacheInDebugMode="(true|false)"
. Disables in debug mode, browser caching of the processed file.disableCompressionInDebugMode="(true|false)"
. Disables GZIP / Deflate compression of the processed file in debug mode.ClosureRemoteJsMinifier
and ClosureLocalJsMinifier
. ClosureRemoteJsMinifier
minimizes using the Google Closure Compiler Service API web service and requires a constant connection to the Internet. ClosureLocalJsMinifier
minimizes using the console application of Google Closure Compiler Application , and requires for its work: the Java virtual machine and the latest version of the compiler.jar file.CrockfordJsMinifier
. CrockfordJsMinifier
based on the C # port of the oldest JS code minimizer , JSMin , which was written by Douglas Crockford. The used C # port was created based on the May 22, 2007 version of JSMin.UglifyJsMinifier
. UglifyJsMinifier
created based on the minimizer of JS-code popular in the Node.js community - UglifyJS (version 1.3.3 is now supported).EdwardsJsMinifier
. EdwardsJsMinifier
is based on the JS minimizer Dean Edwards (Dean Edwards) Packer version 3.0. The main feature of Packer is that in addition to minimizing and obfuscating the code, it can also compress the code using the Base62 algorithm. Base62 compression can be used when GZIP / Deflate compression is not supported on the web server.KryzhanovskyCssMinifier
CSS minimizer adapter. KryzhanovskyCssMinifier
based on the CSS CSSO minimizer (version 1.2.18 is currently supported). This minimizer was created by the developers of the BEM methodology: Sergey Kryzhanovsky and Vitaly Kharisov. The main difference between this CSS minimizer and other means of minimizing CSS code is to support structural minimization (for example, with the CSS code, the following operations are performed: merging blocks with the same selectors, removing overlapped properties, partially selecting properties into a separate block, partially merging blocks and etc.). Structural minimization gives a high degree of compression while minimizing the code generated by the “machine” (for example, a preprocessor).msvcp100.dll
and msvcr100.dll
from Microsoft Visual C ++ 2010 are required. If your system does not have these assemblies, I recommend that you install Microsoft Visual C ++ 2010 Redistributable Package ( x86 , x64 ).WgCssMinifier
. WgCssMinifier
minimizes CSS code using a semantic CSS minimizer from the Microsoft-developed WebGrease library. Semantic CSS-minimizer WebGrease as well as CSSO produces structural minimization of CSS-code.Simple
mode), but do not yet have such a level of reliability .Web.config
file (in the case that all the optional modules are installed): <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!-- Declaration of Bundle Transformer configuration section group --> <sectionGroup name="bundleTransformer"> <section name="core" type="BundleTransformer.Core.Configuration.CoreSettings" /> <section name="less" type="BundleTransformer.LessLite.Configuration.LessLiteSettings" /> <section name="sassAndScss" type="BundleTransformer.SassAndScss.Configuration.SassAndScssSettings" /> <section name="microsoftAjax" type="BundleTransformer.MicrosoftAjax.Configuration.MicrosoftAjaxSettings" /> <section name="yui" type="BundleTransformer.Yui.Configuration.YuiSettings" /> <section name="closure" type="BundleTransformer.Closure.Configuration.ClosureSettings" /> <section name="uglify" type="BundleTransformer.UglifyJs.Configuration.UglifySettings" /> <section name="packer" type="BundleTransformer.Packer.Configuration.PackerSettings" /> <section name="csso" type="BundleTransformer.Csso.Configuration.CssoSettings" /> </sectionGroup> <!-- /Declaration of Bundle Transformer configuration section group --> ... </configSections> ... <!-- Bundle Transformer configuration settings --> <bundleTransformer xmlns="http://tempuri.org/BundleTransformer.Configuration.xsd"> <core enableTracing="false" jsFilesWithMicrosoftStyleExtensions="MicrosoftAjax.js,MicrosoftMvcAjax.js, MicrosoftMvcValidation.js,knockout-$version$.js" useEnableOptimizationsProperty="true"> <css defaultMinifier="NullMinifier"> <minifiers> <add name="NullMinifier" type="BundleTransformer.Core.Minifiers.NullMinifier, BundleTransformer.Core" /> <add name="MicrosoftAjaxCssMinifier" type="BundleTransformer.MicrosoftAjax.Minifiers.MicrosoftAjaxCssMinifier, BundleTransformer.MicrosoftAjax" /> <add name="YuiCssMinifier" type="BundleTransformer.Yui.Minifiers.YuiCssMinifier, BundleTransformer.Yui" /> <add name="KryzhanovskyCssMinifier" type="BundleTransformer.Csso.Minifiers.KryzhanovskyCssMinifier, BundleTransformer.Csso" /> <add name="WgCssMinifier" type="BundleTransformer.WG.Minifiers.WgCssMinifier, BundleTransformer.WG" /> </minifiers> <translators> <add name="NullTranslator" type="BundleTransformer.Core.Translators.NullTranslator, BundleTransformer.Core" enabled="false" /> <add name="LessTranslator" type="BundleTransformer.LessLite.Translators.LessTranslator, BundleTransformer.LessLite" enabled="true" /> <add name="SassAndScssTranslator" type="BundleTransformer.SassAndScss.Translators.SassAndScssTranslator, BundleTransformer.SassAndScss" enabled="true" /> </translators> </css> <js defaultMinifier="NullMinifier"> <minifiers> <add name="NullMinifier" type="BundleTransformer.Core.Minifiers.NullMinifier, BundleTransformer.Core" /> <add name="MicrosoftAjaxJsMinifier" type="BundleTransformer.MicrosoftAjax.Minifiers.MicrosoftAjaxJsMinifier, BundleTransformer.MicrosoftAjax" /> <add name="YuiJsMinifier" type="BundleTransformer.Yui.Minifiers.YuiJsMinifier, BundleTransformer.Yui" /> <add name="ClosureRemoteJsMinifier" type="BundleTransformer.Closure.Minifiers.ClosureRemoteJsMinifier, BundleTransformer.Closure" /> <add name="ClosureLocalJsMinifier" type="BundleTransformer.Closure.Minifiers.ClosureLocalJsMinifier, BundleTransformer.Closure" /> <add name="CrockfordJsMinifier" type="BundleTransformer.JsMin.Minifiers.CrockfordJsMinifier, BundleTransformer.JsMin" /> <add name="UglifyJsMinifier" type="BundleTransformer.UglifyJs.Minifiers.UglifyJsMinifier, BundleTransformer.UglifyJs" /> <add name="EdwardsJsMinifier" type="BundleTransformer.Packer.Minifiers.EdwardsJsMinifier, BundleTransformer.Packer" /> </minifiers> <translators> <add name="NullTranslator" type="BundleTransformer.Core.Translators.NullTranslator, BundleTransformer.Core" enabled="false" /> <add name="CoffeeScriptTranslator" type="BundleTransformer.CoffeeScript.Translators.CoffeeScriptTranslator, BundleTransformer.CoffeeScript" enabled="true" /> </translators> </js> <assetHandler clientCacheDurationInDays="365" enableCompression="true" useLastModifiedHeader="true" useETagHeader="true" serverCacheDurationInMinutes="15" useServerCacheSlidingExpiration="false" disableClientCacheInDebugMode="true" disableCompressionInDebugMode="true" /> </core> <less useNativeMinification="false" severity="0" /> <sassAndScss useNativeMinification="false" /> <microsoftAjax> <css allowEmbeddedAspNetBlocks="false" colorNames="Strict" commentMode="Important" ignoreErrorList="" indentSize="4" minifyExpressions="true" outputMode="SingleLine" blocksStartOnSameLine="NewLine" preprocessorDefineList="" termSemicolons="false" severity="0" /> <js allowEmbeddedAspNetBlocks="false" collapseToLiteral="true" debugLookupList="Debug,$Debug,WAssert,Msn.Debug,Web.Debug" evalTreatment="Ignore" ignoreConditionalCompilation="false" ignoreErrorList="" indentSize="4" inlineSafeStrings="true" knownGlobalNamesList="" localRenaming="CrunchAll" macSafariQuirks="true" minifyCode="true" noAutoRenameList="$super" outputMode="SingleLine" blocksStartOnSameLine="NewLine" preprocessorDefineList="" preserveFunctionNames="false" preserveImportantComments="true" removeFunctionExpressionNames="true" removeUnneededCode="true" renamePairs="" strictMode="false" stripDebugStatements="true" termSemicolons="false" severity="0"/> </microsoftAjax> <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> <closure> <js> <remote closureCompilerServiceApiUrl="http://closure-compiler.appspot.com/compile" compilationLevel="Simple" prettyPrint="false" excludeDefaultExterns="false" severity="0" /> <local javaVirtualMachinePath="" closureCompilerApplicationPath="" compilationLevel="Simple" prettyPrint="false" languageSpec="EcmaScript3" thirdParty="true" processJqueryPrimitives="false" processClosurePrimitives="false" severity="0" /> </js> </closure> <uglify> <js> <parser strictSemicolons="false" /> <mangler mangle="true" topLevel="false" defines="" except="" noFunctions="false" /> <squeezer makeSequences="true" deadCode="true" unsafe="false" /> <codeGenerator beautify="false" indentStart="0" indentLevel="4" quoteKeys="false" spaceColon="false" asciiOnly="false" /> </js> </uglify> <packer> <js shrinkVariables="true" base62Encode="false" /> </packer> <csso> <css disableRestructuring="false" /> </csso> </bundleTransformer> <!-- /Bundle Transformer configuration settings --> ... </configuration>
bundleTransformer
, so this time I will confine myself to listing the new sections:closure
- settings of the BundleTransformer.Closure moduleuglify
- settings of the BundleTransformer.UglifyJs modulepacker
- settings of the BundleTransformer.Packer modulecsso
- settings of the BundleTransformer.Csso modulebundleTransformer
IntelliSense support is implemented for the configuration section :Source: https://habr.com/ru/post/152051/
All Articles