4

I am trying to create scss theming based Angular(ng 11) library which I will use in separate projects through private npm registry. Right now I am trying to add a global scss file into my library and bundle it with my library.

What I want:

let's say I keep my scss in projects/my-lib/src/styles/_material.scss:

// Import library functions for theme creation.
@import '~@angular/material/theming';

// Include non-theme styles for core.
@include mat-core();

// Define your application's custom theme.
$primary: mat-palette($mat-indigo);
$accent: mat-palette($mat-pink, A200, A100, A400);
$theme: mat-light-theme($primary, $accent);

// Include theme styles for Angular Material components.
@include angular-material-theme($theme);

folder structure of angular app and library

I want my bundle(library package) to contain this scss so that after installing this library app can import that into their global style.scss file.

projects/my-app/src/style.scss

@import 'my-lib/_material.scss'

What I did:

I followed this documentation from angular Managing assets in library

library package.json (for the sake of simplicity I kept scss file outside src folder right now):

{
  "$schema": "./node_modules/ng-packagr/package.schema.json",
  "name": "ui-sdk",
  "version": "0.0.1",
  "ngPackage": {
    "assets": [
      "CHANGELOG.md",
      "./styles/material.scss"
    ],
    "lib": {
      "entryFile": "src/public-api.ts"
    }
  },
  "peerDependencies": {
    "@angular/common": "^9.0.6",
    "@angular/core": "^9.0.6",
    "tslib": "^1.10.0",
    "ng-packagr": "^11.2.4"
  },
  "dependencies": {
  }
}

After doing these changes I am facing 2 problems:

  1. ng build is adding library bundle under "projects/my-lib/dist"
  2. library bundle does not contain library folder (my-lib) therefore not able to resolve the path.

dist folder is inside library folder

dist does not contain library folder

Can someone explain a way how we can create a library where I can add material theming and app which extend those themes can use the theme? I have tried official docs as well as other sources but not reached a solution.

Any help or reference would be very much appreciated!

Always_a_learner
  • 4,585
  • 13
  • 63
  • 112

2 Answers2

6

To get hold of your theme SCSS file from your importing application you will need to;

Create the SCSS theme file (which you've already done)

Copy over the SCSS asset using the ng-package.json file

{
  ...
  "assets": [
    "./src/styles/_material.scss"
  ]
  ...
}

Export a virtual access path (relative to your library name) in package.json

{
  ...
  "exports": {
    "./theming": {
      "sass": "./src/styles/_material.scss"
    }
  }
  ...
}

Import theme file from importing/consuming application

@import "~@my-lib/theming";

Orson
  • 14,981
  • 11
  • 56
  • 70
  • I can see `_material.scss` under `dist/my-lib/src/styles` and the `package.json` under `dist/my-lib` shows the manually added exports. However `@import "~@my-lib/theming";` gives `Can't find stylesheet to import.` with `ng serve` any ideas? – Hasan Can Saral Feb 09 '22 at 15:58
  • Do I have to publish to npm for this method to work? – Hasan Can Saral Feb 09 '22 at 18:38
  • @HasanCanSaral you should link your path. for this example like this. => build lib project => ng build --project=my-lib => go to dist folder => cd dist/my-lib and run "npm link" => after that. go to application path. and run "npm link my-lib" – Emre HIZLI Mar 10 '22 at 08:25
0

I have faced similar issue with ng-packagr. I have not yet found a right way to do it but the workaround is you can simply copy the .scss file manually to the final build. You can use ncp - Asynchronous recursive file & directory copying to copy the file to final build.

after the final build command just add && ncp currentpathOfSCSSFile destinationPathOfFinalBuildDistFolder

Just a workaround, but it will resolve this issue ;)

  • Guruprasad mishra Thanks for sharing this solution. I tried this, npm i ncp and then ran "ncp \"./projects/ui-sdk/src/lib/styles/**/*\" \"./dist/ui-sdk/styles\"" after building library but getting error sh: ncp: command not found I installed ncp in my main project space and tried after installing within library project. – Always_a_learner Mar 06 '21 at 11:48
  • check if ncp is added to package.json or not, check in the dependencies/devDependencies. if that is there try removing package-lock.json and run npm install again. And also give the relative path for source to destination "ncp ../projects/ui-sdk/src/lib/styles/style.scss ./dist/ui-sdk/styles/style.scss". Let me know what you have tried to resolve this issue. ThankYou – Guruprasad mishra Mar 07 '21 at 07:50
  • let me know if the solution is working or you are facing any other issue. Also if you are implementing other approach let me know. Thanks – Guruprasad mishra Mar 08 '21 at 16:24
  • Guruprasad mishra Hi, I have found a solution, will post here shortly after testing. – Always_a_learner Mar 09 '21 at 03:52
  • @Always_a_learner - can you post your solution? – David Jeyathilak Oct 20 '22 at 17:49