1

Hello everyone happy New Year 2022! I'm currently working on my angular library and I have two issues I seem unable to fix despite the documentations and the time I've spent looking for a solution. This is what I'm trying to achieve:

  1. I'd like to setup a main entrypoint inside my angular lib so the link is cleaner
  2. I'd like to use styleIncludePaths from ng-packagr in order to shorten my SCSS imports

This is what my current architecture looks like:

enter image description here

// ng-package.json

{
  "$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
  "dest": "../../dist/core",
  "lib": {
    "entryFile": "src/public-api.ts",
    "styleIncludePaths": [
      "./src/lib/scss"
    ]
  },
  "assets": [
    "./src/lib/scss"
  ]
}

// package.json

"name": "@ngx-chrono-ui-kit/core",
...
"exports": {
    ".": {
      "sass": "./lib/scss/_index.scss"
    }
  }

So I'm currently calling my lib this way (the 'src' folder doesn't exist in the dist):

@import '~@ngx-chrono-ui-kit/core/lib/scss';

But I'd like to change it so it looks like this:

@import '~@ngx-chrono-ui-kit/core';

Sadly, with the above configuration it does not work:

enter image description here

Second, in my components (i.e: components/ActionBar/action-bar.component.scss) I'm calling SCSS definitions that I have defined in my SCSS folder:

@use '../../scss/abstracts/variables' as variables;
@use '../../scss/abstracts/colors' as colors;
@use '../../scss/abstracts/typography' as typography;
@use '../../scss/abstracts/mixins' as mixins;
@use '../../scss/abstracts/functions' as functions;

I would like to shorten the links so they look like this:

@use 'abstracts/variables' as variables;
@use 'abstracts/colors' as colors;
@use 'abstracts/typography' as typography;
@use 'abstracts/mixins' as mixins;
@use 'abstracts/functions' as functions;

But my IDE actually warns me that it cannot resolve the imports (and while building, it does not work as well):

enter image description here

Please if you're seeing an error in my configuration feel free to react

Timtim
  • 324
  • 8
  • 18

1 Answers1

5

Hello and happy new year to you too! :) As for your question:

  1. styleIncludePaths is meant to be used when you want to resolve global styles within your lib during main app build: https://github.com/ng-packagr/ng-packagr/blob/master/docs/style-include-paths.md. Here is a very good explanation by user Fy Z1K that goes into detail. As far as I understand, you're attempting to export your library's own scss styles - so in this case, styleIncludePaths will not help.
  2. ng-packagr copies the ./src/lib/scss as-is, which means that after you build your library, you should reference the copied directory like this: @import '~@ngx-chrono-ui-kit/core/src/lib/scss';. ng-packagr should create the directory tree if it doesn't exist. Also, you can't shorten the URL using the "exports" package.json config because it only applies to JavaScript modules and not scss/css files: https://nodejs.org/api/packages.html#exports.

What I can suggest as a solution is to move your library's /scss dir to the root level of the library's directory and then adjusting your ng-packagr config to match that:

  1. Move ./projects/<your-project-name>/src/lib/scss to ./projects/<your-project-name>/scss
  2. Change your "assets" config in ng-package.json to be:
"assets": [
 "./scss"
]

After compiling the library, you should be able to reference the copied /scss dir as ~@ngx-chrono-ui-kit/core/scss.

Of course, moving the directory some levels up would require you to change all references to the /scss directory within the source code of the library.

I have stumbled upon the same issue and this is the only solution I have for now.

ardentia
  • 684
  • 1
  • 9
  • Hello! Thank you very much for you detailed answer and all the information you gathered in your post. I didn't find that post you linked in your anwser and mine's basically a duplicate as I'm facing the same issue with "styleIncludePaths".That also points out the fact that they probably should document the functionality with more details by adding what was specified in the various comments, both on Github and StackOverflow. Finally, with all the information you brought and your recommandations I've been able to solve half my issues, but it's clean enough for me to solve the question. – Timtim Jan 18 '22 at 15:21
  • Following your recommandations, I've set "styleIncludePaths": ["./scss"] in my ng-package.json which now allows me to use shorten paths directly inside my library components files, using it this way: @use 'scss/abstracts/variables' as variables; So once again thank you for your help as you were the only one who answered this and I believe it will probably help people facing the same questions and issues in the future! – Timtim Jan 18 '22 at 15:24
  • @Timtim I'm glad I could help! I agree that we all need better and more detailed documentation when it comes to ng-packagr. – ardentia Jan 18 '22 at 17:44