Gathering detailed insights and metrics for loadjs
Gathering detailed insights and metrics for loadjs
Gathering detailed insights and metrics for loadjs
Gathering detailed insights and metrics for loadjs
A tiny async loader / dependency manager for modern browsers (961 bytes)
npm install loadjs
Typescript
Module System
Node Version
NPM Version
96.1
Supply Chain
100
Quality
76.9
Maintenance
100
Vulnerability
100
License
Total
65,895,702
Last Day
8,434
Last Week
231,082
Last Month
968,529
Last Year
11,375,534
2,583 Stars
174 Commits
150 Forks
73 Watching
2 Branches
8 Contributors
Updated on 21 Nov 2024
Minified
Minified + Gzipped
JavaScript (98.43%)
HTML (1.44%)
CSS (0.13%)
Cumulative downloads
Total Downloads
Last day
10.5%
8,434
Compared to previous day
Last week
5.9%
231,082
Compared to previous week
Last month
-6.1%
968,529
Compared to previous month
Last year
-3.8%
11,375,534
Compared to previous year
7
LoadJS is a tiny async loader for modern browsers (961 bytes).
LoadJS is a tiny async loading library for modern browsers (IE9+). It has a simple yet powerful dependency management system that lets you fetch JavaScript, CSS and image files in parallel and execute code after the dependencies have been met. The recommended way to use LoadJS is to include the minified source code of loadjs.js in your <html> (possibly in the <head> tag) and then use the loadjs
global to manage JavaScript dependencies after pageload.
LoadJS is based on the excellent $script library by Dustin Diaz. We kept the behavior of the library the same but we re-wrote the code from scratch to add support for success/error callbacks and to optimize the library for modern browsers. LoadJS is 961 bytes (minified + gzipped).
Here's an example of what you can do with LoadJS:
1<script src="//unpkg.com/loadjs@latest/dist/loadjs.min.js"></script> 2<script> 3 // define a dependency bundle and execute code when it loads 4 loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar'); 5 6 loadjs.ready('foobar', function() { 7 /* foo.js & bar.js loaded */ 8 }); 9</script>
You can also use more advanced syntax for more options:
1<script src="//unpkg.com/loadjs@latest/dist/loadjs.min.js"></script> 2<script> 3 // define a dependency bundle with advanced options 4 loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', { 5 before: function(path, scriptEl) { /* execute code before fetch */ }, 6 async: true, // load files synchronously or asynchronously (default: true) 7 numRetries: 3 // see caveats about using numRetries with async:false (default: 0), 8 returnPromise: false // return Promise object (default: false) 9 }); 10 11 loadjs.ready('foobar', { 12 success: function() { /* foo.js & bar.js loaded */ }, 13 error: function(depsNotFound) { /* foobar bundle load failed */ }, 14 }); 15</script>
The latest version of LoadJS can be found in the dist/
directory in this repository:
It's also available from these public CDNs:
You can also use it as a CJS or AMD module:
1$ npm install --save loadjs
1var loadjs = require('loadjs'); 2 3loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar'); 4 5loadjs.ready('foobar', function() { 6 /* foo.js & bar.js loaded */ 7});
async: false
support only works in IE10+)LoadJS also detects script load failures from AdBlock Plus and Ghostery in:
Note: LoadJS treats empty CSS files as load failures in IE9-11 and uses rel="preload"
to load CSS files in Edge (to get around lack of support for onerror events on <link rel="stylesheet">
tags)
Load a single file
1loadjs('/path/to/foo.js', function() { 2 /* foo.js loaded */ 3});
Fetch files in parallel and load them asynchronously
1loadjs(['/path/to/foo.js', '/path/to/bar.js'], function() { 2 /* foo.js and bar.js loaded */ 3});
Fetch JavaScript, CSS and image files
1loadjs(['/path/to/foo.css', '/path/to/bar.png', 'path/to/thunk.js'], function() { 2 /* foo.css, bar.png and thunk.js loaded */ 3});
Force treat file as CSS stylesheet
1loadjs(['css!/path/to/cssfile.custom'], function() { 2 /* cssfile.custom loaded as stylesheet */ 3});
Force treat file as image
1loadjs(['img!/path/to/image.custom'], function() { 2 /* image.custom loaded */ 3});
Load JavaScript files as modules with non-module fallbacks (in browsers without module support)
1loadjs(['module!/path/to/foo.js', 'nomodule!/path/to/bar.js'], function() { 2 /* foo.js loaded with type="module" in browsers with module support, skipped silently in browsers without */ 3 /* bar.js loaded with type="text/javascript" in browsers without module support, skipped silently in browsers with */ 4});
Add a bundle id
1loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', function() { 2 /* foo.js & bar.js loaded */ 3});
Use .ready() to define bundles and callbacks separately
1loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar'); 2 3loadjs.ready('foobar', function() { 4 /* foo.js & bar.js loaded */ 5});
Use multiple bundles in .ready() dependency lists
1loadjs('/path/to/foo.js', 'foo'); 2loadjs(['/path/to/bar1.js', '/path/to/bar2.js'], 'bar'); 3 4loadjs.ready(['foo', 'bar'], function() { 5 /* foo.js & bar1.js & bar2.js loaded */ 6});
Chain .ready() together
1loadjs('/path/to/foo.js', 'foo'); 2loadjs('/path/to/bar.js', 'bar'); 3 4loadjs 5 .ready('foo', function() { 6 /* foo.js loaded */ 7 }) 8 .ready('bar', function() { 9 /* bar.js loaded */ 10 });
Use Promises to register callbacks
1loadjs(['/path/to/foo.js', '/path/to/bar.js'], {returnPromise: true}) 2 .then(function() { /* foo.js & bar.js loaded */ }) 3 .catch(function(pathsNotFound) { /* at least one didn't load */ });
Check if bundle has already been defined
1if (!loadjs.isDefined('foobar')) { 2 loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', function() { 3 /* foo.js & bar.js loaded */ 4 }); 5}
Fetch files in parallel and load them in series
1loadjs(['/path/to/foo.js', '/path/to/bar.js'], { 2 success: function() { /* foo.js and bar.js loaded in series */ }, 3 async: false 4});
Add an error callback
1loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', { 2 success: function() { /* foo.js & bar.js loaded */ }, 3 error: function(pathsNotFound) { /* at least one path didn't load */ } 4});
Retry files before calling the error callback
1loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', { 2 success: function() { /* foo.js & bar.js loaded */ }, 3 error: function(pathsNotFound) { /* at least one path didn't load */ }, 4 numRetries: 3 5}); 6 7// NOTE: Using `numRetries` with `async: false` can cause files to load out-of-sync on retries
Execute a callback before script tags are embedded
1loadjs(['/path/to/foo.js', '/path/to/bar.js'], { 2 success: function() {}, 3 error: function(pathsNotFound) {}, 4 before: function(path, scriptEl) { 5 /* called for each script node before being embedded */ 6 if (path === '/path/to/foo.js') scriptEl.crossOrigin = true; 7 } 8});
Bypass LoadJS default DOM insertion mechanism (DOM <head>
)
1loadjs(['/path/to/foo.js'], { 2 success: function() {}, 3 error: function(pathsNotFound) {}, 4 before: function(path, scriptEl) { 5 document.body.appendChild(scriptEl); 6 7 /* return `false` to bypass default DOM insertion mechanism */ 8 return false; 9 } 10});
Use bundle ids in error callback
1loadjs('/path/to/foo.js', 'foo'); 2loadjs('/path/to/bar.js', 'bar'); 3loadjs(['/path/to/thunkor.js', '/path/to/thunky.js'], 'thunk'); 4 5// wait for multiple depdendencies 6loadjs.ready(['foo', 'bar', 'thunk'], { 7 success: function() { 8 // foo.js & bar.js & thunkor.js & thunky.js loaded 9 }, 10 error: function(depsNotFound) { 11 if (depsNotFound.indexOf('foo') > -1) {}; // foo failed 12 if (depsNotFound.indexOf('bar') > -1) {}; // bar failed 13 if (depsNotFound.indexOf('thunk') > -1) {}; // thunk failed 14 } 15});
Use .done() for more control
1loadjs.ready(['dependency1', 'dependency2'], function() { 2 /* run code after dependencies have been met */ 3}); 4 5function fn1() { 6 loadjs.done('dependency1'); 7} 8 9function fn2() { 10 loadjs.done('dependency2'); 11}
Reset dependency trackers
1loadjs.reset();
Implement a require-like dependency manager
1var bundles = { 2 'bundleA': ['/file1.js', '/file2.js'], 3 'bundleB': ['/file3.js', '/file4.js'] 4}; 5 6function require(bundleIds, callbackFn) { 7 bundleIds.forEach(function(bundleId) { 8 if (!loadjs.isDefined(bundleId)) loadjs(bundles[bundleId], bundleId); 9 }); 10 loadjs.ready(bundleIds, callbackFn); 11} 12 13require(['bundleA'], function() { /* bundleA loaded */ }); 14require(['bundleB'], function() { /* bundleB loaded */ }); 15require(['bundleA', 'bundleB'], function() { /* bundleA and bundleB loaded */ });
loadjs/ ├── dist │ ├── loadjs.js │ ├── loadjs.min.js │ └── loadjs.umd.js ├── examples ├── gulpfile.js ├── LICENSE.txt ├── package.json ├── README.md ├── src │ └── loadjs.js ├── test └── umd-templates
Install dependencies
Clone repository
1$ git clone git@github.com:kubetail-org/loadjs.git 2$ cd loadjs
Install node dependencies using npm
1$ npm install
Build examples
1$ npm run build-examples
To view the examples you can use any static file server. To use the nodejs
http-server module:
1$ npm install http-server 2$ npm run http-server -- -p 3000
Then visit http://localhost:3000/examples
Build distribution files
1$ npm run build-dist
The files will be located in the dist
directory.
Run tests
To run the browser tests first build the loadjs
library:
1$ npm run build-tests
Then visit http://localhost:3000/test
Build all files
1$ npm run build-all
No vulnerabilities found.
No security vulnerabilities found.