How can I create a javascript file that contains an Angular 2 module that can be used by other applications, but is loaded at runtime directly from a centralized server and is NOT bundled into a specific application's code?
Think of this as a CDN for an Angular 2 library. The requirement is that the consumers of this library will include a single script on their page.
It is a requirement to implement it this way, so I am not interested in any answers that suggest bundling the library into the individual application's output files. The library script must be loaded directly from the library's server at runtime.
- The centralized web app is called CustomAuth
- CustomAuth has a single Angular 2 module called
CustomAuthModule
- CustomAuthModule exposes several services and components that can be used by external Angular 2 applications.
Here is the desired workflow (from a high level).
- A developer wants to use CustomAuth in their Angular2 application called BookLibrary.
- On the developer's index page, they add a script include that points to
http://server-url/CustomAuth/script
. The consumer should not be required to know anything about module loaders like systemjs or webpack. - In their angular2 code, they import stuff from the CustomAuth module (services, components, etc...).
- When they compile their application using the angular-cli, it will not include the CustomAuth code, but will instead assume that it will be loaded on the fly at runtime.
I've done a lot of research on this, and I'm not having any luck figuring it out. Any help would be greatly appreciated.
You can achieve this with
ES6
+Angular2
+Webpack
.Before going into that, Let me explain what is
umd
module system.As quoted above,
UMD
is a pattern where the libraries created with this pattern would support all the modules/script loaders likerequirejs
,webpack
,browserify
,systemjs
etc. I.e the libraries which followed the UMD pattern would be recognized by all the major module systems (AMD
,CommonJS
,ES6
andVanilla JS
).This is the way that all the libraries in the CDN works.
Now, even you have to follow the same i.e
UMD
pattern. Since your library is onangular2
, i would suggest you to go withES6
,Angular2
andWebpack
.You have to set these configs to get the library in the UMD format, so that it could be used by any script loaders.
Once your webpack output bundle is ready (umd module), then any user can inject your library into the index page and start using your angular2 components regardless of script loaders/ modules systems they use.
There is a nice example here and he explained this here
Q: How would a consumer of our library include this umd bundle in their Angular 2 application?
Ans: Since your library is going to be a UMD module, user can include the library based on the script loader/module system they are using with their application.
For example. Vanilla JS:
RequireJS (AMD):
SystemJS (CommonJS):
Then you can import your library as usual.
Webpack:
In Index.html
In webpack.config.js
And your library would be available as
Your_Lib
in the global scope.