[Feature] Create boilerplate for library
This is a feature request.
It will be great if we can create a basic boilerplate for creating libraries for pipes, components etc. This is the time when people may be willing to create components for angular 2 but due to the lack of this, it's tough. angular-cli already has a build system in place so it will be easier to integrate it here.
For example, If making a pipe, I won't need the whole src folder. A single file and a page where I can see the preview will do. This will make it easier for a lot of people.
Till this feature is implemented here you guys can use https://github.com/jvandemo/generator-angular2-library. It seems it is in active development.
All thanks to @jvandemo
@ritz078 — Thank you, much appreciated. It will be great for the ecosystem if Angular CLI offers the ability to generate a library at some point. 👍
@playground yes, multiple apps are currently supported by the CLI. There is a story that will be added to the wiki: #5737
That library starter from me might also work: https://github.com/cankayacan/ngx-library-starter
You can create a npm library from existing @angular/cli project itself.
Create a new project with ng new -projectname edit "package.json" Add "typings": "./index.d.ts" (change to any name you want)
in "index.d.ts" file export all the classes/interfaces you need to expose, you can even included a new ts file here which has a export (like a nested export)
in my case i used
export * from "./src/app/model";
export * from "./src/app/service";
model and service folders will have index.d.ts which exports the underlying classes/interfaces.
now login to npm from root folder
npm login
and publish the module
npm publish
How to use the published module in angular project
Just add a dependency and devDependencies in your project
"module-project": "^1.0.13"
@achieverprince, you are right, but this approach could not be an official one, caus it has some limitations. For example:
- it could not be used directly from es projects without extra typescript loader, because it based on typescript code distribution
- It contains files as index.htm, main.ts, etc. Those are redundant for library, but could be used as sandbox/live-demo purpose.
- test files are visible in library and it is wired.
- internal classes are visible too and it even more wired.
Even that, we using this approach inside of our team for half year already. It just works and compatible with aot-compilation out of the box (within another angular-cli project).
I just put link to example repository here.
@nsmolenskii , you are right.
I am stuck in final stage with this error
compiler.es5.js:1540 Uncaught Error: Encountered undefined provider! Usually this means you have a circular dependencies (might be caused by using 'barrel' index.ts files.
May be i will try your approach which is neater. But i also have few classes that has to be exposed and can be imported in main project.
Usually, such errors are happens when you doing some imports like:
import {something}from './';
import {something}from '../';
try to get rid of such imports and use at least one path segment explicit.
@nsmolenskii
It was not related to that. Changes i made make it work
i was using "module" property in package.json and referencing it to exported js file (This will not work as the exported js files will not have all the modules)
i removed "module" and not exporting any js file now
using "main" property as your library project which points to same typing ts file
"main": "src/index.ts"
1 more change i had to make is rename all index.d.ts > index.ts , which solved the issue. And i was using dependency as npm module itself (i need to publish to npm every time to get a new version), and my project is private code so now changed to local svn repository.
But i am not able to directly download the code from svn //DOESN'T WORK
"dependencies": {
"yl-angular": "http://****/svn/maven2/repo/trunk/yl-angular/yl-angular/"
}
//WORK - Local
"dependencies": {
"yl-angular": "D://JAVA_DEVELOPMENT//G2_NEW//yl-angular//yl-angular"
}
Do you have any idea to use svn repo for depencency?
@achieverprince @nsmolenskii I strongly discourage that way of publishing a library, it's not at all compliant with the Angular Package Format.
Meanwhile you can read a tentative guide to third party libraries in https://github.com/angular/angular/pull/16486, and a seed for it in https://github.com/filipesilva/angular-quickstart-lib.
@filipesilva, thanks for a really reasonable comment.
I'm not pretend, that my suggestion is a proper way to manage components libraries, it is just workaround until official support by angular-cli finished. I have to admit, that such suggestions would never have appeared, if this issue had been solved.
Now you feel the irony of the situation, don't you?
@filipesilva
Thanks for the update. I will check https://github.com/filipesilva/angular-quickstart-lib I know you guys will have lots of other issues to focus on. Hoping to see an update from cli team soon :)
@nsmolenskii Finally every thing works here , used this npm plugin for svn support in repository. At least i was happy that we are able to bring a temp solution to the desk to keep the project going.
Hi, for my purposes, I scrambled together a packaging script that does all the necessary transformations and produces a library in the Angular Package Format:
https://github.com/dherges/ng-packagr
There are still issues, minification is not yet implemented ~~, source maps need to be re-mapped, and the source maps from downleveling with tsc are lost~~ ... so I will neither support this nor publish this to npm. Things are inspired by the build tasks from the material2 team
If you like, let's try to incorporate this into Angular CLI. Is there anyone working on the compile library feature? If yes, who can I get in touch with?
Cheers! :beers:
UPDATE: I even added the source-map transformation chain.
@dherges I appreciate your offer but we're not yet open to contributions for this new feature :/
@filipesilva Ok, no deal.
Given that, I am looking for people from the community who are interested in implementing a packaging tool for Angular libraries. You can contact me here on my Twitter account
In contradiction to what I just said a very very loooong time ago, I decided to publish ng-packagr.
Once again, I stress that I am not really able to support this tool for everyone. I believe that a community effort will lead to way better results than one individual can ever achieve. Feel free to use ng-packagr when it suits your needs! If you find any issues or want to add missing features, don't hesitate to contact me ... open issues or pull requests! It's always welcome!
It looks like our company is going to hold back on publishing our Angular modules to the Open Source community until this is figured out. We need to spend more time writing application code, less time maintaining our own custom build chain. We're happy to contribute some hours to helping standardize on this.
Thanks to everyone who threw starter packs together. We've struggled with this problem too and spent countless hours on our NG2 builds. In the end some combination of ngc, webpack and custom gulp tasks worked for us.
To angular team: Angular is loosing ground to React and Vue and others for developer mindshare. Mainly due to the fact that it's so damn hard to package reusable components. This should be your number 1 concern to kick start Angular community.
@oliverjanik
I absolutely agree with you.
Angular should support code sharing(library) as soon as possible
@achieverprince that is disrespectful to framework authors. IMHO framework can be anything for anyone. And remember, we get it for free (which is kinda amazing) - if you don't like something - patches are always welcome