Using SCSS with angular library created using angular-cli

蓝咒 提交于 2021-02-09 07:00:23

问题


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!


回答1:


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\"",



回答2:


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{
}


来源:https://stackoverflow.com/questions/53942871/using-scss-with-angular-library-created-using-angular-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!