{"id":1449,"date":"2017-09-23T21:55:25","date_gmt":"2017-09-23T21:55:25","guid":{"rendered":"http:\/\/inchoo.net\/?p=29344"},"modified":"2017-09-23T23:12:28","modified_gmt":"2017-09-23T23:12:28","slug":"javascript-bundling-in-magento-2","status":"publish","type":"post","link":"https:\/\/www.sushilkumar.ind.in\/blog\/magento-2\/javascript-bundling-in-magento-2\/","title":{"rendered":"Javascript Bundling in Magento 2"},"content":{"rendered":"<p><strong>Javascript bundling<\/strong> is a technique that groups separate files in order to reduce the number of HTTP requests that are required to load a page. Bundling is commonly used in today\u2019s \u201cmodule-based\u201d development where some functionalities are basically split into Modules (roughly explained). For loading modules, we usually use some of popular module loaders such as <strong>rollup.js<\/strong> or <strong>RequireJS<\/strong> (which is Magento\u2019s weapon of choice).<span id=\"more-29344\"><\/span><\/p>\n<p><strong>HTTP2<\/strong> is here so this technique will be probably deprecated in the future. You could still use bundling for reducing the number of requests for a specific page but I don\u2019t think it will be worth of it.<br \/>\nRight before we start with \u201creverse\u201d optimisation, we can use bundling to help us organise our assets and serve less files to the client which will (should) result in faster website.<\/p>\n<p>One of our services is <strong>Technical audit<\/strong> which covers various tests on both Frontend and Backend part of the client\u2019s store. Since i\u2019m Frontend Developer, performance is my key point during analysis.<\/p>\n<p>First thing I check is wether Javascript and CSS files are merged\/minified\/bundled (hello bundle). When I was Junior Developer, I was so happy\/angry when I discovered someone didn\u2019t turn these ON since I was thinking that technical level of Developers who worked on site was low and we can do a lot to improve site\u2026 and of course, I could get a \u201cquick win\u201d with turning merging\/minifying ON.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-29356 size-full\" src=\"\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2017\/09\/javascript-bundling-in-magento-2.png\" alt=\"\" width=\"361\" height=\"320\" \/><\/p>\n<p>Well, in theory that was great. Usually I was right about their expertise level but I was wrong about one thing \u2013 they didn\u2019t \u201cforgot\u201d to turn these ON.<\/p>\n<p>Explaining to the client that you would probably have to refactor 80% of the code to change that \u201csimple option\u201d is something I wouldn\u2019t want to go through again.<\/p>\n<p>We were doing Technical audit of the Magento 2 store and when I found out that Javascript and CSS files were not merged\/minified\/bundled I was in shock and first question that was going through my head was \u201cshall I report this to the client?!\u201d.<\/p>\n<p>Strange question, I know. But it does make sense because if I report it, I\u2019ll have to fix it.<\/p>\n<p>Just kidding, of course this was included in our report and was first thing I changed when we took over the project.<br \/>\nHow did it go? Well, do you write articles about stuff that work as expected?<\/p>\n<h2>Magento 2 Bundling<\/h2>\n<p><em>I must note that official docs do not give us a lot of information and there are only few discussions online about it.<\/em><br \/>\n<em> Therefore, I may be wrong in some of my conclusions so feel free to correct me in comments \ud83d\ude42<\/em><\/p>\n<p>For start, let\u2019s see 2 proper ways to load assets in Magento 2.<br \/>\nIn this example, you can see how assets are loaded on two different Magento pages \u2013 Homepage and product page.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-29357\" src=\"\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2017\/09\/javascript-bundling-in-magento-2-1.png\" alt=\"\" width=\"800\" height=\"500\" \/><\/p>\n<p>As you can see on example above, requireJS loaded different assets through different pages while all bundles were loaded regardless of wether they are needed or not.<br \/>\nWith RequireJS, we can load specific JS modules on specific pages which means you will load only necessarily assets and reduce number of requests.<\/p>\n<p><em>(If you wish to know more about how to load Javascript files with RequireJS, you can read this excelent article from my coleague Domagoj Potkoc. )<\/em><\/p>\n<p>While RequireJS did help us with reducing number of requests through the page, we still have few JS files being loaded and \u201cfew\u201d in Magento 2 means 40-50. We need a way to merge those assets into just few files. We need to create 5 files out of 50.<br \/>\nFiles are being loaded asynchronously but still, if we could merge these files into just few, we could improve performance even more.<br \/>\nBundling comes as a problem solver since it is used to merge modules and it dependencies into single file.<br \/>\nBundling denies main benefit of using module loaders such as RequireJS since assets aren\u2019t loaded asynchronously. Bundles are included with &lt;script&gt; tag, inside &lt;head&gt; section of the page.<\/p>\n<p><strong>So, why use it?<\/strong><\/p>\n<p>With bundling we could decide on where to load specific bundle and that\u2019s the best part of it! For example, we want to put all checkout-related stuff into one bundle and load it only on checkout page!<\/p>\n<h3>Feeling happy?<\/h3>\n<p>Well, Magento folks didn\u2019t implement <strong>RequireJS optimizer<\/strong> which is required for ordering and organising module load across different pages. You can exclude some scripts from bundle but you CAN\u2019T decide on bundle content for a specific page.<\/p>\n<p><strong>So, why use it?<\/strong><\/p>\n<p>With Merging, you can only merge Javascript files that are NOT being loaded through RequireJS. If you wish to \u201cmerge\u201d JS modules, you will have to use bundling.<\/p>\n<p>You probably have so many questions in your head right now. So do I. And I\u2019m still searching for the answers.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-29358\" src=\"\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2017\/09\/javascript-bundling-in-magento-2.jpg\" alt=\"\" width=\"648\" height=\"348\" \/><\/p>\n<p><em>Here is a random Zen image, just to chill you down.<\/em><\/p>\n<p>Bundling misses key features (imho) but you can still change few things in order to organize your bundles.<\/p>\n<h2>Bundle size and exclude list<\/h2>\n<p>In <strong><em>Vendor\/Theme\/etc\/view.xml <\/em><\/strong><em>y<\/em>ou can change bundle size and exclude some scripts from bundle.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-29349\" src=\"\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2017\/09\/javascript-bundling-in-magento-2-2.png\" alt=\"\" width=\"1332\" height=\"930\" \/><\/p>\n<p>Default size for bundle is 1MB.<br \/>\nBundle size determines number of bundles that will be created. For example, if you have 4MB of script files and bundle size is set to 1MB, you will have 4 bundles created.<\/p>\n<p>If number is too low, you will probably have 10 and more small bundles which will block each other during rendering so be careful with this.<br \/>\nRemember that bundles are not loaded asynchronously.<\/p>\n<p>We can also exclude certain scripts from bundles. They will be loaded with RequireJS when needed.<br \/>\nKeep in mind that <strong>Luma<\/strong> and <strong>Blank<\/strong> themes have their own exclude lists and if you are not properly fallbacking and don\u2019t have your own exclude list, bundles will be huge since all JS modules will be bundled, regardless of whether you need them or not.<\/p>\n<p><strong>&lt;exclude&gt; <\/strong>handle takes care of files that should be excluded from bundle. As far Magento 2 is concerned \u2013 since we can\u2019t decide on bundle content for each page, at least we can exclude assets that will not be required through the whole site, therefore bundles will consist only files that are required everywhere. As you can see in above example (Luma theme exclude list), jQuery assets are probably required everywhere so i don\u2019t understand idea behind excluding these from bundles. Probably Magento folks wanted to leave most important stuff under RequireJS control.<\/p>\n<h2>Activating bundling<\/h2>\n<p>After we have configured our bundle size and exclude list, it is time that we turn bundling on and enjoy great performance impact.<\/p>\n<p>We can turn bundling on here:\u00a0<strong><em>Stores &gt; configuration &gt; advanced &gt;developer<\/em><\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"29350\" data-permalink=\"https:\/\/www.sushilkumar.ind.in\/blog\/wordpress-tf\/stride-v1-4-backbone-quickload-grid-magazine-theme\/attachment\/stride-v1\/\" data-orig-file=\"https:\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2017\/11\/stride-v1-4-backbone-quickload-grid-magazine-theme.png\" data-orig-size=\"590,300\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Stride v1\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2017\/11\/stride-v1-4-backbone-quickload-grid-magazine-theme-300x153.png\" data-large-file=\"https:\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2017\/11\/stride-v1-4-backbone-quickload-grid-magazine-theme.png\" tabindex=\"0\" role=\"button\" class=\"aligncenter size-full wp-image-29350\" src=\"\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2017\/09\/javascript-bundling-in-magento-2-3.png\" alt=\"\" width=\"1351\" height=\"517\" \/><\/p>\n<p>After bundling is ON, clear cache and switch to production mode (bundling will not work in \u201cDeveloper\u201d mode). Static files will be deployed and you will see your bundles being loaded on the Frontend.<\/p>\n<h2>What about performance?<\/h2>\n<p>We did a lot in order to reduce number of requests through the site. But, there is one small problem with performance.<\/p>\n<p>This is Homepage of the <strong>Luma<\/strong> theme.<br \/>\nTesting was done on Magento 2.2.0 \u201cdev\u201d version, with following setting in Dev console (Chrome):<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-29359\" src=\"\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2017\/09\/javascript-bundling-in-magento-2-4.png\" alt=\"\" width=\"674\" height=\"71\" \/><\/p>\n<p><strong>Before turning bundling ON:<\/strong><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-29360 alignleft\" src=\"\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2017\/09\/javascript-bundling-in-magento-2-5.png\" alt=\"\" width=\"1411\" height=\"490\" \/><\/p>\n<ul>\n<li>Number of JS requests : 137<\/li>\n<li>Size: 2.0MB<\/li>\n<li>Loadtime: 9.46sec<\/li>\n<\/ul>\n<p><strong>With bundling turned ON:<\/strong><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-29361 alignleft\" src=\"\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2017\/09\/javascript-bundling-in-magento-2-6.png\" alt=\"\" width=\"1410\" height=\"491\" \/><\/p>\n<ul>\n<li>Number of JS requests : 8<\/li>\n<li>Size: 4.2MB<\/li>\n<li>Loadtime: 20.12sec<\/li>\n<\/ul>\n<p>Take a look at size and load time?<br \/>\nWe did reduce the number of JavaScript files being loaded, but the total filesize of generated bundles is larger than the total filesize of all non-bundled JavaScript files on the frontend.<br \/>\nReason? With RequireJS you load only needed JS files on a specific page. Bundling merges all JS \u00a0assets and serves them on all pages.<\/p>\n<h3>Conclusion<\/h3>\n<p>I must say I am disappointed with bundling, especially with the fact that we don\u2019t have RequireJS optimizer by default. Without it, whole idea behind bundling is missed.<\/p>\n<p><strong>Pros:<\/strong><\/p>\n<ul>\n<li>Bundles multiple files into single file<\/li>\n<\/ul>\n<p><strong>Cons:<\/strong><\/p>\n<ul>\n<li>negates benefits of using module loader<\/li>\n<li>filesize of bundles is larger than all non-bundled javascript files size in total (per page)<\/li>\n<li>you can\u2019t define bundle content precisely<\/li>\n<li>you can\u2019t decide in which pages which bundle will be loaded<\/li>\n<\/ul>\n<p>I don\u2019t see any reason we should use bundling right now.<br \/>\nBundling will make sense in the future if Magento folks create additional functionalities for us to use.<br \/>\nWe need configuration file where we will be able to decide on :<\/p>\n<ul>\n<li>number of bundles<\/li>\n<li>bundle size<\/li>\n<li>bundle content<\/li>\n<li>which pages specific bundle will be loaded<\/li>\n<\/ul>\n<p>Thanks for reading and i hope we can roll up some discussion about this topic in comments \ud83d\ude42<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"\/\/www.sushilkumar.ind.in\/custome-devlopment\/\">Do you need any custome Development Click here and fill the form with full your requirement<\/a> and after i will send the quotes of your work requirements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Javascript bundling is a technique that groups separate files in order to reduce the number of HTTP requests that are required to load a page. Bundling is commonly used in today\u2019s \u201cmodule-based\u201d development where some functionalities are basically split into Modules (roughly explained). For loading modules, we usually use some of popular module loaders such&#8230;<br \/>\nThe post Javascript Bundling in Magento 2 appeared first on Inchoo. Do you need any custome Development Click here and fill the form with full your requirement  and after i will send the quotes of your work requirements&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":1450,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[347,348,349,225,323,20,226,350],"tags":[351,352,353,230,331,22,231,354],"class_list":["post-1449","post","type-post","status-publish","format-standard","has-post-thumbnail","","category-bundle","category-bundle-magento-2","category-bundling","category-frontend","category-javascript","category-magento-2","category-magento2","category-requirejs","tag-bundle","tag-bundle-magento-2","tag-bundling","tag-frontend","tag-javascript","tag-magento-2","tag-magento2","tag-requirejs"],"jetpack_publicize_connections":[],"acf":[],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2017\/09\/javascript-bundling-in-magento-2.png","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p99pkJ-nn","_links":{"self":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/1449"}],"collection":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/comments?post=1449"}],"version-history":[{"count":0,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/1449\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/media\/1450"}],"wp:attachment":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/media?parent=1449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/categories?post=1449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/tags?post=1449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}