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

It's my understanding of the terms "binary" and "library" (as mentioned by Rob) in the context of a mono-repo (as explained by Victor, and as example used in Nrwl nx workspace).

If I am mistaken, maybe Rob or Victor can help out. But it pretty much sounds like the above because a FESM bundle is self-executing in the browser. You cannot see any output, still you can copy&paste the JavaScript to the browser's console and it will execute the instructions. Also, UMD bundles and IIFEs are self-executing in that perspective.

dherges avatar Oct 24 '17 12:10 dherges

This issue was created on May 31 2017, and we are still here without any official working solution :(

achieverprince avatar Oct 27 '17 03:10 achieverprince

@robwormald Are you referring to a super power of packaging Angular Components up as Web Components? https://twitter.com/robwormald/status/916748096511500288

If so, perhaps that's a major win for this issue?

rjcorwin avatar Oct 30 '17 18:10 rjcorwin

I honestly believe that the absence of an easy and reliable solution to make Angular Libraries will drive developers away from the framework.

Right now there's a gazzilion different solutions, all of them shit the bed at some different corner cases, and Angular CLI still can't offer a reliable library mode. I've been Angular dev from the first Beta and this is what frustrates other devs and me the most.

Making a tool to easily build angular libs should be NUMBER 1 PRIORITY right now, not Angular 5, not Angular Universal, not RxJS 5.5/6 compatibility, not PWA, but Angular component libraries.

avatsaev avatar Oct 31 '17 11:10 avatsaev

Nx workspaces are quite brilliant. I've got an example that uses multiple apps with lazy-loading and core modules etc: https://github.com/intellix/nx-cli My last problem now is that compiler-cli doesn't like that a Component can be declared in 2+ modules which can happen if you need to replace a component by importing only the components you want.

intellix avatar Oct 31 '17 14:10 intellix

@avatsaev That seems unnecessarily harsh.

There are tons of Angular libraries that already exist, created by developers that had to work hard to figure out how to create component libraries the hard way. Thanks to their efforts, there are tools that exist to try to make developing these libraries easier. If these tools are "sh*tting the bed", it's because they need help. So identify the problem, submit a PR, and fix it.

When the CLI's ready to officially support libraries, we can all shut this down and move on, but until then, let's suck it up and help each other out.

nikolasleblanc avatar Oct 31 '17 15:10 nikolasleblanc

I know I'm always up for a PR on Angular Librarian and I doubt any of the other solutions are adverse to positive contributions to their projects as well

Thanks for saying that @nikolasleblanc

gonzofish avatar Oct 31 '17 16:10 gonzofish

Looking at @filipesilva 's response for #8284, I've realized that Angular CLI doesn't work with libraries in TypeScript out of the box.

@filipesilva, I would like to thank you for explaining the issue and providing the workaround. In addition, I would like to explain my situation in more details.

Our team has faced the same issue as @rolaveric . We are building a product suite, which is made up of smaller applications for different domains. Since all apps follow the same design guidelines, we have a library with reusable shared components.

Actually, we do not have the team dedicated to library development. It is used just as a container for reusable code that can be referenced from applications. So, our library is not a standalone project and makes sense only within apps.

Our team considers TypeScript as a first-citizen programming language. It means that all development is carried out in TypeScript, and all repositories use the same TypeScript version and configuration. Taking this into account, we made a decision to ship our shared library just in TypeScript to avoid extra complexity.

I understand that we are doing this at our own risk and violate the Angular package format. However, since Angular CLI does not provide a solution at this point for libraries, this approach helps to avoid extra complexity of configuring and maintaining a custom build process for libraries. Moreover, this allows to dramatically simplify the application development process since we are able just to reference and test the library directly from the app, getting all benefits of fantastic Angular CLI features (e.g. live reloading) out of the box.

I admit that this can sound a bit hacky, but, from the other, side it helps the team to be focused on product development, but not the Angular build process.

Currently, we consider using the Nrwl Extensions for Angular, which looks really promising.

Does someone face the same issues? What approach would you recommend for such case?

  • Use a custom build process (like ng-packagr)
  • Use a mono repository with the Nrwl Extensions for Angular
  • Ship TypeScript library and wait the official solution from the Angular team
  • Something else

alexeykostevich avatar Nov 10 '17 15:11 alexeykostevich

@alexeikostevich If you're in a closed company setting and might even have your own private npm registry then I'd say

  • Ship TypeScript library and wait the official solution from the Angular team

is just good. We've been doing this for the last year and it causes the least trouble for us at the moment. Not publishing TS to the public npm registry is all about web standards (ECMAScript is a standard, TypeScript not), interoperability and ease-of-use (not everyone wants to set up a transpiler). If TypeScript is a defacto standard inside your company then nothing prevents you from publishing TypeScript packages internally. if it works for you just do it.

Nrwl/nx describes an important story (monorepos) in terms of angular app development in general and a story related to library development but not necessarily the solution. E.g. what I don't like about Nrwl/nx is that libs are scoped packages but the scope name is derived from the name of the workspace. So you can't develop packages with different package scopes in the same workspace. Since you say your libraries only make sense within your app a project-specific npm-package scope might be less of an issue to you. If you have a policy, though, that all packages must share a common company-wide package-scope then I guess with multiple workspaces all of these would have to be named identical or you really can have only a single workspace and repo where you develop all of your company's libraries.

Apart from that installing Nrwl/nx via npm didn't work when I tried it. I had to use their bash script install process. For my own purposes the missing flexibility in case of package scopes is a limitation and I posted an issue on this edit: but didn't get an answer so far and the situation is likely to be addressed in some future versions.

What approach would you recommend for such case?

I am afraid its not the answer you look for but in the end I'd honestly recommend to not wait for recommendations but just take an hour or half a day yourself to try Nrwl/nx and ng-packagr. I am sure you'll quickly find out yourself whether they suite your needs or not.

about-code avatar Nov 11 '17 19:11 about-code

Thank you for the detailed response @about-code !

We really publish our TypeScript library to the private company NPM since the package is not compliant with the web standards, but it helps us to keep going with product development and stay productive.

As you suggested, we will definitely review currently available alternatives in the nearest future. It seems that we are walking on the thin ice, shipping the library in TypeScript, and this can prevent us from updating to the Angular/Angular CLI in the future.

I wanted to raise this problem since this use case should be pretty typical for enterprise development.

alexeykostevich avatar Nov 13 '17 12:11 alexeykostevich

Just wondering would it be easier to build something that could produce a library for only cli-based apps?

neo avatar Nov 13 '17 19:11 neo

@neo Solutions for building libraries for consuming in CLI already exists - as mentioned above:

https://github.com/dherges/ng-packagr ( implements full Angular Package Format ) https://github.com/fintechneo/ngmakelib (minimum solution, not full Angular Package Format, but can be consumed in at least other CLI apps, ngc/rollup and SystemJS, I'm the author of this )

petersalomonsen avatar Nov 13 '17 20:11 petersalomonsen

@neo although its build process doesn't directly leverage the CLI, my library, Angular Librarian, compiles to Angular Package Format

gonzofish avatar Nov 14 '17 02:11 gonzofish

Hello to everyone. I am really inspired by such tool like ng-packagr, mentioned above. I've built simple library example, which is based on basic angular-cli app and ng-packagr. It has preview app inside, supports demo/developer mode and can be easily imported into your running angular5 apps. I've also added compodoc and live-rebuild mode to make it a bit more cool.

Check it out here: https://github.com/Tamango92/yo-lib Read the docs (sorry in advance, I am not good in writing nice docs), feel free to rename lib in local package.json file and replace all 'yo-' prefixes with your own.

ar-arkhipov avatar Nov 14 '17 16:11 ar-arkhipov

@alexeikostevich

It seems that we are walking on the thin ice, shipping the library in TypeScript, and this can prevent us from updating to the Angular/Angular CLI in the future.

I am less concerned about this, actually. TS is pretty stable in terms of backwards compatibility. The only thing you need to make sure is that your libraries don't declare TypeScript as a hard (dev-) dependency but as a peer dependency with a version range beginning with the minimal TS version the lib expects (based on the language features it uses) up to the next major version. If Angular requires a newer version of TypeScript then consumers of your lib can choose to install a newer or the newest TS version required by CLI and continue to use and compile your lib.

about-code avatar Nov 18 '17 13:11 about-code

@about-code does angular/cli works ok with "Typescript libs"? If I understood correctly, you work with private Typescript packages, do you mind to share how to set them up? thanks for your time!

matheo avatar Nov 22 '17 07:11 matheo

@matheo I am only using @ngtools/webpack regularly so can't speak about the CLI. But TS libs in the sense I understand them are just plain old NPM packages with TS sources rather than JS sources. They're published to an internal NPM registry untranspiled and when used get installed into node_modules of the dependant. They should have an index.ts at the same level of the package.json. TS implements the Node resolution algorithm and can find the index-file when it is imported via import { FooClass } from "package-name". The index file should (re-)export the public API of the lib - and only the public API. Try to avoid re-exports like export * from "...". TS libs when used in a TS app get transpiled with the app they're used in.

Once again I emphasize that TS libs should live in private registries and packages only. The NPM and node ecosystem shouldn't be polluted with TS libs as they aren't JavaScript and non-standard. Also note what @alexeikostevich wrote and the issue he linked. Some Angular-CLI contributors are heavily against TS-libs (see https://github.com/angular/angular-cli/issues/8284#issuecomment-341417325) so don't take my opinion for granted.

about-code avatar Nov 23 '17 19:11 about-code

@about-code should we build a registry together 😅 that's what I thought it could be too

neo avatar Nov 23 '17 21:11 neo

@about-code I figured out a way to compile my library with ng-packagr but not setting up the CLI project on the root of the repo, but inside a subfolder. Then I compile the lib to /dist and reference it from the root package.json to be consumed by my other projects. I will share more about this setup after get working all my stuff. Thanks to all for the great work! :thanksgiving

matheo avatar Nov 25 '17 11:11 matheo

Following my last issue comment...

I've done a lot of work on this and after analyzing my work I think we can split the problem into 3 parts, from which only 1 (number 3) is an issue related to angular:

  1. Project management (file structure and setup)
  2. Packaging / Bundling
  3. Compiling

I'll write briefly on (1) and (2), (3) is the thing that requires a solution right away.

Notice that (1) and (2) are not unique to angular, any framework or library or vanilla project will have to address these issues.

First some key points:

  • @ngtools/webpack is not related to this process, it bundles apps. For library compilation, we need to work with the @angular/compiler-cli.
  • Webpack can be used to compile resources only (not drive the process nor bundle). This will yield similar results for dev environment and enables usage of the loader configuration we already have!!!

Project management

Angular CLI does this already but for applications, not libraries. A library needs a demo app for integration tests and easy development... This means that any project is a mono-repo... This is not trivial, proper setup requires advanced configuration of typescript and webpack.

For now, Nx workspaces provide a proper solution.

Packaging / Bundling

This is the process of taking the compiled output (JS) and packaging it into a format/formats. Angular has a spec for that, creating bundles in FESM ES2015, FESM ES5 and UMD along with d.ts files and metadata.json files. This is quite technical and already done properly in several projects (material2, ng-packager, angular-library-starter)

This is an external process, it can be done in the cli or by a different package through @angular/dev-kit via separate config files. Because solutions exists we can use them for now. It is purely an implementation and definitely not the problem.

Compiling

This following might be verbose and complex, apologies :)

Compiling is taking the TS files and compiling them to JS files. This is fairly simple with TypeScript (tsc) but get's complicated when using the angular compiler (ngc) The pain point is resources (html, scss, etc):

  • The angular compiler knows how to handle HTML and CSS only, al other formats (SCSS, LESS) will not work.
  • The compiler will not inline resources which are required for libraries.
  • Resources appear in 2 locations, JS output and metadata.json files emitted by the AOT compiler.

ng-packager solved this issue using external process and post-processing scripts, it means using different tools then your development environment (dev use webpack, post-process use gulp tools) and doing it externally instead of part of the compiler.

The @angular/compiler-cli packages is extensible, it can be thought how to load resources so they are loaded as part of the compilation process (no post-processing, native inlining). The problem is that it is missing an async API required for loading resources via webpack (see angular/angular#20130) Once this is fixed it should be easy to do so (it can be done know by creating the API locally, everything requires is available)

By creating a CompilerHost and passing it to angular, we get control over resource loading, the compiler host is the loader of resources (among other things). Now, all we need is a way to use webpack to compile resources only within the compiler host and return them to ngc. We also add custom transformers to inline all resources on the fly and that's it!!!

For this we need to have an API in the CLI that creates a webpack configuration for us (at runtime) and stops (not building an app), from there are can create a webpack instance and create a compilation that can compile resources.

It might look complicated but I already have a working POC that apply a patch to the cli that allows extracting the config, from there it run's the compilation with a native resource loading through webpack. The result goes through rollup and creates the bundles.

The compilation step is done without extra logic or complicated addition to the cli.

@filipesilva @hansl If it's possible, can we discuss it and I'll come with a proposal that will only allow integrating the compilation step (no bundling, no rollup no nothing). This is a first step that will break the compilation barrier allowing ngc output that has resources inlined and compiled through webpack with cli configuration.

This is a logical flow:

  • Call CLI API to get webpack config object for a build state (build, prod, dev etc...)
  • Use custom compiler host that has a webpack resource loader loaded and ready to compile resources through the loader chain.
  • Call performCompilation (async version) with the compiler host and an inlining transformer.
  • Output is ready for bundling

shlomiassaf avatar Nov 26 '17 19:11 shlomiassaf