Skip to content

Angular

2 posts with the tag “Angular”

#100daysofcode Day 5: Shared styles in NX #Angular libraries

With another missed day in my challenge, I will work on packaging styles with a library and use them in our newly created Angular app.

It is easier to serve or build styles inside your app with the default configurations. Once you scaffold an application, the styles are available at {projectRoot}/src/styles.scss.

To test it out, let us serve our application.

$ nx serve linkinbio

Now, open http://localhost:4200/ and you can see your app component.

Screenshot-2023-03-14-at-16.53.57

If we make a small change to our styles, we can see that in real-time in our app.

styles.scss
/* You can add global styles to this file, and also import other style files */
p {
font-weight: bold;
}

Screenshot-2023-03-14-at-16.56.07

Great. So our styling works.

This is good for a self-contained application.

However, in the case of large applications, there will be a requirement for sharing styles across your libraries and including them as part of the main application build process.

Creating a shared UI library containing variables, mixins, and shared component-specific styles is helpful.

So we will learn how to include styles from an NX library into our application.

Solution – sharing styles across NX libraries

Section titled “Solution – sharing styles across NX libraries”

Let’s create some styles in our ui-shared library at libs/linkinbio/ui/shared/src/styles/linkinbio-ui-shared

libs/linkinbio/ui/shared/src/styles/linkinbio-ui-shared/_variables.scss
$color-red: red;
libs/linkinbio/ui/shared/src/styles/linkinbio-ui-shared/theme.scss
p {
font-weight: bold;
}

Now, let’s add the instructions to tell angular compiler to set libs/linkinbio/ui/shared/src/styles as one of the paths to look for for our new styles.

To do that, we will add the following stylePreprocessorOptions property to projects/linkinbio/project.json file.

{
...
"styles": ["projects/linkinbio/src/styles.scss"],
"stylePreprocessorOptions": {
"includePaths": ["libs/linkinbio/ui/shared/src/styles"]
},
"scripts": []
...
}

Now, you can easily import the styles into your app:

styles.scss
/* You can add global styles to this file, and also import other style files */
@use 'linkinbio-ui-shared/variables' as ui-shared-variables;
@import 'linkinbio-ui-shared/theme';
// @import '@rishabhmhjn/linkinbio/ui/shared/linkinbio-ui-shared';
p {
font-weight: bold;
color: ui-shared-variables.$color-red;
}

You can see your styles applied in the browser.

Screenshot-2023-03-14-at-17.28.17

It is important to note that I have added a distinct folder, linkinbio-ui-shared, inside the library styles folder. This is because, in the future, it is likely I will need to import styles from other libraries too.

To ensure the names of the shared styles do not collide with one another, I have chosen to namespace them by putting them in their own unique project name.

That’s it! Happy coding 🙂

#100daysofcode Day 4: Break in the challenge & Ng Scaffolding

I had to take a break! I was working from early in the morning and was finishing Statusbrew tasks until late at night before I could get back home to figure out what to write about the blog and then code/screenshot/document and write about it.

I was up until 2 am consistently for the whole week. I was drained a lot on Wednesday, thinking I would write the blog upon waking up, and, predictably, that didn’t happen.

I would only delay this process for a while, but it won’t be canceled. I want to write daily. I want to build up this habit and do this consistently. A cliché but giving up isn’t an option.

I have to mention that writing regularly is one of the things I want to do, me prioritizing Statusbrew tasks, which is for sure would be my priority, is another way to ensure I get done with the complicated Statusbrew tasks so that I can come back to writing as quickly as possible with a more bandwidth.

Another goal of mine is to return to Sales and Marketing by the end of this month. Looking at the current pace of tasks, it seems unlikely I would be done by the end of the month, but the deviation would probably be for about a week. This doesn’t mean that the Statusbrew tasks will entirely disappear. It just means that I would focus on mentoring & quality control in development while spending significantly more time on Sales.

Getting back to the challenge now. Day 4 it is.

Today, I want to set up an Angular application inside our nx monorepo.

My goal in the next few days is to learn the following concepts/frameworks and combine them to build the application:

  1. SSR – Server-Side Rendering ( Angular )
  2. PWA – Progressive Web Application ( Angular )
  3. NestJS
  4. Storybook (with Compodoc )
  5. Testing ( Angular & NestJS )
  6. Railway

While considering the type of application to cover the above, apart from the de-facto todos app, I want to build something that can align with what we can use at Statusbrew. There is one app idea I think may require integration across all the above – Link In Bio.

It has been asked by a few of our clients already. Quite simply, some brands like to share a page on their bio containing some of the most common links to their brand web pages, social profiles, news articles, and dynamic content, such as links to the latest blog posts, giveaways, etc.

There are some great apps already out there. e.g. https://lnk.bio/ is simple yet filled with compelling features like sync with the latest YouTube videos, and stats.

I plan to eventually have similar features more aligned with our clients’ preferences. Thinking about it in detail feels like a year-long project, but we would start with the basics here and add more features as we move along.

Today, we will bootstrap 2 angular entities, a library, and an app, and use the incremental build feature to build both.

We will start by installing nx angular dependency on our project. I initially tried using the console with the following commands but it seems the workflow to initialize the app afterward is not well laid out.

$ yarn add -D @nrwl/angular @angular-devkit/core @angular-devkit/schematics

I had to use the NX Console extension to add the dependency properly. You can find more information here.

Let’s just go ahead and add the nx angular depenency to our project.

Screenshot-2023-03-13-at-00.48.17

Screenshot-2023-03-13-at-00.48.38

Let it go through the process and modify the repository. You will see changes to package.json, nx.json, .gitignore, and .vscode/extenstions.json. You will also find some jest.* files added.

Here are the commands that it seems to have run in the process

$ yarn add -D -W @nrwl/angular
$ yarn nx g @nrwl/angular:init --interactive=false

We will make some changes to the nx.json file related to generator configs that would look like this

{
"generators": {
"@nrwl/angular:application": {
"style": "scss",
"linter": "eslint",
"unitTestRunner": "jest",
"e2eTestRunner": "cypress",
"strict": true,
"prefix": "rm",
"standalone": true
},
"@nrwl/angular:library": {
"linter": "eslint",
"unitTestRunner": "jest",
"strict": true,
"prefix": "rm",
"style": "scss",
"changeDetection": "OnPush",
"standalone": true
},
"@nrwl/angular:component": {
"style": "scss",
"prefix": "rm",
"changeDetection": "OnPush",
"standalone": true
}
}
}

Now, we will generate a ui library for linkinbio. I would be using the UI to create the lib, which would use the following command.

$ yarn nx generate @nrwl/angular:library shared \
--buildable \
--directory=linkinbio/ui \
--no-interactive

Our new library is now available at /libs/linkinbio/ui/shared.

You can now quickly build the library using the nx-console, project.json or with the following command

$ nx build linkinbio-ui-shared

Now, let’s quickly create a new application. Again, I will use nx-console to initialize the app, which eventually uses the following command.

$ nx generate @nrwl/angular:application linkinbio

I learned that we must manually add the missing @angular-eslint/eslint-plugin since it is not added after scaffolding. Let’s add that package too.

$ yarn add -D @angular-eslint/eslint-plugin

Now, we can launch the newly created app

$ nx serve linkinbio

Let’s make the following changes to the app

  1. Add imports: [LinkinbioUiSharedComponent] to app.component.ts
  2. Update the content of app.component.html to just [ ]
  3. Remove nx-welcome.component.ts

You can see our app successfully imported a component from a library project.

Screenshot-2023-03-13-at-01.26.07

Let us build the app to ensure proper library imports as well as incremental builds.

$ nx build linkinbio
✔ 1/1 dependent project tasks succeeded [0 read from cache]
Hint: you can run the command with --verbose to see the full dependent project outputs
———————————————————————————————————————————————————————————————————————————————————————————————————————————————————
> nx run linkinbio:build:production
✔ Browser application bundle generation complete.
✔ Copying assets complete.
✔ Index html generation complete.
Initial Chunk Files | Names | Raw Size | Estimated Transfer Size
main.068e18dfd9616db8.js | main | 82.28 kB | 25.05 kB
polyfills.a0e551d9f0aaa365.js | polyfills | 33.09 kB | 10.65 kB
runtime.dfd26405657e5395.js | runtime | 896 bytes | 509 bytes
styles.ef46db3751d8e999.css | styles | 0 bytes | -
| Initial Total | 116.24 kB | 36.19 kB
Build at: 2023-03-13T00:27:27.824Z - Hash: 0e4718ad16d04a5c - Time: 5027ms
———————————————————————————————————————————————————————————————————————————————————————————————————————————————————
> NX Successfully ran target build for project linkinbio and 1 task it depends on (8s)

You will see that the initial instructions are to build the library before building the app. In the end, the app builds successfully

This will be it for tonight. We will work with Angular Material and Storybook tomorrow to create UI components.

See you tomorrow.