-
Notifications
You must be signed in to change notification settings - Fork 12k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Angular (+ CLI) 6.0.0: Global styles for libraries #10869
Comments
I tried to add
https://github.com/angular/angular-cli/wiki/stories-global-styles shows, how to use global styles. This does not work when you add them into your library json configuration
The documentation feels misleading since this does work for app builds but not for libraries. |
The options are particular to the referenced builder (in this case, |
Okay - I've used ng-packgr before. Afaik there's no support for global css in ng-packagr and I had to add own build steps in order to get this running Since having a global css file seems to me like a common thing, is there any plan to collab with the ng-packagr project on this? |
Any update on this? |
@jhuenges if it helps, here's a gulp task snippet as workaround (or long-term solution, depending on what the angular team is planning 😃 ) The snippet includes:
I had to change a couple of things since we have a slightly different custom-build but it should give an idea on how you can solve this. Of course you need to install the necessary gulp-packages. |
Thanks! I will try to use this, but an official solution would be great :) |
Here is another solution using only node package. In your package.json add dependency to package
The build-mylib-styles works in two steps. First it copys all styles files found in the lib/style folder and then it uses the scss-bundle package to bundle all imports of the _style.scss file. I have other files in the lib/style folder without import so no need to bundle them. |
Hi. I wrote another solution that adapts to the
gulpfile.js:
|
This might be a good read when a official solution is not yet ready: https://medium.com/@Dor3nz/compiling-css-in-new-angular-6-libraries-26f80274d8e5 |
Thanks! this solution works well. However, the new watch mode for the library doesn't work after this. So, one needs to rebuild and serve the page manually after any code change. Any way to make the watch mode work with this? |
but while packaging angular try to remove the script section of package.json and the following message occurs: Removing scripts section in package.json as it's considered a potential security vulnerability. |
Why would you do that?! |
@ramandeep-singh-1983 i got it to work with watch mode, for everything except the scss-bundle, which i guess has to be a post-build step. i used concurrently and rimraf, in addition to the already mentioned cpx and scss-bundle here's the scripts i developed for it:
i put in the clean step because i have configured my ng-package.json with i opened an issue in scss-bundle repo to ask them about adding a -watch mode |
Would love to know when there is a general solution. For now I am just creating a |
Thanks to @hughanderson4 there is now a watch mode for scss-bundle (see version 2.5.0-beta) |
@hughanderson4 your solution is great, but I encountered a problem while using it. The To overcome this situation :
|
Yeah buddy similarly in my lib I got a ng-package.json and I had to modify like this to prevent the cleaning of the output folder:
That why I manually put in the I had problems trying to run all of it concurrently. My workflow is to start the |
Great, thanks for sharing this solution. I didn't think about directly checking ng-packagr config options. Now I understand why you were using rimraf. It makes more sense ;). I'm able to run the watchers together and everything seems to work fine. I use npm-run-all to run parallel watch tasks, but it probably works the same way concurrently works (I didn't take the time to check). |
Great job, yeah npm-run-all is about the same as concurrently, I selected concurrently since it has wild card support to run bunch of jobs at same time. ✌️ |
Thanks @hughanderson4, the watch mode is working nicely for me now with the following code:
I have added " && scss-bundle" in the "watch-scss" command as "scss-bundle" was not being called anywhere in the code you posted. However, i have 1 issue: the assets and styles are copied inside the my-lib directory after running "npm run watchAll". Now, how do i use the dist/my-lib\styles/_styles.scss in my application? I have made the application's styles.scss empty as i want all library specific global styles to go under my-lib/styles/_style.scss |
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
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
Ater that, in your app in any .scss file you should be able to write |
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-app app js files assets 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 Then I have to config each of my client apps to do this glob:
This goes in the angular.json under 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 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 :) 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
In your main application configure the tsconfig tsconfig.json
my-component.scss
When the cli notice the With this approach you can use paths everywhere to access different places in your cli app. |
Hi thanks for your build scripts,
but this fails by saying
can you please help? |
The following set works for me:
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.
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? |
@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 |
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. |
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 ng-packagr/ng-packagr#632 EDIT:
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Versions
Repro steps
ng build your-library
./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)
The text was updated successfully, but these errors were encountered: