Reputation: 261
What about official documentation for creating angular2 library?
I don't understand. Why do we need to compile for creating library?
I'd like to have only source (some common modules) that could be reused in multiple projects without to have to compile it.
Upvotes: 0
Views: 90
Reputation: 105547
There are few things you need to do.
The first thing is to create a single module that will be exported from your library and then can be imported into another application. This module can import or re-import other modules of your library.
Then, you need to create a declaration file with typings for your application. You can do that using declaration: true
option of tsc
. It can even be compiled into one file by using outDir
option.
The last thing is that you should tell webpack
not no bundle @angular/...
dependencies into your bundle. This can be done using externals
property. Read more here.
Upvotes: 1
Reputation: 21
I think I understand what you are trying to say. Angular libraries should be AOT (ahead of time) compatible, meaning that when another project imports your library and they want to have their project AOT compiled, it will throw the error if your library (that they imported) is not ready for AOT compilation. There are a lot of requirements to fulfil for reusable angular library, and not a lot of documentation on it.
Upvotes: 1
Reputation: 1054
I am not sure to completely understand your problem, but as far as I know an Angular 2 library is nothing more but an Angular 2 application you import in another as a module using npm. For example ngx-translate.
These kind of modules have to be added to your app.module.ts file.
Angular 2 accepts non-specific Angular 2 modules as well, and you can require them in your app. For example if you want to use MomentJS, you can do :
npm i --save moment
And in your component or service or whatever:
import moment = require("moment");
So to create a library you can follow one of these schemes and nothing should run well.
If you use Webpack and want a library to be globally available (JQuery for example), you can require it in the vendor.ts file.
Upvotes: 1