{"id":3106,"date":"2024-02-19T01:06:55","date_gmt":"2024-02-19T00:06:55","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=3106"},"modified":"2024-03-19T10:45:14","modified_gmt":"2024-03-19T09:45:14","slug":"angular-bundle-how-to-reduce-style-size-in-bundle","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2024\/02\/19\/angular-bundle-how-to-reduce-style-size-in-bundle\/","title":{"rendered":"Angular Bundle , How to reduce stylesheet size (in Bundle) ?"},"content":{"rendered":"\n<p>Building applications\/websites using&nbsp;<code>Angular<\/code>&nbsp;has a downside &#8211;&nbsp;<strong>the bundle size<\/strong>. This directly affects the loading speed and user experience of our projects. Reducing&nbsp;<strong>the bundle size<\/strong>&nbsp;is important.<\/p>\n\n\n\n<p>Let\u2019s learn how we can load stylesheets only when needed without making them part of the application bundle.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4>Case study<\/h4>\n\n\n\n<p>Let\u2019s assume that apart from the main&nbsp;<em>styles.css<\/em>&nbsp;file, we have 2 theme files present:<\/p>\n\n\n\n<ul><li>src\/styles\/themes\/theme-light.scss<\/li><li>src\/styles\/themes\/theme-dark.scss<\/li><\/ul>\n\n\n\n<p>Next, we would have them in&nbsp;<code>angular.json<\/code>&nbsp;&#8216;s styles option:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"styles\": &#91;\n    \"src\/styles.css\",\n    \"src\/styles\/themes\/theme-light.scss\",\n    \"src\/styles\/themes\/theme-dark.scss\"\n  ]<\/code><\/pre>\n\n\n\n<p id=\"a3f1\">And lastly, we would handle the loading of a particular theme based on the users\u2019 choice or their preferences.<\/p>\n\n\n\n<p id=\"3579\">Everything works great, but <strong>both of our theme files are part of our application bundle all the time<\/strong>, and this can increase the size of a bundle, which we want to avoid<\/p>\n\n\n\n<h4>Exclude Theme Files<\/h4>\n\n\n\n<p>Let\u2019s make a minor change in angular.json to exclude theme files from the bundle:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"styles\": &#91;\n    \"src\/styles.css\",\n    {\n      \"input\": \"src\/styles\/themes\/theme-light.scss\",\n      \"inject\": false,\n      \"bundleName\": \"theme-light\"\n    },\n    {\n      \"input\": \"src\/styles\/themes\/theme-dark.scss\",\n      \"inject\": false,\n      \"bundleName\": \"theme-dark\"\n    }\n  ]<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Two new options to learn here:<\/p>\n\n\n\n<ol><li><code><strong>inject<\/strong><\/code>: Setting this false will not include the file from &#8220;input&#8221; path in bundle<\/li><li><code><strong>bundleName<\/strong><\/code>: A separate bundle will be created containing the stylesheet from &#8220;input&#8221; path<\/li><\/ol>\n\n\n\n<p>Now if we try to build the project, it will create separate files and the output will look something like below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"682\" height=\"432\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/02\/image.png\" alt=\"\" class=\"wp-image-3107\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/02\/image.png 682w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/02\/image-300x190.png 300w\" sizes=\"(max-width: 682px) 100vw, 682px\" \/><\/figure>\n\n\n\n<p>After we build, we can locate our two files,<strong><em> theme-light<code>.css<\/code><\/em><\/strong>&nbsp;and<strong><em>&nbsp;<code>theme-dark.css<\/code><\/em><\/strong>, inside the&nbsp;<code>dist\/<\/code>&nbsp;folder.<\/p>\n\n\n\n<p>Notice that&nbsp;<strong><code>theme-light.css<\/code>&nbsp;<\/strong>and<strong>&nbsp;<code>theme-dark.css<\/code>&nbsp;<\/strong>are part of&nbsp;<strong>Lazy Chunk Files<\/strong>. Lazy chunk files speed up the application load time, because they are loaded on demand.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4>Lazy Load Theme Files<\/h4>\n\n\n\n<p>We managed to exclude them from the bundle and they are externally available. Now the question arises how to load these theme files?<\/p>\n\n\n\n<p>Within the&nbsp;<code>ngAfterContentInit()<\/code>&nbsp;method of&nbsp;<code>app.component.ts<\/code>,&nbsp;<strong>we define a new function<\/strong>&nbsp;named&nbsp;<code>appendStyle()<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ app.component.ts\nngAfterContentInit() {\n  \/\/...\n  function appendScript() {\n    \/\/Here we can append script as well\n  }\n  function appendStyle(name: string) {\n    let style = document.createElement(\"link\");\n    style.rel = \"stylesheet\";\n    style.type = \"text\/css\";\n    style.href = name;\n    document.head.appendChild(style);\n  }\n}\n<\/code><\/pre>\n\n\n\n<p>Now, we use&nbsp;<code>appendStyle()<\/code>&nbsp;function to&nbsp;<strong>lazy load<\/strong>&nbsp;<code>theme_styles.scss<\/code>&nbsp;by calling it within the&nbsp;<code>window.onload<\/code>&nbsp;event. For example, let&#8217;s say we want to load <strong><em>theme-dark.scss <\/em><\/strong>stylesheet:  ( we will use the <strong>.css<\/strong> extension, because the <strong>.scss <\/strong>was transpiled during the build process)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ app.component.ts\n  ngAfterContentInit() {\n    \/\/...\n    window.onload = () =&gt; {\n      \/\/...\n      appendStyle('theme-dark.css');\n    };\n  }\n<\/code><\/pre>\n\n\n\n<p>Complet code here: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ app.component.ts\nngAfterContentInit() {\n window.onload = () =&gt; {\n\n      appendStyle('theme-dark.css');\n    };\n\n  function appendStyle(name: string) {\n    let style = document.createElement(\"link\");\n    style.rel = \"stylesheet\";\n    style.type = \"text\/css\";\n    style.href = name;\n    document.head.appendChild(style);\n  }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>PS: <\/strong>The&nbsp;<code>window.onload<\/code>&nbsp;event is fired when the entire website loads.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Building applications\/websites using&nbsp;Angular&nbsp;has a downside &#8211;&nbsp;the bundle size. This directly affects the loading speed and user experience of our projects. Reducing&nbsp;the bundle size&nbsp;is important. Let\u2019s learn how we can load stylesheets only when needed without making them part of the application bundle. Case study Let\u2019s assume that apart from the main&nbsp;styles.css&nbsp;file, we have 2 theme [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3108,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[37],"tags":[798,667,797,127,794,795,796,792,793],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3106"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=3106"}],"version-history":[{"count":2,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3106\/revisions"}],"predecessor-version":[{"id":3146,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3106\/revisions\/3146"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/3108"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}