loading...

Sharing styles between apps inside Nx workspace

vitorstick profile image Vitor Ferreira Updated on ・2 min read

In my current job we have decided to make the change into Nx workspace and with that we decided to create our very own Design System.

But I didn't want to maintain the same styles in more than one application.

Creating a lib for styles

The best solution for my case was to create a lib named ui for the styles:

nx generate @nrwl/angular:library ui

Then I moved the sass files into the library:
alt text

The problem now, is the @import in all the scss files.
How to make them recognize the correct files?
On angular.json on every project the path will have to be included.

"projects": {
    "ds-project": {
        "projectType": "application",
        ...
        "architect": {
            "build": {
                ...
                "stylePreprocessorOptions": {
                    "includePaths": [ "libs/ui/src/lib/styles" ]
                },
                "extractCss": true,
                ...

Now you can import the mixins on the scss files of your project just like if they were still part of the project:

@import "mixins/list_mixin";
@import "variables";

@include list_layout;

Even the base style, like font-family are importable.

Inside the style.scss of the project to became the global styles (for this case the module contains the global styles).

// styles.scss
/* You can add global styles to this file, and also import other style files */

@import 'module';

alt text

Conclusion

This solves easily a problem that could be an issue and I was somehow afraid to tackle, that is having global scss files shared between applications and easily maintainable with only one source of truth (the library).

If you have any suggestion of how I could do this differently, please share your ideas.

Posted on by:

vitorstick profile

Vitor Ferreira

@vitorstick

Software developer @Hovione

Discussion

markdown guide
 
 

Will 'nx dep-graph' command recognize the dependency of the library in projects?

 

I don't think so, at least for my case it does appear as a dependency.