[RFC] Library Support and Boilerplate Comment Thread
As a follow up for https://github.com/angular/angular-cli/issues/1692#issuecomment-304970595.
Continue the conversion here.
For folks landing on this issue, it might help to point to the documentation PR even if it's not live.
By that I mean it would be helpful for the folks landing on this issue if the first comment was updated to summarize the efforts being taken.
@deebloo I wouldn't push for another command or any other tool that in the reality should live inside ng.
Angular team,
Look your Polymer partners, they push for bower because of the limitation of npm and everyone hate it but at the same time just dont care because it works at the end of the day and they show you all over the place how to do it. No by reading 3 pages long of some specification or something like that, their strategy includes short videos and a lot of interaction with the community, documentation and so on, but also the tooling backup.
Some take away from there, is that you do not have to adapt to everything in the Javascript community but if you don't teach us how to be productive, care about us, how can we use Angular. I am fine with any architecture and ecosystem switch if I will be back up for the creators and I will find the answers without reading a technical paper (no because I don't know, which I am excluding the people that actually don't, but because that detail on my stack should be far away from me as possible, even Typescripts configs).
And btw, probably the information is there but we failed on put it out.
@deebloo that's the intention behind ng-packagr. There's also a demo showing ng and ng-packagr side by side
@dherges is this what Angular CLI uses? I am trying to push such of tool in the Angular core team. They should be doing that work, IMO
Hi guys,
Following a massive work i'v done to integrate a solid build process I recently converted it to a general purpose repository due to demand in our community.
After some exploration I found that these are the key features a library template should provide:
-
Scoping (
npm install @my-scope/my-lib) -
Multiple library repository (
@my-scope/my-lib,@my-scope/her-lib, ... all in the same repo) -
Package extensions. Ship packages with an opt-in internal package (e.g:
@angular/core/testing), extension are built separately. -
Build hooks, tap in to alter package.json, tsconfig, rollup config etc... (globally and per package)
-
Webpack driven
-
Outputs:
- Flatten ES Modules (es5, es2015)
- Rollup UMD bundle + minified version + gzipped version
- Source maps, complete and accurate.
-
Flat Angular compiler module via
metadata.jsonfile. -
Resource inlining (html, css, scss) for source code + metadata.json
-
Resource inlining driven by webpack, no fuzzy tasks, use your favourite webpack loaders.
Some bonus features that are nice:
- Dedicated demo app, write as if your library is a module (no relative imports)
- Simulation mode - Run the demo app on the prod compiled library as if its in
node_modules, in dev or prod mode.
The simulation is actually a big deal, it enables running the demo app against a compiled, published version of the library where the demo can be JIT and then AOT. This is a good place to run E2E tests and verify that the publish (to be) version is legit.
The E2E unit tests on the demo app are more aimed towards the library ofcourse, not the demo app.
To make this work there are some changes needed in @ngtools/webpack.
This mainly concerns providing hooks in the AOT plugin for integration.
I'v implemented this in ngc-webpack which is a light compiler-cli wrapper that allows these hooks. (ngc-webpack provides the AOT integration in AngularClass/angular-starter)
The plugin will notify when the AOT compiler loads a resource, this allows saving the resource for future inlining. (lib is built with skipTemplateCodegen false)
**The huge benefit here is that we don't need gulp or any other process to manage our resources. They are handled natively through the webpack loader chain we configured, this is a seamless process that ensures consistency. (think SCSS, postCSS, etc)
@hansl I know that @jskrzypek started talking with tou about that, based on the work I'v done in ngc-webpack, but he had to drop it due to other constraints.
All of the features I'v listed above (+ the demo and simulation) are part of the library starter repository I mentioned previously.
You can check it out in this repo: https://github.com/shlomiassaf/angular-library-starter
It is working great and there are examples there where you can see the output, its the same output as angular packages with support for UMD, FESM (es2015+5), tree shaking etc..
One good example to look at is a scope, multi-lib project with internal extensions (e.g. @angular/core/testing) which I call extensions / plugins.
Scope: @corp
There are 3 libraries and 2 extensions:
-
@corp/core- extension:
@corp/core/testing - extension:
@corp/core/plugins/core-plus
- extension:
-
@corp/common -
@corp/rare
This is a good example since it demonstrates the capabilities for building a complex structure with inter-dependencies and how they are handled when bundling (rollup external and globals)
Frankly, I'm not a fan of starters if we can integrate it into the CLI, I would love to help getting it into the CLI.
cc @shairez
Edit: Theoretically speaking, if we achieve the above the angular project, material, etc.. can all use the cli, which is remotely similar to TypeScript written in TypeScript :)
@shlomiassaf I really like the idea of build hooks and the in-library scoping of packages.
Currently on Angular Librarian we have a lot of the features you mentioned plus some light scaffolding capabilities (initializing project, components, directives, services) and it'd be great to integrate some of those features in.
I know it's not the official Angular CLI, but I'm just trying to fill the gap in the meantime as best I can.
So I gave https://github.com/filipesilva/angular-quickstart-lib a shot. And after having spent a day playing with it, my conclusion is that it has a ways yet to go. It's a bit un-DRY, and I dislike the reliance on systemJs and Rollup. While Rollup might produce smaller bundles, I'm drowning in complicated techs that I don't have time to get fully acquainted with; techs that produce obscure errors, from time to time.
So my suggestion for people coming here looking for a module bundling solution, is wait a while longer.
Use npm link for now.
My 2c.
@larssn My guess is that these errors mostly concern the bundling, more specifically defining externals and globals.
IMHO, this will not change in a CLI implementation as well, you will have to provide proper configuration to your library build process.
While there is a high probability that external entries can be discovered automatically, globals are less likely, you'll have to provide them if you want UMD bundle support.
Anyway, building a library is considered advanced and some knowledge, I believe, is expected.
@shlomiassaf Yes, the errors were mostly related to that. I'd fix one, and a new one would pop up. The challenge I laid on myself was to have import support for json. Mostly got it working, except for tests, which is where I gave up for now. Might have been able to solve it; but a part of it was to review the work involved in quickly creating reusable libraries; a point on which I'm not convinced on.
@larssn That happens because you have to declare 3rd party imports. For each unique import token you need an external declaration, that's for FESM. For UMD you also need to declare the global name.
So, if you add import { Observable } from 'rxjs/Observable'; you need to make sure you declare
external: rxjs/Observable
globals: Rx
external is straight-forward, this is why it can be automated.
globals is custom, the way the author decided a UMD bundle will register in the global (e.g. window) object.
Other examples:
rxjs/operator/combineLatest - Rx.Observable.prototype
@angular/common - ng.common
Not so difficult to maintain, specially since rollup will notify whats missing.
For 9-out-of-10 libraries in the Angular world, these configurations can and should be provided w/ well-known values out-of-the-box.
A rollup config for rxjs and the @angular/* packages doesn't need to be copy-pasted from one seed to another seed to yet another starter... it can be simple, when we try to improve one step after another.
Hi folks, I wrote up a proposal of how we can share Angular Modules right now between Angular CLI Apps. I call it NG Remix and it has two principles.
- Principle 1 - All Angular Modules are uncompiled
- Principle 2 - Angular Modules share NPM dependencies
In my proposal I discuss how we can apply those principles, the pros and cons, and also include a specification for a ng-remix CLI that could help take out the manual labor. I'm interested in feedback from folks. Certainly those two principles won't work for everyone, but if it works for let's say 50% of the thousands of Angular developers I'd say it could spur a lot of sharing.
See the proposal here -> https://gist.github.com/rjsteinert/82b3000037a5672f43a4d15313ac613f
@robwormald I'd be interested in your thoughts on comparing the seed library approach to the NG Remix approach I proposed above.
@rjsteinert feedback:
-
What does "uncompiled" mean in this context? Shipping Typescript to the outside world is a very, very difficult thing to manage properly - anybody who wants to consume your library has to be able to recreate the entire typesystem / environment on their end. We explored this early on but it appears to be a non-starter.
-
Why does bower come into this? Even the Bower authors are saying it's dead, and adding yet-another-package-manager seems like a bad idea.
I guess i'm not quite clear on what your approach is here?
@robwormald
anybody who wants to consume your library has to be able to recreate the entire typesystem / environment on their end
That's what ng-cli is good at I believe :-). Being an angular-cli created app is a hard requirement and I could make that more clear in that proposal. That also now makes me realize that these modules will eventually need to specify the version of ng-cli they are based on. Note, to someone like me angular-cli is the Angular Framework. If a framework is a bunch of things strapped together, angular-cli fits that description better than @angular/angular.
Why does bower come into this?
Lol, ya, surprising. It was recommended by Rob Dodson presenting at Google I/O for use with Angular in the context of Web Components right at this point in the talk. The reason for this is "flat installing". Why would we for example want two different versions of the same module that define the exact same route? Bower helps you sort that out when two modules define the same dependency but at different versions.
@robwormald Updates to your questions now added to the proposal. Thanks for taking a look!
The last few discussions here again stress the point in having small, focused tools do to the job. While seeds and starter packages are a nice thing, the questions is: how do they work together with ng CLI or custom webpack builds, or bazel, or how-does-YOUR-set-up-look?
Again, I think, it should be public_api.ts in and a dist folder in Angular Package Format out. As a good start, build and test should be the two commands to support!
# Build the library
$ <angular-library-build-tool> build -p <library-conf>.json
# Run unit tests from *.spec.ts files
$ <angular-library-build-tool> test -p <library-conf>.json
And it's definetly on the agenda for dherges/ng-packagr#5 next!
Well, after reading through #1692 and this issue I'm totally puzzled what approach to use to currently create a library. To get the least problems as soon as the angular / angular-ci team comes up with their own solution. The most extensive approach seems to be the one from @shlomiassaf at https://github.com/angular/angular-cli/issues/6510#issuecomment-309702987. But I don't really need the "multiple libs in one repo" approach in my case (although I understand its existence).
So what to choose: https://github.com/filipesilva/angular-quickstart-lib or https://github.com/shlomiassaf/angular-library-starter or https://github.com/jvandemo/generator-angular2-library or check out how they did it: https://github.com/ng-bootstrap/ng-bootstrap
And if you just try to find a solution on the internet (like most people will do) tutorials like http://www.dzurico.com/how-to-create-an-angular-library/ or https://github.com/tpadjen/tpadjen.github.io/blob/master/_drafts/publishing-an-angular-2-typescript-component-to-npm.md pop up.
It gets even more complicated when you have resources (scss and images) or want to build a library that itself depends on a library/framework like bootstrap or zurb foundation. I fully understand @oliverjanik in https://github.com/angular/angular-cli/issues/6510#issuecomment-305050324 and there should be some official response what to do to fill the gap.
@Flixt See this talk from ng-conf for the most basic setup. Example from the talk is also available on GitHub, so you can take a look. It is not fancy (doesn't have many things you would like to have), but it works and let's you get started quickly.