<?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: Pushpak K</title>
    <description>The latest articles on DEV Community by Pushpak K (@pkdev).</description>
    <link>https://dev.to/pkdev</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%2F370754%2Fb8b30c25-030a-4806-b42e-9d21858033de.png</url>
      <title>DEV Community: Pushpak K</title>
      <link>https://dev.to/pkdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pkdev"/>
    <language>en</language>
    <item>
      <title>Create angular apps with shared library</title>
      <dc:creator>Pushpak K</dc:creator>
      <pubDate>Tue, 30 Mar 2021 06:06:40 +0000</pubDate>
      <link>https://dev.to/pkdev/create-angular-apps-with-shared-library-2dl6</link>
      <guid>https://dev.to/pkdev/create-angular-apps-with-shared-library-2dl6</guid>
      <description>&lt;p&gt;Once, a wise Front-end developer was developing applications, one for his customers and the other was for their customers. He decided to go with Angular Framework which provides great support for Multiple applications with a shared library so he can reuse the same components. &lt;/p&gt;

&lt;p&gt;Let's see how he saved his time by creating reusable components with one shared library and 2 applications. &lt;/p&gt;

&lt;h2&gt;
  
  
  Create Angular workspace
&lt;/h2&gt;



&lt;p&gt;&lt;code&gt;ng new my-library-workspace --create-application=false&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This command creates an angular workspace without creating any app along with some config files.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aWIwyNYv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/laadni9fevobnyggzd4n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aWIwyNYv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/laadni9fevobnyggzd4n.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Generate angular library
&lt;/h2&gt;



&lt;p&gt;&lt;code&gt;ng generate library my-library&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Remember to run upcoming commands from your workspace folder.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This command creates an angular library and makes an entry in &lt;strong&gt;angular.json&lt;/strong&gt; which is generated previously. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;All the applications and library created will be placed under the projects folder in the workspace. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Updated &lt;strong&gt;angular.json&lt;/strong&gt; looks like this &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lgzqQJKa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jqtcrtflurl0zjnqfi80.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lgzqQJKa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jqtcrtflurl0zjnqfi80.png" alt="image"&gt;&lt;/a&gt;&lt;br&gt;
It creates entry for library under projects key. Important keys are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"projectType" is library&lt;/li&gt;
&lt;li&gt;"root" is path to library folder &lt;/li&gt;
&lt;li&gt;"sourceRoot" is src folder path in your library &lt;/li&gt;
&lt;li&gt;"architect" property holds an configuration object  for build, test and lint.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q9NQwUbA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lzanr4d1jljo7qr445ze.png" alt="image"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All this is for the library in the project.&lt;br&gt;
Other important commands&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ng build my-library --prod // build production version of the library 
ng build my-library --watch // watch for any changes in library and build after changes are saved 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even you can publish your library on npm. Check out Angular's official document &lt;a href="https://angular.io/guide/creating-libraries#publishing-your-library"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Few things to consider to make library components reusable. Check out &lt;a href="https://angular.io/guide/creating-libraries#refactoring-parts-of-an-app-into-a-library"&gt;here&lt;/a&gt; for more. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Another important file from library is &lt;strong&gt;/src/public-api.ts&lt;/strong&gt;. Every component, service, the module you create in the library should be exported from this file. When angular imports any library component, it internally searches in this file. &lt;/p&gt;

&lt;h2&gt;
  
  
  Generate application
&lt;/h2&gt;



&lt;p&gt;&lt;code&gt;ng generate application my-application&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This command creates an application within your workspace that can access your library components. It asks you series of questions before creating an app like do you want routing in the app, which CSS preprocessor you would prefer.&lt;br&gt;
After creating an application, angular CLI makes an entry of the application inside &lt;strong&gt;angular.json&lt;/strong&gt; similar to the library. There are key differences in the application configuration. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aOQHTkCP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t5lh097aemw5rbheucjm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aOQHTkCP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t5lh097aemw5rbheucjm.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"projectType" is application&lt;/li&gt;
&lt;li&gt;"architect" object has build, serve, extract-i18n, test, lint and e2e configurations
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GlJ089M2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfgz6m6hfzb1bum6sqp0.png" alt="image"&gt;
&lt;/li&gt;
&lt;li&gt;You can configure various parts for your build process. &lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  How to use the library in app?
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Just import your library in your application modules where you are using the library components.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;import {module_name} from my-library;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make an entry of imported module in application modules imports array. 
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--F4FMP1L8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lsxaijqg02gg8wmncy0f.png" alt="image"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the same way, you can create multiple applications which can use the same library and configure the separate build, serve processes individual to each application in &lt;strong&gt;angular.json&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Find more details on angular.io. &lt;br&gt;
Reference Links &lt;br&gt;
&lt;a href="https://angular.io/guide/libraries"&gt;https://angular.io/guide/libraries&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>library</category>
    </item>
  </channel>
</rss>
