58

I'm in the process refactoring an Angular application and as I'm moving components around to new directory locations, I'm finding that dealing @import paths in the components' SCSS files is getting a bit tedious.

For example, let's say I have the file src/_mixins.scss in the root of my application and the component src/app/my-widget/my-widget.component.scss imports the SCSS like so @import '../../mixins';. All well and good.

But then I decide that my-widget.component is really be a "shared component" under another component my-thingy.component, so I create a shared folder under src/app/my-thingy and move everything that was in src/app/my-widget into it.

I hope you're still with me... So, now I've got src/app/my-thingy/shared/my-widget and I modify my SCSS file to import @import '../../../../mixins'.

Note: This is a simplified example. Some of the paths get relatively deep (no pun intended... or was it?) and all of these . and / are a bit much.

TL;DR

It would be super-handy if from the start I could have just imported _mixins.scss relative to the root in all of my components' SCSS files so I don't have to keep messing with the @import paths when refactoring. (e.g. Something along the lines of @import '~/mixins'). Does something like this exist?

What I've tried (and, sadly, has failed):

  1. @import '~/mixins'; /** I had real hope for this one **/
  2. @import 'mixins'; /** Just being overly optimistic here **/
  3. @import '~//mixins'; /** Now I'm just making stuff up **/

I understand that I'm already going to have to modify my mod files to point to the new path of the component with all of this "moving stuff around", but... hey, one less thing, right?

Steve
  • 11,596
  • 7
  • 39
  • 53

6 Answers6

77
  • If you are using Angular CLI, take a look at Global styles, "stylePreprocessorOptions" option specifically.
  • For webpack, you can configure includePaths in sassLoader plugin configuration.
  • And it's similar for gulp builds, you pass includePaths to the sass plugin.

Whatever your build tool is, sass will treat those paths as root, so you can import them directly, so with:

includePaths: ["anywhere/on/my/disk"]

you can just @import 'styles' instead of @import 'anywhere/on/my/disk/styles'.

nshew13
  • 3,052
  • 5
  • 25
  • 39
Sasxa
  • 40,334
  • 16
  • 88
  • 102
  • 3
    I had a problem with `npm test` (using Karma) after upgrading Angular 5 to Angular 7 using the automated CLI command. Turns out the "test.options" section in `angular.json` needed the `stylePreprocessorOptions` as well! – Boris Yakubchik Nov 12 '18 at 19:55
  • Thanks Boris, I had exactly the same problem! – Ash McConnell Mar 28 '19 at 08:27
  • Can't figure it out: let's say I use `@import "@import "~mapbox-gl/dist/mapbox-gl.css` for my Angular library and it points to the 3rd party MapBox library CSS installed in `node_modules/mapbox-gl/...` - how to make it point to right direction in the app I install my library to, instead of `https://localhost:4200/~/mapbox-gl/...`? – Alexander May 05 '22 at 16:55
19

If I've understood the question correctly then using @import 'src/app/...' works correctly.

e.g.

@import 'src/mixins';

Note that there's no leading backslash on the path.

(this is tested on Angular 9)

Peter Nixey
  • 16,187
  • 14
  • 79
  • 133
15

You can use {} to reference the top level of the project path when using meteor-scss, hence something like that will work.

@import "{}/node_modules/module-name/stylesheet";

You can also use the tilda operator ~ If using webpack (or angular-cli), accessing libraries under node_modules as per the following -- Credit to @splintor

@import "~module-name/stylesheet";
helcode
  • 1,859
  • 1
  • 13
  • 32
  • 6
    If using webpack (or angular-cli), accessing libraries under `node_modules` can also be done using the tilde operator - `@import "~module-name/stylesheet";` – splintor Mar 18 '18 at 11:43
  • @Jessycormier as explained in the answer `{}` represents the project root. Hence it may be easier to reference files from root (absolute reference) than a relative reference from file location (ex. via `../../`) – helcode Nov 09 '18 at 19:07
  • Neither Angular's webpack configuration nor the Jetbrains sass tools know how to resolve anything with this syntax. – Kevin Beal Jun 25 '20 at 16:33
  • Above referencing uses [meteor-scss](https://github.com/Meteor-Community-Packages/meteor-scss) – helcode Jun 28 '20 at 22:09
7

The definition of paths depends on your version of Angular. In our project, old versions use angular-cli.json and new ones use angular.json:

for "@angular/cli": "~1.7.4" use angular-cli.json:

"stylePreprocessorOptions": {
    "includePaths": [
      "../src",
      "./scss"
    ]
  },

for "@angular/cli": "~7.0.6":

"stylePreprocessorOptions": {
    "includePaths": [
       "./src",
       "./src/scss"
    ]
 }
Youp Bernoulli
  • 5,303
  • 5
  • 39
  • 59
Serkan KONAKCI
  • 1,100
  • 11
  • 16
  • And may I add that if you use preprocessor paths then you should not use tilde in import. I learn this in hard way. – Janne Harju Jan 31 '19 at 11:34
4

solution for angular-cli is to add stylePreprocessorOptions to .angular-cli.json.

{
    "apps": [{
        ...
        "stylePreprocessorOptions": {
            "includePaths": [
                "./app/global-styles"
            ]
        },
        ...
    }]
}

if you use server-side rendering remember to add this for both ssr and main app build - otherwise you will get NodeInvocationException: Prerendering failed because of error: Error: Cannot find module 'C:\Users\...\ClientApp\dist-server\main.bundle.js'

{
    "apps": [{
            ...
            "outDir": "dist",
            "stylePreprocessorOptions": {
                "includePaths": [
                    "./app/global-styles"
                ]
            },
            ...
        },
        {
            ...
            "name": "ssr",
            "outDir": "dist-server",
            "stylePreprocessorOptions": {
                "includePaths": [
                    "./app/global-styles"
                ]
            },
            ...
        }
    ]
}
godblessstrawberry
  • 4,556
  • 2
  • 40
  • 58
3

What worked for me was

/* file src/assets/scss/app.scss */

@import "src/assets/css/style";
  1. Removed the '.css' at the end of the file name,
  2. Imported from "src/assets/...” (no backslash)

This imported a .css file from .scss in Angular 11 + Webpack,

user9869932
  • 6,571
  • 3
  • 55
  • 49