Angular (+ CLI) 6.0.0: Global styles for libraries
Versions
Angular CLI: 6.0.1
Node: 8.9.4
OS: win32 x64
Angular:
...
Package Version
------------------------------------------------------
@angular-devkit/architect 0.6.1
@angular-devkit/core 0.6.1
@angular-devkit/schematics 0.6.1
@schematics/angular 0.6.1
@schematics/update 0.6.1
rxjs 6.1.0
typescript 2.7.2
Repro steps
- Step 1: generate a new library
- Step 2: add some global styles to the library
- Step 3: run
ng build your-library - Step 4: global styles are not in
./dist
Observed behavior
The global styles from the library are not copied/used
Desired behavior
Global styles inside a library should be part of the generated output. I know that I could use component level styles (this works). However this doesnt work in all use cases. For example I have some css to style buttons but I dont have a ButtonComponent (and I dont want one) since I am only modifying the html <button> tag with the css and I am not adding some behaviour.
Mention any other details that might be useful (optional)
Hi @ramandeep-singh-1983
"scss-bundle" was not being called
Yeah I was launching that separately back then. Now, here is my latest package.json commands which work really nice. I need to run only the watch command and it does everything:
"scripts": {
"clean": "rimraf \"./dist/*\"",
"watch": "npm run clean && concurrently \"ng build --watch=true\" \"npm:watch-scss-*\" \"npm:watch-assets\"",
"watch-scss-framework": "scss-bundle -e \"./framework/styles/_framework.scss\" -d \"./dist/framework/styles/_framework.scss\" -w \"./framework/styles\"",
"watch-scss-variables": "scss-bundle -e \"./framework/styles/variables.scss\" -d \"./dist/framework/styles/variables.scss\" -w \"./framework/styles\"",
"watch-assets": "cpx \"./framework/assets/**/*\" \"./dist/framework/assets\" --watch --verbose"
}
I bundled my variables.scss file separately so users of my framework could refer to them without importing entire framework.scss.
To answer your question, good sir, in your app (not lib) go to the angular.json and add this under projects\MyAwesomeApp\architect\build\options:
"stylePreprocessorOptions": {
"includePaths": [
"/dist/my-lib/styles/"
]
},
Ater that, in your app in any .scss file you should be able to write @import "styles"; to bring in your lib's _styles.scss.
Thanks! it worked. I also referred this article about global styles: https://scotch.io/tutorials/angular-shortcut-to-importing-styles-files-in-components
Another question: My global style is being picked up from the library scss file now but what about the assets directory. Right now, the dist directory has this structure:
my-app
app js files assets my-lib:
assets styles Suppose my library is a full fledged GUI library. The global styles reside in my library and are picked up by the app using @import statement. But what about the assets directory (icons, fonts etc.). They are part of my library but i have to copy them over to the my-app/assets directory for it to work. The my-lib directory resides outside the my-app directory in the dist directory so is it possible to pick up assets from the my-lib directory?
The my-app should be independent of my-lib so ideally it shouldn't need to contain assets for my-lib and all assets belonging to my-lib should be part of my-lib directory only.
@ramandeep-singh-1983 My watch-assets command is just a cpx that gonna copy my assets from my lib project to my lib project's dist\assets folder.
Then I have to config each of my client apps to do this glob:
"assets": [
"src/favicon.png",
"src/assets",
{
"glob": "**/*",
"input": "node_modules/@dynatron/framework/assets/",
"output": "./assets/framework"
}
]
This goes in the angular.json under projects\MyAwesomeApp\architect\build\options
This glob gonna copy all my framework assets into my client site's assets/framework folder where they can be used. That way my framework could provide multimedia assets to be used by all clients.
Hope it helps bro! 🍺
Excellent discussion here! I can already see a few workarounds which seem reasonable. Although I'd discourage global styles in libraries, if your use case require such I'd recommend you to look at ngx-build-plus. You can customize your build using it.
Closing the issue here. If you think we're missing something and it's essential for the CLI to support this out of the box (i.e., you have use cases that we have thought about), please open a new issue.
Thanks @hughanderson4 for your help, the assets thing worked too :) @mgechev, thanks for the info about ngx-build-plus, i will explore it.
About the need to bundle global style in libraries: I am creating a UI application as a library and don't want to be dependent on the base app for the styles or assets, so my requirement is to have everything inside the library itself.
Hey guys.
The best solution IMHO is using cpx in the package.json to copy the css files and use npm run build.lib when building your library:
"scripts": {
...
"build.lib": "ng build my-lib && npm run copy-styles",
"copy-styles": "cpx \"./projects/my-lib/src/lib/assets/styles/**/*.scss\" \"./dist/my-lib/assets/styles/\"",
...
}
In your main application configure the tsconfig PATHS for pointing to your lib directory and than use your lib as you do with installed ones.
tsconfig.json
{
...
"compilerOptions": {
...
"paths": {
"my-lib": [
"projects/my-lib"
],
...
}
}
}
my-component.scss
@import '~my-lib/assets/my-style';
When the cli notice the ~ in the import it will try to load from the configured path and if nothing is found it will look inside node_modules so you can use the local lib or the installed one without caring about changing the imports. It is like simulating an installation of the lib.
With this approach you can use paths everywhere to access different places in your cli app.
Hi @ramandeep-singh-1983
"scss-bundle" was not being called
Yeah I was launching that separately back then. Now, here is my latest package.json commands which work really nice. I need to run only the
watchcommand and it does everything:"scripts": { "clean": "rimraf \"./dist/*\"", "watch": "npm run clean && concurrently \"ng build --watch=true\" \"npm:watch-scss-*\" \"npm:watch-assets\"", "watch-scss-framework": "scss-bundle -e \"./framework/styles/_framework.scss\" -d \"./dist/framework/styles/_framework.scss\" -w \"./framework/styles\"", "watch-scss-variables": "scss-bundle -e \"./framework/styles/variables.scss\" -d \"./dist/framework/styles/variables.scss\" -w \"./framework/styles\"", "watch-assets": "cpx \"./framework/assets/**/*\" \"./dist/framework/assets\" --watch --verbose" }I bundled my variables.scss file separately so users of my framework could refer to them without importing entire framework.scss.
To answer your question, good sir, in your app (not lib) go to the angular.json and add this under
projects\MyAwesomeApp\architect\build\options:"stylePreprocessorOptions": { "includePaths": [ "/dist/my-lib/styles/" ] },Ater that, in your app in any .scss file you should be able to write
@import "styles";to bring in your lib's _styles.scss.
Hi thanks for your build scripts,
- but I am still not able to run
scss-bundlein watch mode this is how I am using
"watch-scss": "scss-bundle -e \"./projects/my-lib/src/lib/sass/my-lib.scss\" -d \"./dist-lib/my-lib/sass/my-lib.scss\" -w \"./projects/my-lib/src/lib/sass\""
- I want to run both my application project and my library in watch mode so that if there is any change it should re-compiled automatically and these are the scripts I am using
"scripts": {
"start": "yarn clean && ng build my-lib && concurrently \"ng serve\" \"ng build qwc --watch=true\" \"yarn:watch-scss\"",
"clean": "rimraf \"./dist-lib/*\"",
"watch-scss": "scss-bundle -e \"./projects/my-lib/src/lib/sass/my-lib.scss\" -d \"./dist-lib/my-lib/sass/my-lib.scss\" -w \"./projects/my-lib/src/lib/sass\""
}
but this fails by saying
ERROR in src/app/app.module.ts(23,27): error TS2307: Cannot find module 'my-lib'.
can you please help?
Hi @ramandeep-singh-1983
"scss-bundle" was not being called
Yeah I was launching that separately back then. Now, here is my latest package.json commands which work really nice. I need to run only the
watchcommand and it does everything:"scripts": { "clean": "rimraf \"./dist/*\"", "watch": "npm run clean && concurrently \"ng build --watch=true\" \"npm:watch-scss-*\" \"npm:watch-assets\"", "watch-scss-framework": "scss-bundle -e \"./framework/styles/_framework.scss\" -d \"./dist/framework/styles/_framework.scss\" -w \"./framework/styles\"", "watch-scss-variables": "scss-bundle -e \"./framework/styles/variables.scss\" -d \"./dist/framework/styles/variables.scss\" -w \"./framework/styles\"", "watch-assets": "cpx \"./framework/assets/**/*\" \"./dist/framework/assets\" --watch --verbose" }I bundled my variables.scss file separately so users of my framework could refer to them without importing entire framework.scss. To answer your question, good sir, in your app (not lib) go to the angular.json and add this under
projects\MyAwesomeApp\architect\build\options:"stylePreprocessorOptions": { "includePaths": [ "/dist/my-lib/styles/" ] },Ater that, in your app in any .scss file you should be able to write
@import "styles";to bring in your lib's _styles.scss.Hi thanks for your build scripts,
- but I am still not able to run
scss-bundlein watch mode this is how I am using
"watch-scss": "scss-bundle -e \"./projects/my-lib/src/lib/sass/my-lib.scss\" -d \"./dist-lib/my-lib/sass/my-lib.scss\" -w \"./projects/my-lib/src/lib/sass\""
- I want to run both my application project and my library in watch mode so that if there is any change it should re-compiled automatically and these are the scripts I am using
"scripts": { "start": "yarn clean && ng build my-lib && concurrently \"ng serve\" \"ng build qwc --watch=true\" \"yarn:watch-scss\"", "clean": "rimraf \"./dist-lib/*\"", "watch-scss": "scss-bundle -e \"./projects/my-lib/src/lib/sass/my-lib.scss\" -d \"./dist-lib/my-lib/sass/my-lib.scss\" -w \"./projects/my-lib/src/lib/sass\"" }but this fails by saying
ERROR in src/app/app.module.ts(23,27): error TS2307: Cannot find module 'my-lib'.can you please help?
The following set works for me:
"clean": "rimraf "./dist/"", "watch-scss": "scss-bundle -e "./projects/my-lib/src/styles/_my-lib.scss" -d "./dist/my-lib/styles/_my-lib.scss" -w "./projects/my-lib/src/styles"", "watch-assets": "cpx "./projects/my-lib/src/assets/**/" "./dist/my-lib/assets" --watch --verbose", "watch": "npm run clean && concurrently "ng build --watch=true" "npm:watch-scss" "npm:watch-assets"",
I use 2 terminals, in one i use "npm run watch" and in the other i run "ng serve" and whenever i make any changes to my library's code, both build up pretty fast and the Angular app is automatically updated.
@ramandeep-singh-1983 Thanks for your answer.
-
But isn't
ng build --watch=truewill build the app itself rather than the library. -
And My library is dependent on materialize-css and I need to import its scss files into lib's global scss, but this is not working until I installed the materialize-css to my application project. Is this the only way?
-
As I am using the scss-bundle and this will bundle all the scss files into one but there is an scss files that imports some resources like
@font-face { font-family:"Roboto Regular"; font-weight:normal; font-style:normal; src: url("../../resources/fonts/roboto-regular/Roboto-Regular.eot?") format("eot"), /* IE9 Compat Modes / url("../../resources/fonts/roboto-regular/Roboto-Regular.woff") format("woff"), / Pretty Modern Browsers / url("../../resources/fonts/roboto-regular/Roboto-Regular.ttf") format("truetype"); / Safari, Android, iOS / / url("../../resources/fonts/roboto-regular/Roboto-Regular.svg#Roboto-Regular") format("svg"); / / Legacy iOS */ }
and I copied the resources folder as well but as there is only one bundled scss file for all scss (no folder hierarchy) so ../../resources fails as there need only one ../resources, what is the proposed solution ?
@ramandeep-singh-1983 Thanks for your answer.
- But isn't
ng build --watch=truewill build the app itself rather than the library. As i mentioned, i run 2 commands in parallel, 'npm run watch' watches and rebuilds the library and 'ng serve' watches and rebuilds the application.
- And My library is dependent on materialize-css and I need to import its scss files into lib's global scss, but this is not working until I installed the materialize-css to my application project. Is this the only way? >> Looks like you were able to solve this by updating the library's ng-package.json
- As I am using the scss-bundle and this will bundle all the scss files into one but there is an scss files that imports some resources like
@font-face { font-family:"Roboto Regular"; font-weight:normal; font-style:normal; src: url("../../resources/fonts/roboto-regular/Roboto-Regular.eot?") format("eot"), /* IE9 Compat Modes / url("../../resources/fonts/roboto-regular/Roboto-Regular.woff") format("woff"), / Pretty Modern Browsers / url("../../resources/fonts/roboto-regular/Roboto-Regular.ttf") format("truetype"); / Safari, Android, iOS / / url("../../resources/fonts/roboto-regular/Roboto-Regular.svg#Roboto-Regular") format("svg"); / / Legacy iOS */ }
and I copied the resources folder as well but as there is only one bundled scss file for all scss (no folder hierarchy) so ../../resources fails as there need only one ../resources, what is the proposed solution ?
I struggled with paths too but i was able to fix the issues eventually. Do you still have this issue?
this is how i done this https://stackoverflow.com/questions/54003034/scss-compilation-issue-in-angular-lib-created-using-angular-cli
Excellent discussion here! I can already see a few workarounds which seem reasonable. Although I'd discourage global styles in libraries, if your use case require such I'd recommend you to look at
ngx-build-plus. You can customize your build using it.Closing the issue here. If you think we're missing something and it's essential for the CLI to support this out of the box (i.e., you have use cases that we have thought about), please open a new issue.
@mgechev angular material and cdk are examples of libs which use global styles. If you use cdk in your lib and need to override something how would you do it without the global styles? Or If your lib uses material and wants to provide custom theme? Also ngx-build-plus doesn't seem to have support for libraries at all at the moment.
To overcome this situation :
- Use Angular-cli 7.1 which has a new option (--deleteOutputPath=false) and use the rimraf command to enrue dist cleaning is proceeded before any other script is executed.
It seems that this options is taken away from cli in Angular CLI: 7.3.1 version. I didn't find any information about it and didn't find any replacement for this options. So I started use wait-on package to wait until package-name.d.ts file is ready until to start scss-bundle.
And for others trying to find -w options in scss-bundle you should update your package to 2.5.0-beta. It is new feature of that package.
To overcome this situation :
- Use Angular-cli 7.1 which has a new option (--deleteOutputPath=false) and use the rimraf command to enrue dist cleaning is proceeded before any other script is executed.
- If using previous Angular version : add a short delay before using cpx, scss-bundle (using npm package like @sindresorhus delay-cli)
I'm still wondering how your solution can even work with library because if I am understand correctly there is no --delete-output-path parameter with ng-packagr like normal angular application has. Check this out https://github.com/ng-packagr/ng-packagr/issues/632 So I'm stuck with your other suggestion.
EDIT: I research more and notice that they have implemented deleteDestPath in ng-packagr. I read wrong that ng-packagr issue history. It was merged and implemented, but still even I use deleteDestPath(ng-packagr parameter name) or deleteOutputPath (angular cli parameter name) both give me this error "Data path "" should NOT have additional properties(deleteDestPath || deleteOutputPath)" So it seems that it is not working with libraries only with application.
Angular CLI: 7.3.3
Node: 10.15.0
OS: win32 x64
Angular: 7.2.6
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router, service-worker
Package Version
------------------------------------------------------------
@angular-devkit/architect 0.13.1
@angular-devkit/build-angular 0.13.1
@angular-devkit/build-ng-packagr 0.13.1
@angular-devkit/build-optimizer 0.13.1
@angular-devkit/build-webpack 0.13.1
@angular-devkit/core 7.3.1
@angular-devkit/schematics 7.3.3
@angular/cdk 7.3.3
@angular/cli 7.3.3
@angular/flex-layout 7.0.0-beta.19
@angular/material 7.3.3
@ngtools/json-schema 1.1.0
@ngtools/webpack 7.3.1
@schematics/angular 7.3.3
@schematics/update 0.13.3
ng-packagr 4.7.0
rxjs 6.3.3
typescript 3.1.3
webpack 4.29.0
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.