Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SCSS with angular library created using angular-cli

I have created an angular library project where I want to use SCSS for styles.

So I have configured

ng config schematics.@schematics/angular:component.styleext scss

and this made an entry to angular.json file

"schematics": {
    "@schematics/angular:component": {
      "styleext": "scss"
    }
}

Now I am using materialize-css UI library in my library components. And it requires to import its SCSS file.

I am not seeing styles.scss file where I can import this and my components and other common styles?

I tried creating one and making entry into angular.json

"styles": ["projects/library_name/styles.scss"]

Under projects -> library_name -> architect -> build -> options

but this is showing error while building the library project

Schema validation failed with the following errors:
  Data path "" should NOT have additional properties(styles).

Update

I got this, addressing the same issue if it helps!

like image 971
Sunil Garg Avatar asked Dec 27 '18 09:12

Sunil Garg


2 Answers

I know it was late but, maybe help with somebody's idea.

  1. Create style.scss in the library which the same path with the ng-package.json.
  2. Open ng-package.json
  3. Add assets:["style.scss"] property
  4. run ng build @your-library-name --prod
  5. You will see style.scss in your dist/@your-library-name.
  6. Then it is importable

ng-package.json

{
  "$schema": "../../../node_modules/ng-packagr/ng-package.schema.json",
  "dest": "../../../dist/logo-software/accordion",
  "lib": {
    "entryFile": "src/public-api.ts"
  },
  "whitelistedNonPeerDependencies": [
    "@logo-software/theme"
  ],
  "assets": [
    "style.scss"
  ]
}

If you want to use this style.scss in your current library: Add encapsulation: ViewEncapsulation.None to the top main @Component of your library.

accordion.component.ts

@Component({
  selector: 'logo-accordion',
  templateUrl: './accordion.component.html',
  styleUrls: ['./accordion.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class AccordionComponent implements OnInit {}

And the last thing you can use now this style.scss at any component.

accordion.component.scss

// `@import "~@logo-software/theme/style";` for external import

@import "../../style"; // path to the style.scss file

:host{
}
like image 92
Serkan KONAKCI Avatar answered Sep 30 '22 21:09

Serkan KONAKCI


Till the date (10-Jan-2019, there is no direct support for global scss in the library even though this is a very common scenario.

From this discussion, I got the workaround that I need to bundle it myself. So I used scss-bundle to create one big scss file. You can add it using

yarn add scss-bundle@next -D

and then the script to bundle and run in watch mode

"build-lib-watch-styles": "scss-bundle -e \"./projects/lib-name/src/lib/styles/lib-name.scss\" -d \"./dist-lib/lib-name/styles/lib-name.scss\" -w \"./projects/lib-name/src/lib/styles\"",
like image 30
Sunil Garg Avatar answered Sep 30 '22 19:09

Sunil Garg