angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

[RFC] Library Support and Boilerplate Comment Thread

Open hansl opened this issue 9 years ago • 163 comments

As a follow up for https://github.com/angular/angular-cli/issues/1692#issuecomment-304970595.

Continue the conversion here.

hansl avatar May 30 '17 18:05 hansl

TypeScript just pushed a fix for symlinked repositories containing a node_modules and not being able to resolve types as being the same: https://github.com/Microsoft/TypeScript/pull/18185

I pushed the 2x repositories that work with both angular@next and typescript@next:

  • https://github.com/intellix/angular-cli-red
  • https://github.com/intellix/angular-cli-blue

Blue CLI depends on Red CLI and uses a component from it. I just referenced angular-cli-red locally since they're not published: "angular-cli-red": "file:../angular-cli-red" so checkout next to each other like:

angular-cli-red
angular-cli-blue

intellix avatar Sep 12 '17 10:09 intellix

Potential solution until the CLI starts being able to build out component libraries thanks to @dherges's ng-packagr: https://medium.com/@ngl817/building-an-angular-4-component-library-with-the-angular-cli-and-ng-packagr-53b2ade0701e

nikolasleblanc avatar Sep 14 '17 20:09 nikolasleblanc

@nikolasleblanc This is fantastic as a stop gap. Any chance you've used a monorepo setup like lerna with this config?

jimthedev avatar Sep 21 '17 13:09 jimthedev

I've said a couple times in this thread (and I hate self-promoting, but here I go again) I've been working on a project called Angular Librarian that has worked pretty well for my team.

It has all of the scaffolding you'd expect from the CLI and bundles using the Angular Package Format. I'm trying to make it so monorepos can work out-of-the-box.

I really respect what @dherges has done with ng-packagr and it's integration with the CLI, but always want to give people options!

gonzofish avatar Sep 21 '17 14:09 gonzofish

@gonzofish Any chance you and @dherges could work together to merge those projects?

jimthedev avatar Sep 21 '17 14:09 jimthedev

That's probably the best solution, isn't it? I'd be game and I think integrating with the CLI might be a more ergonomic choice

gonzofish avatar Sep 21 '17 14:09 gonzofish

@gonzofish Seems like a solid good goal but also sounds like CLI might change to integrate with DevKit so does integrating with it in its current state mean things get pretty brittle? I can't find it right now but I think the Angular team might have indicated to @dherges already that they weren't interested in landing this in 1.x either. I don't know the answer so would love to hear what Librarian and ng-packagr currently do and what the differences are. @dherges might have to give us some insight into his goals / tradeoffs he's run into. Anyhow I don't want to be the middleman unless the two of you would like it. As someone who is hoping to use these I can see how some consensus would help in an otherwise fragmented environment when it comes to packaging Angular libraries pre-devkit.

jimthedev avatar Sep 21 '17 14:09 jimthedev

Here's a design doc for the motivation of the tool and the purposes of ng-packagr. Think of it as (entryFile: TypeScriptSource) => AngularPackageFormat. You can integrate it with any other build tools around. There's no official support for it, but the GitHub issues are also a source of documentation.

Angular CLI has library developer mode for one year now, well in the announcements. I have no "stake" in Angular CLI and cannot speak for the Angular team. I do not know what their plans are. I just speak about my highly subjective perceptions :-)

When you run several dev teams at different locations, you appreciate to have a common tool chain. Eventually, Angular CLI helps us out a lot for building apps. No one wants to maintain highly customized build scripts in more than {{pick an arbitrary number now}} source code repositories. For libraries we now have a solution, too. We can perfectly live by an opinionated tool and its several best practice configurations.

dherges avatar Sep 21 '17 15:09 dherges

I published 3 npm packages using https://github.com/robisim74/angular-library-starter as a starting point. https://github.com/orizens/ngx-infinite-scroll https://github.com/orizens/ngx-typeahead https://github.com/orizens/ng2-youtube-player

It's:

  • AOT friendly
  • supports ES5
  • Support UMD bundles
  • easy to publish to npm

orizens avatar Oct 01 '17 17:10 orizens

Nx looks like an interesting solution to this:. https://blog.nrwl.io/nrwl-nx-an-open-source-toolkit-for-enterprise-angular-applications-38698e94d65

sparty02 avatar Oct 09 '17 19:10 sparty02

Nx looks cool and all but I don't think its trying/meant to solve the library issue at all. It's really just putting everything into a monorepo so that you don't actually have to solve the library issue, right? Putting your whole company under one repo simply isn't going to work for a lot of organizations. Too many teams/people and releases to manage, and too much politics involved. This issue is more for generating standalone libraries that can be exported (and pushed to npm, for example) and imported into other apps (in other repos) - all while doing it with best practices (bundling, minification, etc), and supporting the developer experience as well.

Matmo10 avatar Oct 09 '17 23:10 Matmo10

True, Nx is a little bit of an overkill, plus almost nobody does mono-repo projects, except some huge enterprises. We just want Angular CLI to initialize a project in Library mode, that would be so nice.

avatsaev avatar Oct 10 '17 12:10 avatsaev

Putting your whole company under one repo simply isn't going to work for a lot of organizations

nobody does mono-repo projects, except some huge enterprises

@Matmo10 @avatsaev I think you miss the point. Monorepos might not be necessary to build a library. But I think they are important beneficial to how libraries will emerge as part of an app development process. I find that Nx describes pretty much the same story I isolated from my own experience and consolidated here and here.

Monorepo doesn't necessariliy mean to develop all libs of a company in one giant repo (although Google, FB, etc. are said to do so) but also to develop one application from multiple smaller packages in the same repo. App development and library development are two sides of the same coin, I think. Done right, application development is a process of incubating new and reusing existing libraries. And even small companies develop large apps (should better say applications) and need to practice reuse to be competitive. They need to keep building blocks simple and focused to grow their system without growing the mental overhead to understand it on the same scale.

So let's assume collegues from another project asking you if you have solved the logging problem for your app. I can imagine teams which say, yes we have solved it. Just copy our sources. And I can imagine teams which say, yes we have solved it. Just run npm install foo-logging (much more appealing). I can even see the same team copying things around for the Next-Important-Big-Customer project and I can imagine the same team just installing their own logging-lib from the last Important-Big-Customer project.

With Angular and Angular-CLI as of today, unfortunately, I have seen a lot copy-cats beginning to fiddling around with relative import paths extracting the logging services, ngModule and other stuff out of their CLI-generated app once someone asks for reusing this and that. They might not like a monorepo approach and then decide to put the logging parts into a separate repo, okay. The question is why haven't they done it from the beginning?

In my experience - you won't get people to think modular if your project structure encourages a monolith and with costs for modularity as high and complex as they are today. If people have to set up and checkout separate repos for each package, if they have to set up build scripts for each package even more so since building a lib for angular is such a pain, then they simply don't do it early enough. Modularity will be an afterthought. Cutting a monolith into modules is ways harder than doing it right from the start and usually even more than just copying things around (unfortunately, YAGNI always sounds pretty cool unless you find out what N really means and how it relates to Spaghetti).

So you want to develop an application from packages but you don't want to necessarily spread the codebase in as much repos as you might happen to have packages. That's where a project can benefit from a monorepo structure. If it contains a package useful to other teams, then you might decide to move that package into a separate repo at some point in its lifetime. E.g. if you want to spread maintenance efforts accross all teams using the lib (company culture!). However, the first important thing is to imagine this specific package, the second is keeping costs of realising it small and only third step is to moving it into another repo. Monorepos help with all three points. For the third step: you don't have to adjust a mass of relative paths when you always imported foo-logging by its package name rather than its location in your repo. If you have always imported your own packages like an ordinary npm package, then moving it in another repo is peace of cake with minimum impact on the incubator app!

Said, this I think monorepo support is not a must have for library support, but it does definitely assist in library emergence as part of an app development process.

about-code avatar Oct 10 '17 16:10 about-code

One does not need a monorepo at all! 💯

One could even publish npm packages to private git repositories and npm install | yarn install from git urls! Even angular does for their builds

dherges avatar Oct 10 '17 17:10 dherges

One does not need a monorepo at all! :100:

@dherges Even though writing about benefits of monorepos I can support that. Being able to make a script iterate over a package folder during build like with ng-packagr is just enough for me at the moment :smiley: (optimized version supporting scoped packages here).

about-code avatar Oct 10 '17 19:10 about-code

I don't think it's been mentioned here but according to official blog post https://blog.angular.io/the-past-present-and-future-of-the-angular-cli-13cf55e455f8

Future of the CLI The team working on the CLI has a lot of plans for making developers’ lives even easier. Here are a few of the ideas the team hopes to (but doesn’t promise to) include in future releases: [....] Library support — Today the CLI can produce UMDs optimized for the browser, and CommonJS bundles optimized for the server. What if the CLI could help you produce a bundle that could be consumed by other Angular applications? [...]

adamlubek avatar Oct 16 '17 09:10 adamlubek

We're in the process of making a simple command tool for generating libraries. You just pass the ts-file you want to be the entry point of the library:

ngmakelib src/library.entry.ts library-name

and this creates a tar.gz that you may use directly with npm install.

Currently only exporting FESM module which works for all our usages for reuse in other Angular projects.

By installing this tool in your CLI project you may export it as a library to use in other projects.

You can take a look here: https://github.com/fintechneo/ngmakelib/

petersalomonsen avatar Oct 19 '17 16:10 petersalomonsen

https://github.com/dherges/ng-packagr does that with FESM2015, FESM5, and UMD formats, it's been broken in some uses cases, but the 4.4.5 release of Angular fixed the issue (https://github.com/angular/angular/pull/19579), the ticket was closed today, I've tested and it works great now https://github.com/dherges/ng-packagr/issues/101

avatsaev avatar Oct 19 '17 19:10 avatsaev

There are a lot different aspects to Angular libraries.

  1. Rob said to distinguish binaries and libraries - let's use this terms now!
  2. Monorepos and Nx workspace do favour "libraries": everything is linked within a single source code repo, so every app can build the library from sources – also, this monorepo steam is trending… 🚂
  3. Angular Package Format is for distributing "binaries" to npm registry: the library is build from source before publishing.

I'd say you need to answer first how your workflow is (or how you like it to be)?!

When you have that ... 🎉 , then the very last step is to decide what tools to use. There are a lot of and you have to decide how much you want to get into the details of the "fav tool of week"-thing 😂

Next question of interest: in what direction does the ng CLI want to go? Do they have plans for supporting Angular Package Format? Or is schematics / code scaffolding the primary way, thus telling and assuming to use monorepo? Is it a mixed approach?

dherges avatar Oct 20 '17 06:10 dherges

@dherges I have great respect for your work and it makes me sound a bit like a smart-ass but I think you misunderstood what Rob meant when he wrote:

At Google, we use the terms "binary" and "library" for this:

  • a binary is a shippable-to-the-browser-artifact that's "self executing"
  • a library is a designed to be consumed by binaries (or other libraries)

Maybe @robwormald can jump in if I am wrong. But I am sure building the library from its sources into Angular Package Format doesn't make it a binary in the sense Rob meant.

Angular Package Format is for distributing "binaries" to npm registry: the library is build from source before publishing.

Let's stick to the APF docs and keep saying its a format for distributing packages / libraries which support the most common consumption patterns as well as the additional requirements to be consumed by Angular apps. The term binary isn't used anywhere in the APF docs. APF-compliant npm packages don't need to be self-executing bundles shippable to the browser. In many cases they will be component libraries like Material UI which aren't ever meant to execute independent of an app. The app is the binary here.

Rob said to distinguish binaries and libraries - let's use this terms now!

He said at Google they do distinguish in the way cited above. I wouldn't read it as a recommendation to adopt Google-internal terminology but just as an explanation by Rob. The term binary isn't self-evident in the JavaScript context and without explanation is misleading at best. In the end I am pretty sure we can skip any "binary" wording when we talk about library support and I advocate to do so to avoid confusion.

about-code avatar Oct 20 '17 20:10 about-code