Angular 6 library shared stylesheets

后端 未结 4 1260
清酒与你
清酒与你 2021-01-31 03:28

How can you setup a index.scss and import global stylesheets for variables, mixins, etc, to an angular 6 library?

Angular CLI generates a lib with a root component &

相关标签:
4条回答
  • 2021-01-31 04:08

    For global styles, I've answered it in this question.

    Update

    For ng-packgr versions 9.x and above

    Copying assest to output folder is now directly supported as explained in this page

    {
      "$schema": "./node_modules/ng-packagr/package.schema.json",
      "name": "@my/library",
      "version": "1.0.0",
      "ngPackage": {
        "assets": [
          "CHANGELOG.md",
          "./styles/**/*.theme.scss"
        ],
        "lib": {
          ...
        }
      }
    }
    

    Old Answer

    **For other versions**
    1. Create an index.scss file in your library's root folder. If you follow this guide from Angular, then your path will be my-project/projects/my-library/index.scss. This is also the folder where your package.json is.

    So, index.scss will be the file with your variables and mixins

    $grey: #222;
    @mixin mymixin {
        background: #222;
    }
    
    1. Include this in you library scss files using import
    @import '../../index.scss';
    

    or whatever relative path your component scss file is at.

    1. Now in order to have this file in your app project, copy it post build to the dist directory. To do this, edit your angular library's project's package.json file (NOT THE LIBRARY'S).
    {
        "name": "my-project",
        "version": "1.0.0",
        "scripts": {
            "ng": "ng",
            "start": "ng serve",
            "build": "ng build && npm run copyScss",
            "test": "ng test",
            "lint": "ng lint",
            "e2e": "ng e2e",
            "copyScss": "xcopy \"projects\my-library\index.scss\" \"dist\\my-library\\\""
        },
    
        ...
    }
    
    1. Now, very important, DO NOT use ng build to build your library, instead use npm run build. This will automatically execute the copy command. Now the index.scss file is exported along with your library in the my-project/dist folder.

    2. Include the index.scss in your app project's scss files

    // ~ stands for the node_modules folder
    @import '~my-library/index.scss';
    

    Now you have all your library mixins in all of the projects you installed your library.

    Cheers!

    PS Workarounds are not the most elegant solutions, but when nothing else works, they work around!

    0 讨论(0)
  • 2021-01-31 04:09

    Have you tried setting the encapsulation level of the component to none as part of the component metadata? Like this:

    @Component({
      selector: 'app-component',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.scss'],
      encapsulation: ViewEncapsulation.None
    })
    

    The ViewEncapsulation levels are:

    • ViewEncapsulation.Emulated: The default, Angular emulates a Shadow DOM.
    • ViewEncapsulation.Native: Use the browser's own Shadow DOM.
    • ViewEncapsulation.None: Styles are Global

    Check out the Angular Docs on ViewEncapsulation and I wrote a blog post on it.

    0 讨论(0)
  • 2021-01-31 04:14

    I think people may be missing the fact that while encapsulation options exist, global styles are output in style tags and can cascade throughout your project.

    Here is my setup:

    1. My global styles: styles.scss is output in style tags. The rules target all matching classes and elements as expected. Even within components.

    2. Component styles are encapsulated. That is their value prop. They overwrite globals and keeps component-specific styles with the component family.

    3. @includes such as variables.scss, mixins.scss etc. are included explicitly at the top of any component .scss files when needed.

    @import '../../../../scss/includes/variables';

    What else would we need?

    0 讨论(0)
  • 2021-01-31 04:18

    Run ng init on the project, so that it will initialize the project as an Angular CLI project.

    Edit: My bad, it seems they removed init from the CLI. You can try ng new --src

    It might overwrite some files, so try this on a copy of the project.

    0 讨论(0)
提交回复
热议问题