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": {
...
}
}
}
So in your project you would use the styles by importing the files from the library in the following way:
@import '@my/library/path-to-file/file-name.scss'
or using explicit relative path (make sure to use as may ../ as required)
@import '../node_modules/@my/library/path-to-file/file-name.scss'
Old Answer
**For other versions**
- Create an
index.scssfile in your library’s root folder. If you follow this guide from Angular, then your path will bemy-project/projects/my-library/index.scss. This is also the folder where yourpackage.jsonis.
So, index.scss will be the file with your variables and mixins
$grey: #222;
@mixin mymixin {
background: #222;
}
- Include this in you library scss files using
import
@import '../../index.scss';
or whatever relative path your component scss file is at.
- 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.jsonfile (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 buildto build your library, instead usenpm run build. This will automatically execute the copy command. Now theindex.scssfile is exported along with your library in themy-project/distfolder. -
Include the
index.scssin 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!