Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create angular2 library which could be supported by all script loaders

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.

like image 861
TwitchBronBron Avatar asked Dec 16 '16 18:12

TwitchBronBron


People also ask

How do I add a library to my angular application?

Create a new angular application and install your library inside this using: Check your package.json to see if it has installed and is reflecting isnide the dependencies. You will notice it being added as a file just like this: Next step, import the module of your library, and use the component.

What version of angular should I use to build my application?

The Angular version used to build an application should always be the same or greater than the Angular versions used to build any of its dependent libraries. For example, if you had a library using Angular version 12, the application that depends on that library should use Angular version 12 or later.

Why is my angular library not working in my App?

The Angular CLI uses the tsconfig paths to tell the build system where to find the library. If you find that changes to your library are not reflected in your app, your app is probably using an old build of the library. You can rebuild your library whenever you make changes to it,...

What are the features of an angular library?

The features of a library therefore are, An Angular library contains components, modules, services to define the functionality. We already use so many libraries in our apps for features like a countdown timer, or maybe a scroll bar. If you have some functionality which you see getting used frequently, why not export it as a library and use it.


1 Answers

You can achieve this with ES6 + Angular2 + Webpack.

Before going into that, Let me explain what is umd module system.

The UMD pattern typically attempts to offer compatibility with the most popular script loaders of the day (e.g RequireJS amongst others)

As quoted above, UMD is a pattern where the libraries created with this pattern would support all the modules/script loaders like requirejs, 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 and Vanilla 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 on angular2, i would suggest you to go with ES6, Angular2 and Webpack.

You have to set these configs to get the library in the UMD format, so that it could be used by any script loaders.

output: {
    path: __dirname + '/lib', // path to output
    filename: outputFile, // library file name
    library: libraryName, // library name
    libraryTarget: 'umd', // the umd format
    umdNamedDefine: true // setting this to true will name the AMD module
  },

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:

    <script src="http://example.com/your_lib.js"></script>
    <script>
        // Your_Lib will be available and will attach itself
        // to the global scope.
        var html = Your_Lib.render();
    </script>

RequireJS (AMD):

    <script src="http://example.com/require.js"></script>
    <script> requirejs config goes here </script>
    <script>
        define(['your_lib', function(Your_Lib) {
             var html = Your_Lib.render();
        }])
    </script>

SystemJS (CommonJS):

var map = {
            '@angular': 'node_modules/@angular',
             ....
            'your_lib': 'example.com/your_lib.js'
        };
        var config = {
            defaultJSExtensions: true,
            map: map,
            packages: packages
        };
        System.config(config);

Then you can import your library as usual.

Webpack:

In Index.html

In webpack.config.js

{
    externals: {
        // require("your_lib") is external and available
        //  on the global var Your_Lib
        "your_lib": "Your_Lib"
    }
}

And your library would be available as Your_Lib in the global scope.

like image 128
Thaadikkaaran Avatar answered Sep 30 '22 10:09

Thaadikkaaran