<?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: Luca</title>
    <description>The latest articles on DEV Community by Luca (@lucanardelli).</description>
    <link>https://dev.to/lucanardelli</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%2F315791%2F4cba1daf-8333-4146-b4bf-156c2eb70391.jpeg</url>
      <title>DEV Community: Luca</title>
      <link>https://dev.to/lucanardelli</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lucanardelli"/>
    <language>en</language>
    <item>
      <title>Angular `ng serve`: importing SCSS files in the global styles.scss vs including them in angular.json</title>
      <dc:creator>Luca</dc:creator>
      <pubDate>Sun, 19 Jan 2020 18:29:47 +0000</pubDate>
      <link>https://dev.to/lucanardelli/angular-ng-serve-importing-scss-files-in-the-global-styles-scss-vs-including-them-in-angular-json-1f5n</link>
      <guid>https://dev.to/lucanardelli/angular-ng-serve-importing-scss-files-in-the-global-styles-scss-vs-including-them-in-angular-json-1f5n</guid>
      <description>&lt;p&gt;With this article I'd like to share a tip I've discovered to quicken the SCSS rebuild time when using &lt;code&gt;ng serve&lt;/code&gt; and several 3rd party SCSS files. By rebuild time, I mean the time it takes for webpack to parse the modifications in the project's SCSS files and update the web app whenever there's a modification in said SCSS files.&lt;/p&gt;

&lt;p&gt;While working on Angular projects, whenever I had to include external SCSS files (e.g. Bootstrap, FontAwesome) I always &lt;code&gt;@import&lt;/code&gt;'ed them in my global &lt;code&gt;styles.scss&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Example from a recent project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="k"&gt;@import&lt;/span&gt; &lt;span class="s2"&gt;"global-styles/fontawesome.scss"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;@import&lt;/span&gt; &lt;span class="s2"&gt;"global-styles/bootstrap.scss"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;@import&lt;/span&gt; &lt;span class="s2"&gt;"global-styles/nebular.scss"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// This file calls nb-install() to init Nebular's theme system, after setting the relevant variables and overrides for my theme&lt;/span&gt;

&lt;span class="c1"&gt;// [My own SCSS rules below]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This worked perfectly, until I had to edit one of my global SCSS rules. Every modification triggered a rebuild of the whole styles.scss, and that took somewhere along 15 seconds every time.&lt;/p&gt;

&lt;p&gt;By selectively commenting out some of the imports, I saw that the biggest time-consumer was &lt;code&gt;nebular.scss&lt;/code&gt;, I guess because of their theme system. Without it, my rebuild time was ~3 seconds.&lt;/p&gt;

&lt;p&gt;I did not want to wait for 15+ seconds for every small SCSS modification, especially because 99% of my modifications did not have anything to do with the SCSS files I was importing! I couldn't find much on the internet apart from this StackOverflow question (&lt;a href="https://stackoverflow.com/questions/55309150/importing-styles-in-angular-json-vs-importing-in-styles-css"&gt;https://stackoverflow.com/questions/55309150/importing-styles-in-angular-json-vs-importing-in-styles-css&lt;/a&gt;), however this made me think that, since having multiple entries in the &lt;code&gt;styles&lt;/code&gt; section of &lt;code&gt;angular.json&lt;/code&gt; is basically the same as adding multiple independent CSS files to the app, maybe I could split my SCSS files in such a way that Webpack could be smart about it and only recompile some parts of them whenever a change was detected.&lt;/p&gt;

&lt;p&gt;In my case, the change was simple. Since the 3 files were independent one from the other (i.e. no file was referencing the other) I simply removed the 3 import lines and I moved them to my &lt;code&gt;angular.json&lt;/code&gt; styles section:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"styles": [
              "src/global-styles/fontawesome.scss",
              "src/global-styles/bootstrap.scss",
              "src/global-styles/nebular.scss",
              "src/styles.scss"
            ],
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;By doing this, whenever I had to change something, Webpack only rebuilt the relevant SCSS file where the change happened. If I had to change something in the &lt;code&gt;nebular.scss&lt;/code&gt; file I would have to wait ~13 seconds, but if the change was, for example, in the &lt;code&gt;styles.scss&lt;/code&gt; file, the rebuild time went down to &lt;strong&gt;800ms&lt;/strong&gt;! I did not observe any changes in the app, so I'd say that, CSS wise, the output should be the same as the &lt;code&gt;@import&lt;/code&gt; approach.&lt;/p&gt;

&lt;p&gt;Of course, this approach only works if we have several independent SCSS files, but this should be the case whenever we import external SCSS dependencies in our project. This could also be used in chase the project's SCSS files start to become heavy in terms of build time. Whenever the files can be isolated with their own dependencies, then they could be moved in &lt;code&gt;angular.json&lt;/code&gt; instead of being imported in the main &lt;code&gt;styles.scss&lt;/code&gt; file.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>webpack</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
