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

ぃ、小莉子 提交于 2019-12-03 11:36:41

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!