 {"id":449,"date":"2021-01-17T15:36:08","date_gmt":"2021-01-17T23:36:08","guid":{"rendered":"https:\/\/www.mavice.com\/blog\/?p=449"},"modified":"2021-02-17T15:45:45","modified_gmt":"2021-02-17T23:45:45","slug":"cherry-picking-your-vue-js-application","status":"publish","type":"post","link":"https:\/\/www.mavice.com\/blog\/cherry-picking-your-vue-js-application\/","title":{"rendered":"Cherry Picking Your Vue JS Application"},"content":{"rendered":"\n<p>Vue JS is a versatile and performant web framework that allows developers to build small or sophisticated web applications. Paired with the Vue CLI, Vuex, and Vue router, Vue is more than capable of handling a production-level environment.<\/p>\n\n\n\n<p>Out of the box, Vue presents some pretty impressive numbers, but of course, as you reach a certain point in development the speed of your application may decrease and you may notice things seem a bit slower than expected. Cherry-picking and optimizing imports is a great way to decrease bundle size and speed up page loads.<\/p>\n\n\n\n<h2>Cherry Picking<\/h2>\n\n\n\n<p>When using a module bundler you can optionally import only specific components and directives. Pulling in a specific component or set of components only when needed can greatly decrease the size of your bundle.<\/p>\n\n\n\n<p>Let&#8217;s use <a href=\"https:\/\/bootstrap-vue.js.org\/\" data-type=\"URL\" data-id=\"https:\/\/bootstrap-vue.js.org\/\">Bootstrap-Vue<\/a> as an example.<\/p>\n\n\n\n<p>Registering and using Bootstrap-Vue is easy and allows for easy prototyping. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/main.js\n\nimport Vue from 'vue'\nimport { BootstrapVue, IconsPlugin } from 'bootstrap-vue'\n\n\/\/ Import Bootstrap an BootstrapVue CSS files (order is important)\nimport 'bootstrap\/dist\/css\/bootstrap.css'\nimport 'bootstrap-vue\/dist\/bootstrap-vue.css'\n\n\/\/ Make BootstrapVue available throughout your project\nVue.use(BootstrapVue)\n\/\/ Optionally install the BootstrapVue icon components plugin\nVue.use(IconsPlugin)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Nav.vue\n\n&lt;template&gt;\n&lt;div&gt;\n\/\/Bootstrap-Vue Nav Component\n  &lt;b-nav&gt;\n    &lt;b-nav-item active&gt;Active&lt;\/b-nav-item&gt;\n    &lt;b-nav-item&gt;Link&lt;\/b-nav-item&gt;\n    &lt;b-nav-item&gt;Another Link&lt;\/b-nav-item&gt;\n    &lt;b-nav-item disabled&gt;Disabled&lt;\/b-nav-item&gt;\n  &lt;\/b-nav&gt;\n&lt;\/div&gt;\n&lt;\/template&gt;\n\nexport default {\n   name: 'Nav'\n}<\/code><\/pre>\n\n\n\n<p>Quick and easy, right? Let&#8217;s check on our bundle size after including Bootstrap-Vue in our project.<br><img loading=\"lazy\" width=\"1946\" height=\"1218\" class=\"wp-image-450\" style=\"width: 900px;\" src=\"https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2021\/01\/Screen-Shot-2021-01-17-at-5.39.17-PM.png\" alt=\"Overall bundle size of the Vue Application after installing Bootstrap-Vue\" srcset=\"https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2021\/01\/Screen-Shot-2021-01-17-at-5.39.17-PM.png 1946w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2021\/01\/Screen-Shot-2021-01-17-at-5.39.17-PM-300x188.png 300w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2021\/01\/Screen-Shot-2021-01-17-at-5.39.17-PM-1024x641.png 1024w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2021\/01\/Screen-Shot-2021-01-17-at-5.39.17-PM-768x481.png 768w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2021\/01\/Screen-Shot-2021-01-17-at-5.39.17-PM-1536x961.png 1536w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2021\/01\/Screen-Shot-2021-01-17-at-5.39.17-PM-360x225.png 360w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2021\/01\/Screen-Shot-2021-01-17-at-5.39.17-PM-1040x651.png 1040w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2021\/01\/Screen-Shot-2021-01-17-at-5.39.17-PM-640x401.png 640w\" sizes=\"(max-width: 1946px) 100vw, 1946px\" \/><\/p>\n\n\n\n<p>157.98 KB gzipped after registering Bootstrap-Vue (101.87 KB gzipped). This is a pretty heavy addition when compared with our initial bundle size. Let&#8217;s see if we can bring this number down by cherry-picking the components we actually need, instead of importing all components from the package.<\/p>\n\n\n\n<p>When using a module bundler you can optionally import only specific components and directives. Pulling in a specific component or set of components only when needed can greatly decrease the size of your bundle. <\/p>\n\n\n\n<p>We will cherry-pick the &#8220;nav&#8221; component from the previous example.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/main.js\n\nimport Vue from 'vue'\nimport { NavPlugin } from 'bootstrap-vue'\n\n\/\/ Import Bootstrap an BootstrapVue CSS files (order is important)\nimport 'bootstrap\/dist\/css\/bootstrap.css'\nimport 'bootstrap-vue\/dist\/bootstrap-vue.css'\n\n\/\/ Make BootstrapVue available throughout your project\nVue.use(NavPlugin)\n\/\/ Optionally install the BootstrapVue icon components plugin\nVue.use(IconsPlugin)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Nav.vue\n\n&lt;template&gt;\n&lt;div&gt;\n\/\/Bootstrap-Vue Nav Component\n  &lt;b-nav&gt;\n    &lt;b-nav-item active&gt;Active&lt;\/b-nav-item&gt;\n    &lt;b-nav-item&gt;Link&lt;\/b-nav-item&gt;\n    &lt;b-nav-item&gt;Another Link&lt;\/b-nav-item&gt;\n    &lt;b-nav-item disabled&gt;Disabled&lt;\/b-nav-item&gt;\n  &lt;\/b-nav&gt;\n&lt;\/div&gt;\n&lt;\/template&gt;\n\nimport { BNav, BNavItem } from 'bootstrap-vue';\n\nexport default {\n   name: 'Nav',\n   components: {\n      BNav,\n      BNavItem\n   }\n}<\/code><\/pre>\n\n\n\n<p>Let&#8217;s check on our bundle size after cherry-picking.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"622\" src=\"https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2021\/01\/Screen-Shot-2021-01-17-at-6.20.06-PM-1024x622.png\" alt=\"Bundle size after cherry-picking.\" class=\"wp-image-452\" srcset=\"https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2021\/01\/Screen-Shot-2021-01-17-at-6.20.06-PM-1024x622.png 1024w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2021\/01\/Screen-Shot-2021-01-17-at-6.20.06-PM-300x182.png 300w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2021\/01\/Screen-Shot-2021-01-17-at-6.20.06-PM-768x466.png 768w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2021\/01\/Screen-Shot-2021-01-17-at-6.20.06-PM-1536x932.png 1536w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2021\/01\/Screen-Shot-2021-01-17-at-6.20.06-PM-2048x1243.png 2048w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2021\/01\/Screen-Shot-2021-01-17-at-6.20.06-PM-360x219.png 360w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2021\/01\/Screen-Shot-2021-01-17-at-6.20.06-PM-1040x631.png 1040w, https:\/\/www.mavice.com\/blog\/wp-content\/uploads\/2021\/01\/Screen-Shot-2021-01-17-at-6.20.06-PM-640x389.png 640w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>&gt;50% decrease in our bundle size after cherry-picking. In a real-world application we would use more components, but even still, importing only what you need makes a huge difference.  Utilizing modern-day best web practices such as cherry-picking, tree-shaking, and code splitting is just a couple of ways that could optimize your application and ultimately create a better experience for your users.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vue JS is a versatile and performant web framework that allows developers to build small or sophisticated web applications. Paired with the Vue CLI, Vuex, and Vue router, Vue is more than capable of handling a production-level environment. Out of the box, Vue presents some pretty impressive numbers, but of&#8230;<\/p>\n","protected":false},"author":2,"featured_media":465,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[36],"tags":[37,9],"_links":{"self":[{"href":"https:\/\/www.mavice.com\/blog\/wp-json\/wp\/v2\/posts\/449"}],"collection":[{"href":"https:\/\/www.mavice.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mavice.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mavice.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mavice.com\/blog\/wp-json\/wp\/v2\/comments?post=449"}],"version-history":[{"count":3,"href":"https:\/\/www.mavice.com\/blog\/wp-json\/wp\/v2\/posts\/449\/revisions"}],"predecessor-version":[{"id":455,"href":"https:\/\/www.mavice.com\/blog\/wp-json\/wp\/v2\/posts\/449\/revisions\/455"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mavice.com\/blog\/wp-json\/wp\/v2\/media\/465"}],"wp:attachment":[{"href":"https:\/\/www.mavice.com\/blog\/wp-json\/wp\/v2\/media?parent=449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mavice.com\/blog\/wp-json\/wp\/v2\/categories?post=449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mavice.com\/blog\/wp-json\/wp\/v2\/tags?post=449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}