orenyakobi / lazy

Lightweight lazy loader for JS, CSS & LESS assets.
10 stars 1 forks source link

lazy

Lightweight (3KB!) lazy loader for JS, CSS & LESS assets.

On demand assets loader built with ease, nested dependencies support and cache care.

Installation

Usage

Asynchronic assets load

lazy.load(['myScript.js','myStyle.css','myLessStyle.less']);`

And this is also fine:

lazy.load('myScript.js,myStyle.css,myLessStyle.less');

Dependencies

Use the '<' operator to define dependencies while x < y means: x depand on y, so Lazy will make sure y is being loaded before x. You can also use '<' to define nested dependcies:

lazy.load(['loadMeLast.js < loadMeSecond.js < loadMeFirst.less'], ['LoadMeWhenEver.js', 'LoadMeWhenEverAsWell.js']);

loadMeLast.js depand on loadMeSecond.js, which depend on loadMeFirst.less

Lazy loading with callback

You can send a function variable or an anonymous function as the second variable to be call when all the files are loaded

lazy.load(['loadMeLast.js < loadMeFirst.less ', 'LoadMeWhenEver.js'], function(){
    console.log('All files have been loaded');
});

Further reading