feat: add support for watch when building a library#11358
feat: add support for watch when building a library#11358hansl merged 4 commits intoangular:masterfrom alan-agius4:ng-packagr_watch
watch when building a library#11358Conversation
| "rxjs": "^6.0.0" | ||
| }, | ||
| "peerDependencies": { | ||
| "ng-packagr": "^2.2.0 || ^3.0.0 || ^4.0.0-rc.0" |
There was a problem hiding this comment.
I don't think we can drop support for 2.x and 3.x here, since that would be a breaking change for us. WDYT of having the watch option verify for a compatible version of ng-packagr instead?
We've had a couple of options that work like that in the past. Current examples include
angular-cli/packages/angular/cli/commands/serve.ts
Lines 27 to 28 in 6449a75
There was a problem hiding this comment.
I like the whole idea!
| "flatModuleId": "AUTOGENERATED", | ||
| "flatModuleOutFile": "AUTOGENERATED" | ||
| "flatModuleOutFile": "AUTOGENERATED", | ||
| "enableResourceInlining": true |
There was a problem hiding this comment.
Is this required for the watch mode? If so we might need to warn users of older versions about it.
There was a problem hiding this comment.
Yes, this is required for V4. As we removed a lot of internal logic to inline resources.
There was a problem hiding this comment.
Ok, then I think we'll need to check this as well when asserting compatibility.
There was a problem hiding this comment.
For v3, this flag might cause some unwanted behaviour I think.
There was a problem hiding this comment.
Yeah makes sense.
Maybe we can fully remove these values and let ng-packagr take care of them? I think we ended up adding them manually to get around a tsconfig merging bug in ng-packagr. It might not be present anymore.
If they are still needed then we need to do the health check both ways then: that they are not present when using ng-packagr v3, and that they are when using v4.
There was a problem hiding this comment.
Actually I gave this a bit more thought. And I think. IN V3, this might not have any sideEffects as there is a previous step to transforms the components.
I have updated the v3 branch to include this flag, and from the Unit Tests all seem fine
ng-packagr/ng-packagr#989
| { | ||
| "$schema": "<%= relativePathToWorkspaceRoot %>/node_modules/ng-packagr/ng-package.schema.json", | ||
| "dest": "<%= relativePathToWorkspaceRoot %>/<%= distRoot %>", | ||
| "deleteDestPath": false, |
There was a problem hiding this comment.
I'd like to understand the motivation to remove this preset better. Does this option not play well with watch mode?
I originally added it to enable users to re-run build in dev mode without breaking watched files. But now with watch mode existing, this usage might be different.
I guess ideally we'd want a single ng-package.json file at all time, with the destination path being automatically deleted in single-run mode and not in watch mode.
But that does beg the question: if deleteDestPath does not interact well with watch mode, maybe it should be ignored in ng-packagr directly.
There was a problem hiding this comment.
Regardless of the final semantics of watch mode, I do agree on doing away with the dev/prod configs if their only difference is watch mode support.
There was a problem hiding this comment.
It’s not the it doesn’t play well. It’s that with watch mode it”s kinda redundant.
The reason behing the original implementation of deleteDestPath was for users to create their own watch, and due to the fact that in webpack 3 when a file got deleted from disk webpack would crash with a fatal error. Now this is super seeded with the watch mode.
There was a problem hiding this comment.
It's also important to remove the dist dir overall though. If you don't, artifacts that aren't there in subsequent builds will still be present because of previous builds.
With apps this is especially relevant because of assets, but less relevant for libraries. Still, the correct thing to do on a fresh, non-watch build is to clean (delete) the outpath before re-generating artifacts.
That was why we had it on on the production builds, and not the development ones. The production builds were the 'correct' ones, while the dev builds made that concession for a makeshift watch mode.
There was a problem hiding this comment.
By default ng-packagr deletes the dist directory when when triggering the build/watch
There was a problem hiding this comment.
Ah I wasn't aware of that! Then removing the option makes perfect sense.
|
@filipesilva I updated the PR as suggested. Do you have any sample on how should I test the |
|
AppVeyor is failing due to the recurring issue |
|
A good way to test the watch mode is to both check the build events are being emitted properly, and that the build artifacts contain what you expect. A good example can be found here The appveyor error (below) you are seeing is unrelated to your PR though. It's something I have been unable to catch properly on appveyor builds. Will look at in again today. BTW regarding timing, this PR cannot go in for 6.1 because we are already in RC. But it can go in for 6.2. |
|
Thanks @filipesilva, that was really helpful. I have added the unit tests. Ps: I love how the Virtual FileSystem thingy works in the CLI. Big kudos guys :) |
| @@ -180,7 +180,7 @@ | |||
| "@types/estree": { | |||
There was a problem hiding this comment.
Not sure why so many changes, I am using npm --version 6.1.0
There was a problem hiding this comment.
Looks like it was due to a private npm repo
|
@filipesilva I am now having the rmdir error on my newly added tests aswell. :/ |
|
Yeah we are trying to do a FS abstraction with the Hosts but still have a couple of parts missing for true cross platform support. Glad you like it! I think the current rmdir error you have is legit. To make watch mode work with tooling, it is necessary that the watcher can exit gracefully. For webpack, that happens in This is actually not as big a problem in linux/osx as it is on windows. In linux/osx, if you don't exit the watchers gracefully, you get hanging process. In our test runner that isn't a problem because we do process.exit at the end. We do this because we have found some situations when processes still hang in webpack and in karma. In windows however, you get errors when removing files because the watcher is keeping a file lock. So I think you need a API to tell ng-packagr to exit gracefully in order to use it with tooling (such as the ng-packagr builder). |
|
Ah year, the teardown logic makes perfect sense as I am doing the subscription internally and I was never unsubscribing. It was odd though because I didn't have the same errors when running the tests locally on a Windows machine. Internally ng-packagr, will close the watch on unsubscribe, as it has it's own teardown logic https://github.com/dherges/ng-packagr/blob/master/src/lib/file/file-watcher.ts#L36 Ps: teardown logic added. |
filipesilva
left a comment
There was a problem hiding this comment.
LGTM
Excited to get this in for the 6.2 beta, I feel a lot of users are going to benefit from the work you've done in ng-packagr watch mode!
| try { | ||
| ngPackagrJsonPath = resolveProjectModule(projectRoot, 'ng-packagr/package.json'); | ||
| } catch { | ||
| // @angular/service-worker is not installed |
There was a problem hiding this comment.
Nit: comment is lying.
Not really important to get this merged but should be changed if a rebase is needed.
There was a problem hiding this comment.
I will rebase and do it :)
|
Eventually https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/create-library.md should be updated with the watch mode instead of directing users to rebuild manually. |
|
@filipesilva docs have been updated, would you mind review it again please? Thanks Ps: I am really excited about this feature as well. From the little feedback I go from users using ng-packagr directly it improved their experience quite a bit. 😄 |
|
Doc changes look good too, great work! We'll be looking at getting this merged after 6.1 final, which is when we will release 6.2.0-beta.0. |
|
Correct me if I'm wrong, but this feature will make available for monorepos with multiple libraries to live-reload the main application which uses the libraries? Currently I have a working setup with multiple libraries and a consuming app with lerna, but the developer experience is not quite good because every time someone makes a change in a library it's needed to be rebuilt. Is this feature going to solve this problem? |
|
Hi, while I don’t have full picture of your architecture with lerna. However this should work, by spawning 2 process one that watches for the changes in the library and the other one that is watching the app. So both library and app will do a partial re-build. |
|
Thanks for the quick answer @alan-agius4, I'm using Angular CLI for both building the libraries and the main application. I guess I will setup a lerna command to execute If everything goes well I guess it should work correctly, because right now if I have a running main app in a process and I trigger a manual library build, the main app partially re-builds as expected. |
|
@pterblgh You don't need lerna, I have setup the following scripts https://github.com/ngx-api-utils/ngx-api-utils/blob/master/package.json#L13 and it works quite well. For library / package rebuild on change I am using https://github.com/ngx-api-utils/ngx-api-utils/blob/master/package.json#L24 so you can also have |
| obs.complete(); | ||
| }) | ||
| .catch((e) => obs.error(e)); | ||
| if (options.watch) { |
There was a problem hiding this comment.
Since ng-packagr build() uses buildAsObservable() internally why not remove the if and the new Observable statement and do return ngPkgProject..withOptions({ watch: options.watch })buildAsObservable().pipe(map(() => {return { success: true };}))
There was a problem hiding this comment.
The reason being is that angular cli still needs to support ng-package v2 and v3. And this method is only available in ng-packagr v4
There was a problem hiding this comment.
Never mind, I see, for backwards compatibility with ng-packagr 3.x when not using watch, right?
|
Will eventually rebase when it's ready to get merged. Also, I created another PR to bump ng-packagr to v4 final |
|
@alan-agius4 should be ok to rebase now, #11622 is in as well. |
…brary `ng-packagr` version `4.0.0-rc.3`, lands the incremental builds feature. More info: https://github.com/dherges/ng-packagr/blob/master/CHANGELOG.md#400-rc2-2018-06-23 `enableResourceInlining` needs to be enabled for libraries that contain components Closes: #11100
…building a library `ng-packagr` version `4.0.0-rc.3`, lands the incremental builds feature. More info: https://github.com/dherges/ng-packagr/blob/master/CHANGELOG.md#400-rc2-2018-06-23 `enableResourceInlining` needs to be enabled for libraries that contain components Closes: #10643
|
thanks @filipesilva, I just rebased. |
|
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. |
add support for
watchwhen building a libraryng-packagrversion4.0.0-rc.2, lands the incremental builds feature.More info: https://github.com/dherges/ng-packagr/blob/master/CHANGELOG.md#400-rc2-2018-06-23
BREAKING CHANGE:
ng-packagrversion4.0.0-rc.2or greater is required.enableResourceInliningneeds to be enabled for libraries that contain componentsPs: most likely this should go in V7, but still felt like doing it, so to be ready.
Closes: #11100