DEV Community

Cover image for A simple setup to simplify your Style File Imports into your Angular app
Renan Ferro
Renan Ferro

Posted on

2 1 1

A simple setup to simplify your Style File Imports into your Angular app

Hi guys, how are you?!

Have you ever wanted to simplify importing style files into your app, component, or whatever?!

Today I would like to share a simple, but maybe not well known configuration of angular.json and with that we can simplify the way of importing the style files!


For example, to import the theme-tokens.scss file in your home.component.ts you should write:

@import "src/resources/sass/frontend/theme-tokens.scss";
Enter fullscreen mode Exit fullscreen mode

Soo long, right?! Imagine writing this whole line in several components.

Buuut relaaax, the awesome Angular and with Sass give us includePaths feature for component and global styles, and with that we can set up extra base path for your app and no longer write long imports lines!

Image description

So, let's see how to make this!!!


✅ Implementing the configuration:

In your angular.json file, include the stylePreprocessorOptions with the includePaths property and your extra path, as below:

{
  ...
  "projects": {
    "your-app": {
      "architect": {
        ...
        "build": {
          ...
          "stylePreprocessorOptions": {
            "includePaths": [
              "src/resources/sass"
            ]
          },
          ...
        },
        ...
      },
      ...
    }
  }
 }
}
Enter fullscreen mode Exit fullscreen mode

Annd now you can just import your style file more easily and simpler in your component.scss or something else, take a look:

⛔️ Old Way:
@import "src/resources/sass/frontend/theme-tokens.scss";
Enter fullscreen mode Exit fullscreen mode
🔥 New Way:
@import "frontend/theme-tokens.scss";
Enter fullscreen mode Exit fullscreen mode

Awesome, right?!

A simple configuration and with that we can improve our development time and simplify and standardize our structure!

If you want, you can see the Angular documentation here!


That's all guys, I hope you like it and if any questions, suggestions or other topics, please comment!

See you later ✌🏼

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay