<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mahmood Abbas</title>
    <description>The latest articles on DEV Community by Mahmood Abbas (@m9hmood).</description>
    <link>https://dev.to/m9hmood</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F187371%2F50810af2-24d7-4b74-939c-c3cbfe83638d.jpeg</url>
      <title>DEV Community: Mahmood Abbas</title>
      <link>https://dev.to/m9hmood</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/m9hmood"/>
    <language>en</language>
    <item>
      <title>Reduce Vue 2 application size</title>
      <dc:creator>Mahmood Abbas</dc:creator>
      <pubDate>Fri, 03 Jul 2020 17:08:59 +0000</pubDate>
      <link>https://dev.to/m9hmood/reduce-vue-js-application-size-kfl</link>
      <guid>https://dev.to/m9hmood/reduce-vue-js-application-size-kfl</guid>
      <description>&lt;p&gt;before a few days, one of my clients has an application built with vue.js, so the application was working well, and we can say everything was ok, but there was a big issue for my client &lt;strong&gt;The Size Of The application&lt;/strong&gt; because we have a slow internet speed (15kb/s - 50kb/s), and the size of the application was more than &lt;strong&gt;1.2mb&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;so he wanted me to reduce the application size, and in this article, I will talk about what I did to reduce the size.&lt;/p&gt;

&lt;h2&gt;
  
  
  1 - generate a report about the application bundle
&lt;/h2&gt;

&lt;p&gt;to understand what makes bundle size bigger than expected&lt;br&gt;
I needed to generate a report about the application to provide a visual guide to the size of each package in the bundle, so I can check if there is any package that has a big size and I can replace it.&lt;/p&gt;

&lt;p&gt;fortunately, &lt;code&gt;Vue-Cli&lt;/code&gt; offers a lot of commands that can help us in this situation, one of them is &lt;code&gt;build --report&lt;/code&gt; command, this command will build a report for the application using &lt;strong&gt;Webpack-bundle-analyzer&lt;/strong&gt;&lt;br&gt;
and to use it, open &lt;code&gt;package.json&lt;/code&gt; and in the scripts add&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"build-report": "vue-cli-service build --report"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then run &lt;code&gt;npm run build-report&lt;/code&gt; 🙆‍♂️&lt;/p&gt;

&lt;p&gt;after process end, a &lt;code&gt;report.html&lt;/code&gt; file will be created inside the &lt;code&gt;dist&lt;/code&gt; folder, open the file in the browser and you will see something like this: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2Fym2X4mn%2FCapture.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2Fym2X4mn%2FCapture.png" alt="webpack-bundle-anylazer"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Note:&lt;/strong&gt; from the &lt;code&gt;Webpack-bundle-analyzer&lt;/code&gt; menu we can see the size of the application, my application size is &lt;strong&gt;stat size: 5.17mb, parsed: 1.57mb, gzipped: 500kb&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2 - reduce the bundle size
&lt;/h2&gt;

&lt;p&gt;from the report, we can see every package in the bundle and how much size it's added to the bundle and we can see that the packages with big size in the application are: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;echarts (stat size: 2.7mb, parsed: 814kb, gzipped: 270.7kb)&lt;/li&gt;
&lt;li&gt;bootstrap-vue (stat size: 772kb, parsed: 297kb, gzipped: 69kb)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Reduce the size of echarts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;we can see that &lt;code&gt;echarts&lt;/code&gt; has a big size 😫&lt;/p&gt;

&lt;p&gt;now to reduce the size of the bundle we should:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;use another package with a smaller size&lt;/li&gt;
&lt;li&gt;reduce the package size with &lt;a href="https://webpack.js.org/guides/tree-shaking/" rel="noopener noreferrer"&gt;tree-shaking&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;now in my situation, I should use another package because I only need a pie &amp;amp; line chart and it's better to me to find a smaller package 😅, so the best alternative to me is &lt;a href="https://gionkunz.github.io/chartist-js/" rel="noopener noreferrer"&gt;Chartist.js&lt;/a&gt; 😍&lt;br&gt;
after removing &lt;code&gt;echarts&lt;/code&gt; and using &lt;code&gt;Chartist.js&lt;/code&gt; let's generate another report and see the result!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FtLzZTXg%2Freport.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FtLzZTXg%2Freport.png" alt="second-report"&gt;&lt;/a&gt;&lt;br&gt;
the application size now is &lt;strong&gt;stat size: 2.35mb, parsed: 819kb, gzipped: 240kb&lt;/strong&gt;&lt;br&gt;
it's a big change for now 🎉&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reduce the size of Bootstrap-Vue&lt;/strong&gt;&lt;br&gt;
I know bootstrap-vue it's not too big, but we can remove unused components to get better performance and size.&lt;br&gt;
if we go to the &lt;a href="https://bootstrap-vue.js.org/docs/#tree-shaking-with-module-bundlers" rel="noopener noreferrer"&gt;bootstrap-vue docs&lt;/a&gt; we can see it supports tree-shaking 🎉&lt;br&gt;
but only applies to the JavaScript code and not CSS/SCSS so in this method we will remove unused js code only 😥&lt;/p&gt;

&lt;p&gt;now, what I need to do is check what is the components that I used in the application, and Import it like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FkSTn0sn%2Fbootstrap-Plugin.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FkSTn0sn%2Fbootstrap-Plugin.png" alt="bootstrap-plugin"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;now let's see the result 🙄:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FTHq3vRR%2Fres2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FTHq3vRR%2Fres2.png" alt="third-report"&gt;&lt;/a&gt;&lt;br&gt;
the application size now is &lt;strong&gt;stat size: 2mb, parsed: 700kb, gzipped: 200kb&lt;/strong&gt;&lt;br&gt;
it's not a big change but hey, we removed unused code and make the bundle size smaller (40kb smaller than before) 😅&lt;/p&gt;

&lt;h2&gt;
  
  
  3 - Remove unused CSS
&lt;/h2&gt;

&lt;p&gt;what I did before is only reduce the Javascript bundle size, that means remove unused Javascript code form the bundle, and now I want to remove unused CSS from the application 😉&lt;/p&gt;

&lt;p&gt;maybe you are saying &lt;code&gt;this step is unnecessary, CSS will never be a problem for my application 🤷‍♂️&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;but no, if you care about your application performance, you shouldn't say that because reducing file sizes is good for performance 🐱‍🏍.&lt;/p&gt;

&lt;p&gt;you can use tools like Purge-CSS to make this step easier, but you should know that Purge-CSS will remove used CSS in sometimes too 🙆‍♂️&lt;/p&gt;

&lt;p&gt;so because I'm using bootstrap and Unfortunately, Purge-CSS doesn't play well with bootstrap-vue I can't use Purge-CSS  🤕&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What Should I Do?&lt;/strong&gt;&lt;br&gt;
you can use &lt;strong&gt;SCSS&lt;/strong&gt; version of bootstrap and only import the styles that you need 😊&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FvXZSGF2%2Fused-bootstrap.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FvXZSGF2%2Fused-bootstrap.png" alt="used-styles"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Note:&lt;/strong&gt; you can see here I only imported the styles that I use in the application&lt;/p&gt;

&lt;p&gt;so, what I remove was: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FWpg51K1%2Funused-bootstrap.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FWpg51K1%2Funused-bootstrap.png" alt="unused-styles"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;a lot of unused styles has been remove and this a big change for the application performance 🎉&lt;/p&gt;

&lt;p&gt;for more details read &lt;a href="https://getbootstrap.com/docs/4.0/getting-started/theming/" rel="noopener noreferrer"&gt;Theming Bootstrap&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4 - compress images to improve loading time
&lt;/h2&gt;

&lt;p&gt;now, only what we need to do in this step is compressing the images that we are using to improve the loading time&lt;br&gt;
so in my case, I have only one image in the login page and the size of it is: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FzSCCMpZ%2Fimage-size.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FzSCCMpZ%2Fimage-size.png" alt="login-image-size"&gt;&lt;/a&gt;&lt;br&gt;
now, this size is so big, right? &lt;br&gt;
I used an online tool to compress the image and its called &lt;a href="https://compress-or-die.com/" rel="noopener noreferrer"&gt;Compress-Or-Die&lt;/a&gt; so let's see the result&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FFqhWP5y%2Fl-image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FFqhWP5y%2Fl-image.png" alt="compressed-img"&gt;&lt;/a&gt;&lt;br&gt;
that was a big change in the size of the image 😁&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;in the beginning, the application size was bigger than &lt;strong&gt;1mb&lt;/strong&gt; and this size includes the images and fonts, my goal was to reduce the bundle size and after I reduce it the final size was: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FByD77CY%2FCapture.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FByD77CY%2FCapture.png" alt="final-size"&gt;&lt;/a&gt;&lt;br&gt;
the final size is 50% less than the original application, so let's do a little party with the client 😂🎉&lt;/p&gt;

&lt;p&gt;finally, I hope you find this article helpful&lt;br&gt;
and if you have any suggestions I will appreciate it so much.&lt;br&gt;
Thank you very much for reading.&lt;/p&gt;

</description>
      <category>vue</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
