1.2.1 • Published 3 years ago

async-loader-cdn v1.2.1

Weekly downloads
2
License
ISC
Repository
github
Last release
3 years ago

Instalation

Using npm:

$ npm i async-loader-cdn

UPDATES

  • Change browser support to > 25%
  • Resolve fixture head.append to head.appendChild

What is AsyncLoaderCDN?

The AsyncLoaderCDN is a module that exposes a configurable class, where it has just one method: loader. This method is responsible for reading an informed list (either a fixed list or a firebase return), locating the item specified as a parameter in the method and uploading all necessary files. The load method returns a Promise when resolved it contains the global reference of what was loaded.

As some libraries are very extensive and large, including them in the project even with tree shaking, code splitting, in addition to requiring a more advanced configuration (just like in the case of webpack) can generate a final large file (like firebase library for example).

With AsyncLoaderCDN you can:

  • Significantly reduce your work and the size of the generated files.
  • Load resources at runtime and asynchronous mode according to the needs of the aplication state (load on demand)
  • Distribute several modes with just one instance, it’s no longer necessary other teams from the same ecosystem use their resources through the CDN or package managers.
  • When creating a new module, simply add it to the list to make it available for the entire ecosystem where it’s inserted.
  • Make the same modules available with different versions (new features for example), and the user can choose the version that suits at the moment.
  • If associated with a database (currently connected to Firebase, look at the exemples section) to save the list of modules, it’ll eliminate the need to generate a new release if you need to update CDN links or make a new feature available.

How to use

After installing, you can use in the following ways:

Import

import asyncLoader from 'async-loader-cdn';
const list = [
  {
    global: '_',
    name: 'lodash',
    version: '',
    files: ['https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.19/lodash.min.js'],
  },
];

// use async/await
const myLoaders = new asyncLoader(files);
myLoaders
  .load('lodash')
  .then((_) => {
    if (!_) return console.log('erro');
    console.log(_.VERSION);
  })
  .catch((err) => console.log(err));

Important

If you decide to use the list parameter, you need to send a list with this structure (contract):

const list = [
  {
    global: String, // global variable name of the module or project. Ex.: '$' if you want to load jQuery
    name: String, // name that asyncLoaderCDN will look for when the load method is called. Ex.: 'jQuery'
    version: <String|Int>, // if you need distribute multiple versions of the same module.
    files: [String],
  },
];

Examples

  • Usage with firebase here
  • Usage with list parameter here

How to contribute

To contribute to the project, fork this repository;

  • Create a branch with your feature;
  • Commit your changes;
  • Push to your branch;
  • Open a PR for the master;

After the PR is approved and the merge occurs, you can delete your branch.

1.2.0

3 years ago

1.2.1

3 years ago

1.1.0

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.8-beta.3

4 years ago

1.0.8-beta.2

4 years ago

1.0.8-beta.1

4 years ago

1.0.8-beta.0

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago