Rebuilding or serving library project
Feature Request
Some interface that would rebuild library project that is generated with ng g library niceLib.
Of Course the best option would be ng serve niceLib or include it to the application serve. Or at least another build that allow to add --watch parameter like it does for for application. Here what I mean:
- for application we are allowed to run
ng build --watch - for library we have
ng build niceLib - but we can't use
ng build niceLib --watch, as it throwsUnknown option: '--watch'
The new angular.json specifies a 'defaultProject' of which you generate and then are able to use the generate library command which creates the subsequent projects/ and updates the angular.json.
What I'm doing is using my 'defaultProject' as the demo app and then import { ProjectModule} from 'projects/name/src/public_api'.
When serving the app, any file changes inside 'defaultProject' or libraries projects/ reloads the development server.
Heya, this feature request is something we are aware of and talk about in https://github.com/angular/angular-cli/wiki/stories-create-library#why-do-i-need-to-build-the-library-everytime-i-make-changes.
In the future we want to add watch support to building libraries so it is faster to see changes.
We are also planning to add internal dependency support to Angular CLI. This means that Angular CLI would know your app depends on your library, and automatically rebuilds the library when the app needs it.
As a workaround you can use nodemon to rebuild the libraries:
npx nodemon -w ./projects/ui -e ts --exec 'ng build ui'
It's on the slow side but does get the job done. One downside is that you need to run a separate process for each of the libs you want to rebuild.
try File Watchers if you use WebStorm.
The proposal from @Tyler-V is by far the only acceptable at this moment. No developer can take the productivity hit that manually rebuilding the library causes. Even with an automated watcher the build time increases substantially. Until there is a fix I, differently from what the ng team proposes, will err on the side of productivity instead of caution.
@giggio,
@filipesilva and @alan-agius4 worked on some changes this morning and I'm currently able to serve my library from my demo project as suggested and build all in --aot!
You can view the changes here, specifically the PR by filipesilva https://github.com/Tyler-V/ngx-easy
I believe changes were committed and merged on ng-packagr to support this.
https://github.com/dherges/ng-packagr/pull/862
BTW, to anyone interested, it is easier to, instead of using import { ProjectModule } from 'projects/lib/src/public_api' to simply change tsconfig.json to "lib": ["projects/lib/src/public_api"].
When there is an acceptable fix simply change this one line back to "lib": ["dist/lib"].
@Tyler-V That is awesome, but with what dependency and tools versions?
@giggio
I'm using all @latest packages with the proper scoped lib configurations as suggested by @filipesilva
You can use my project as a reference on how I am serving my demo to develop the libraries with refresh.
@giggio I also use that way :cat:. With projects do have UI. It's really paintful to rebuild library and re-run everytime we do have a change :cry:
@hansl how is this going to work and in what version of the CLI should we expect it? Is it 6.1.0 as per the milestone?
@giggio, you can read the documentation here: https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/create-library.md#why-do-i-need-to-build-the-library-everytime-i-make-changes
In a nutshell you'd need to run
ng build my-lib --watch
This will do a partial recompilation of the library, if you want to rebuild also the app when the library changes, you'd need to spawn 2 processed. One that watches the library and another that watches the application.
As far as the release, this won't be under 6.1.x, but should be under 6.2.x
I've been using the ng build my-lib --watch for some time now but I 've been unhappy with the rebuild time.
Today I changed the default path mapping in main project's tsconfig to point to lib's public_api:
"paths": {
"users": [
"projects/users/src/public_api"
],
"shared": [
"projects/shared/src/public_api"
]
}
now the libraries are built by ng serve command, changes propagates super fast and also the Chrome devtools are able to debug raw typescript files.
Sounds too good to be true but really works.
@alan-agius4 we have a repo with 6 angular projects inside. Are there already plans on solving A depends on B while watching all projects?
If I watch A but B wasn't compiled yet, A fails. Do I have to manually know the dependencies of each library and start watch mode one after another?
Something like ng watch --all-projects would be super cool in my naive thinking process 😄
@filipesilva how would you handle project with multiple library dependencies? For example project A depends on lib-B and lib-C and lib-C depends on lib-D, if changes are made to lib-D, will building project A only compile lib-C and lib-D?
Heya, this feature request is something we are aware of and talk about in https://github.com/angular/angular-cli/wiki/stories-create-library#why-do-i-need-to-build-the-library-everytime-i-make-changes.
@playground the CLI at the moment does not rebuild project dependencies at all. You have to manually build those libraries in separate processes, and you probably want to keep them in watch mode so the libraries are rebuilt.
On my machine rebuilding the library in a separate process triggers the rebuild of the main app too early and the resulting library code in the app is always one change behind. The solution by @GeorgeKnap works, though.
@GeorgeKnap 's suggestion works!
@GeorgeKnap you're a lifesaver! Thanks 🎉
@GeorgeKnap
Wow...so that really does work eerily well...do you know what the downsides are of using the projects path instead of dist? Surely there must be some reasons NOT to do that or else angular would have it be the default / recommended path?