{"id":2228,"date":"2019-03-02T23:22:35","date_gmt":"2019-03-02T23:22:35","guid":{"rendered":"http:\/\/salientsoft.co.uk\/?p=2228"},"modified":"2019-03-02T23:38:21","modified_gmt":"2019-03-02T23:38:21","slug":"compiling-a-custom-primeng-theme-deployed-as-a-static-asset","status":"publish","type":"post","link":"https:\/\/salientsoft.co.uk\/?p=2228","title":{"rendered":"Compiling a custom PrimeNG theme deployed as a static asset"},"content":{"rendered":"<p>I am\u00a0 using multiple themes and theme selection, using custom themes<\/p>\n<p>For interest I am loading the theme css as a static asset, in index.html:-<\/p>\n<blockquote><p>&lt;head&gt;<br \/>\n&lt;meta charset=&#8221;utf-8&#8243;&gt;<br \/>\n&lt;title&gt;Microapp Fabric Prototype&lt;\/title&gt;<br \/>\n&lt;base href=&#8221;\/&#8221;&gt;<br \/>\n&lt;meta name=&#8221;viewport&#8221; content=&#8221;width=device-width, initial-scale=1&#8243;&gt;<br \/>\n&lt;link id=&#8221;theme-css&#8221; rel=&#8221;stylesheet&#8221; type=&#8221;text\/css&#8221; href=&#8221;assets\/resources\/themes\/salient\/theme.css&#8221;&gt;<br \/>\n&lt;\/head&gt;<\/p><\/blockquote>\n<p>Then I am dynamically changing the href to switch themes:-<\/p>\n<blockquote><p>export class ThemeSwitcher {<br \/>\n\/*<br \/>\n* Select the target theme<br \/>\n* this may be an external theme or an internal packaged one<br \/>\n* If the path contains a &#8216;\/&#8217; the theme is assumed to be an external theme source<br \/>\n*\/<br \/>\nchangeTheme(theme: string) {<br \/>\nconst themeLink: HTMLLinkElement = &lt;HTMLLinkElement&gt;document.getElementById(&#8216;theme-css&#8217;);<br \/>\nthemeLink.href = theme.indexOf(&#8216;\/&#8217;) &gt;= 0 ? theme : &#8216;assets\/resources\/themes\/&#8217; + theme + &#8216;\/theme.css&#8217;;<br \/>\n}<br \/>\n}<\/p><\/blockquote>\n<p>A theme SCSS under the assets folder will be automatically compiled and included inline in the package only when a specific theme is referred to in the \u201cstyle\u201d section of angular.json. When dynamically switching multiple themes as above, the themes are kept as static assets and are not inlined in the package and so are removed from the \u201cstyle\u201d section. When this is done, the theme scss files for all the themes are not compiled to css automatically when an <em>ng build<\/em> or <em>ng serve<\/em> is done. This post <a href=\"https:\/\/medium.com\/@OlegVaraksin\/simple-ways-to-create-a-new-theme-in-primeng-12d9bbe3fc60\"><strong>here<\/strong><\/a> details how to manually install and build the scss using node-sass. It goes into other details which I haven\u2019t needed as I am using static assets, so only <em>node-sass<\/em> is needed. If in the actual theme directory, the following commands will install\u00a0 node-sass and compile the scss for the theme and also create the source map:-<\/p>\n<blockquote><p>npm install node-sass<\/p>\n<p>node-sass theme.scss theme.css \u2013\u2013source-map .<\/p><\/blockquote>\n<p>This can also be added to the scripts section of package.json to make an npm command:<\/p>\n<blockquote><p>&#8220;scripts&#8221;: {<br \/>\n&#8220;ng&#8221;: &#8220;ng&#8221;,<br \/>\n&#8220;start&#8221;: &#8220;ng serve&#8221;,<br \/>\n&#8220;build&#8221;: &#8220;ng build&#8221;,<br \/>\n&#8220;test&#8221;: &#8220;ng test&#8221;,<br \/>\n&#8220;lint&#8221;: &#8220;ng lint&#8221;,<br \/>\n&#8220;e2e&#8221;: &#8220;ng e2e&#8221;,<br \/>\n&#8220;scss&#8221;: &#8220;cd src\/assets\/resources\/themes\/legacy-blue &amp;&amp; node-sass theme.scss theme.css \u2013\u2013source-map .&#8221;<br \/>\n},<\/p><\/blockquote>\n<p>This can be run by simply using <em>npm run scss<\/em><\/p>\n<p>Note as above that npm supports the cd command in the scripts section, both for linux and windows (as detailed here). The default directory is the root of the project, and this is consistent even if you run the npm command e.g. from a subdirectory. I did not manage to find a way to automate this via an <em>ng build<\/em> or <em>ng serve<\/em> command, so these would have to be run in conjunction with <em>npm run scss<\/em> (with the latter running first).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am\u00a0 using multiple themes and theme selection, using custom themes For interest I am loading the theme css as a static asset, in index.html:- &lt;head&gt; &lt;meta charset=&#8221;utf-8&#8243;&gt; &lt;title&gt;Microapp Fabric Prototype&lt;\/title&gt; &lt;base href=&#8221;\/&#8221;&gt; &lt;meta name=&#8221;viewport&#8221; content=&#8221;width=device-width, initial-scale=1&#8243;&gt; &lt;link id=&#8221;theme-css&#8221; rel=&#8221;stylesheet&#8221; type=&#8221;text\/css&#8221; href=&#8221;assets\/resources\/themes\/salient\/theme.css&#8221;&gt; &lt;\/head&gt; Then I am dynamically changing the href to switch themes:- export class [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[198,209],"tags":[16,15],"_links":{"self":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2228"}],"collection":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2228"}],"version-history":[{"count":13,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2228\/revisions"}],"predecessor-version":[{"id":2242,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2228\/revisions\/2242"}],"wp:attachment":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}