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 &
For global styles, I've answered it in this question.
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": {
...
}
}
}
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;
}
import@import '../../index.scss';
or whatever relative path your component scss file is at.
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\\\""
},
...
}
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.
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!