<?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: RadW2020</title>
    <description>The latest articles on DEV Community by RadW2020 (@radw2020).</description>
    <link>https://dev.to/radw2020</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%2F199572%2Fe54bf7fd-8e02-4f71-aff4-3af9460c1699.jpeg</url>
      <title>DEV Community: RadW2020</title>
      <link>https://dev.to/radw2020</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/radw2020"/>
    <language>en</language>
    <item>
      <title>Components library with Angular</title>
      <dc:creator>RadW2020</dc:creator>
      <pubDate>Mon, 22 Jul 2019 11:17:09 +0000</pubDate>
      <link>https://dev.to/radw2020/components-library-with-angular-2c4a</link>
      <guid>https://dev.to/radw2020/components-library-with-angular-2c4a</guid>
      <description>&lt;p&gt;Let's say our project is growing and we want to keep things separated. Sometimes we can split our angular project in two or three different projects, but then, we realize it will be repeated code from styles, components or any other resource.&lt;/p&gt;

&lt;p&gt;As a first resume of what we want to do:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a couple of Angular projects that will use the same custom library&lt;/li&gt;
&lt;li&gt;Create the custom components library (with Angular)&lt;/li&gt;
&lt;li&gt;Host that custom library in our base projects like a GIT submodule&lt;/li&gt;
&lt;li&gt;Build and access the library from our base projects like an angular package&lt;/li&gt;
&lt;li&gt;Profit :)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can find nice posts with generic information that can help you to accomplish this task. Like this amazing &lt;a href="https://gist.github.com/gitaarik/8735255" rel="noopener noreferrer"&gt;summary&lt;/a&gt; about GIT submodules. Or the official documentation of &lt;a href="https://github.com/ng-packagr/ng-packagr" rel="noopener noreferrer"&gt;ng-packagr&lt;/a&gt; about transpile a library to Angular Package Format.&lt;/p&gt;

&lt;p&gt;But for this post, what a I want to do is a &lt;strong&gt;practical example with Angular&lt;/strong&gt; (I need to practice to feel like I really understand, I'm sure some of you too!).&lt;/p&gt;

&lt;h3&gt;
  
  
  Create the example projects
&lt;/h3&gt;

&lt;p&gt;Create three minimal Angular projects:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

ng new HelloFoo &lt;span class="nt"&gt;--minimal&lt;/span&gt;
ng new HelloBar &lt;span class="nt"&gt;--minimal&lt;/span&gt;
ng generate library ComponentsLibrary


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

&lt;/div&gt;

&lt;p&gt;After a few tweaks, &lt;strong&gt;to help us differentiate both Hello projects&lt;/strong&gt;, we could have something 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fplnqjzvk21awvzps6xlq.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fplnqjzvk21awvzps6xlq.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Prepare the custom library project
&lt;/h3&gt;

&lt;p&gt;Pay attention to the shape of our &lt;strong&gt;components library&lt;/strong&gt;. We have generated it as a library. Doing so, we don't need to delete non useful Angular configuration files, we only need to be able to export components/modules.&lt;/p&gt;

&lt;p&gt;For this occasion, lets create a footer module in our new components library project. &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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F141jgsy3mvljh3yrfrw2.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F141jgsy3mvljh3yrfrw2.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We want to use this resource in both Hello projects and we want to be sure that it's going to &lt;strong&gt;keep consistency while avoiding DRY&lt;/strong&gt;. (This is one of the main reasons of having a common submodule)&lt;/p&gt;

&lt;p&gt;Pay attention to &lt;code&gt;public-api.ts&lt;/code&gt; file. It describes the access points that we'll need to import in the Hello Projects later on.&lt;/p&gt;

&lt;p&gt;For the purposes of this tutorial, we can have these two projects in local, but we need to put the components library in a shared repository. &lt;br&gt;
We use Github in this case, so we push this new components library into his repository. &lt;a href="https://github.com/RadW2020/components_library_tutorial" rel="noopener noreferrer"&gt;https://github.com/RadW2020/components_library_tutorial&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Add the submodule to the projects
&lt;/h3&gt;

&lt;p&gt;At this point, we want our two base projects stay aware of this new library. In order to do that we execute this command in the root folder of our Hello project:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

git submodule add git@github.com:RadW2020/components_library_tutorial.git custom_lib


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

&lt;/div&gt;

&lt;p&gt;Execute this in our two Hello projects. This will add it as a submodule in the given folder. Please notice the new file GIT has created for us named &lt;code&gt;.gitmodules&lt;/code&gt;&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fynea1137sesz68e4j825.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fynea1137sesz68e4j825.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;From now on, this shared library, that is a project on itself, is tied in our Hello projects as a submodule hosted in his own folder &lt;em&gt;custom_lib&lt;/em&gt;. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Maintain the submodule updated
&lt;/h3&gt;

&lt;p&gt;What happens if a change is pushed to the components library repository? We need to be aware and check it to keep our library updated.&lt;br&gt;
This will be noticeable any time we use &lt;code&gt;git status&lt;/code&gt; in our Hello projects:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdrp7srvg43kr1wdsl5wa.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdrp7srvg43kr1wdsl5wa.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;GIT is warning us about commits in lib's origin. We want to tell to our Hello project to point to the last commit of the components library repository. For that, we do:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

git add .
git commit -m "update custom_lib to last reference" 


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

&lt;/div&gt;

&lt;p&gt;This will update the submodule from the point of view of our Hello project. What we really are doing here is maintaining our Hello project custom_lib folder. This is done by holding a reference that points to a commit of the custom_lib GIT history, if it is not the last, the message &lt;strong&gt;(new commits)&lt;/strong&gt; shows up.&lt;/p&gt;

&lt;h3&gt;
  
  
  Components library evolution
&lt;/h3&gt;

&lt;p&gt;To finnish this mutation, we have to prepare our library to behave like that. A new configuration file has to be created, so &lt;strong&gt;ng-packagr&lt;/strong&gt; will know how to handle it:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhpx42f9jncxwpg3sfugf.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhpx42f9jncxwpg3sfugf.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;ng-package.json&lt;/code&gt; it is specified where will be located the build (dest), and how it will be accesible (entryFile).&lt;/p&gt;

&lt;h3&gt;
  
  
  Next steps
&lt;/h3&gt;

&lt;p&gt;Let's &lt;strong&gt;tell to our Hello projects&lt;/strong&gt; that this &lt;em&gt;custom_lib&lt;/em&gt; is more than just a sub-folder synced with GIT. We have to specify a few things:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;How to compile &lt;em&gt;custom_lib&lt;/em&gt; so it can be used like a regular &lt;strong&gt;dependency&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Where it's located the library dependency. (We will leave it in dist folder next to the other installed dependencies)&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;compiled&lt;/strong&gt; library needs to be listed in package.json.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Dev Dependencies
&lt;/h4&gt;

&lt;p&gt;In &lt;code&gt;devDependencies&lt;/code&gt;, it is declared the ng-packagr and other dependencies needed to transpile the library to an Angular package format.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3x90xwesd4rl4f4mvhf0.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3x90xwesd4rl4f4mvhf0.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Time to execute &lt;code&gt;npm install&lt;/code&gt; to get those new dependencies.&lt;/p&gt;

&lt;h4&gt;
  
  
  Build lib
&lt;/h4&gt;

&lt;p&gt;In &lt;code&gt;scripts&lt;/code&gt;, it is declared how to build the library.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyugcoyn4hvy4975kdu0y.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyugcoyn4hvy4975kdu0y.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will build the project that we have to define in &lt;code&gt;angular.json&lt;/code&gt;&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg4zr8r3z1xalr4bwcxy3.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg4zr8r3z1xalr4bwcxy3.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then execute &lt;code&gt;npm run build-lib&lt;/code&gt;. The console will show useful information.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5n9jixeqzhiuw5gbgpjo.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5n9jixeqzhiuw5gbgpjo.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;dependencies&lt;/code&gt;, must be declared where to find the build.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fof81axl7huakil0e3tuh.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fof81axl7huakil0e3tuh.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The last step is to execute &lt;code&gt;npm install&lt;/code&gt;. With this final step we're installing the package as a regular dependency. In fact, you can find it inside &lt;em&gt;node_modules&lt;/em&gt; folder.&lt;/p&gt;

&lt;h3&gt;
  
  
  Back to work
&lt;/h3&gt;

&lt;p&gt;It's time to come back to our regular Angular workflow. This means, to import the FooterModule &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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwq4x2ockamxdkum682qv.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwq4x2ockamxdkum682qv.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and then use Footer component in the template.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy4patkae7wdfjec753nc.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy4patkae7wdfjec753nc.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Both Hello projects should look 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkbl7fn7uylv0gxagji46.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkbl7fn7uylv0gxagji46.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;yai!&lt;br&gt;
We got it :D&lt;/p&gt;

&lt;h3&gt;
  
  
  Test it
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Custom lib: Edit footer background. &lt;/li&gt;
&lt;li&gt;Custom lib: Commit and push changes.&lt;/li&gt;
&lt;li&gt;Hello projects: &lt;code&gt;git pull --recurse-submodules&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Hello projects: Serve and enjoy.&lt;/li&gt;
&lt;/ol&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzkkqry2h9q6k38rvqsfb.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzkkqry2h9q6k38rvqsfb.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Salute!!&lt;/p&gt;

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