Socket
Socket
Sign inDemoInstall

loadjs

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

loadjs - npm Package Compare versions

Comparing version 2.0.0 to 2.1.0

test/assets/empty.css

2

bower.json
{
"name": "loadjs",
"version": "2.0.0",
"version": "2.1.0",
"license": "MIT",

@@ -5,0 +5,0 @@ "authors": [

# LoadJS Changelog
## 2.0.1 - June 19, 2016
* Added support for loading CSS files
## 2.0.0 - June 15, 2016

@@ -4,0 +8,0 @@

@@ -79,20 +79,35 @@ loadjs = (function () {

/**
* Load individual JavaScript file.
* Load individual file.
* @param {string} path - The file path
* @param {Function} callbackFn - The callback function
*/
function loadScript(path, callbackFn, async) {
function loadFile(path, callbackFn, async) {
var doc = document,
s = doc.createElement('script');
e;
s.src = path;
s.async = (async === undefined) ? true : async;
if (/\.css$/.test(path)) {
// css
e = doc.createElement('link');
e.rel = 'stylesheet';
e.href = path;
} else {
// javascript
e = doc.createElement('script');
e.src = path;
e.async = (async === undefined) ? true : async;
}
s.onload = s.onerror = s.onbeforeload = function(ev) {
e.onload = e.onerror = e.onbeforeload = function(ev) {
var result = ev.type[0];
// treat empty stylesheets as failures (to get around lack of onerror
// support in IE
if (e.sheet && !e.sheet.cssRules.length) result = 'e';
// execute callback
callbackFn(path, ev.type[0], ev.defaultPrevented);
callbackFn(path, result, ev.defaultPrevented);
};
// add to document
doc.head.appendChild(s);
doc.head.appendChild(e);
}

@@ -102,7 +117,7 @@

/**
* Load multiple JavaScript files.
* Load multiple files.
* @param {string[]} paths - The file paths
* @param {Function} callbackFn - The callback function
*/
function loadScripts(paths, callbackFn, async) {
function loadFiles(paths, callbackFn, async) {
// listify paths

@@ -130,3 +145,3 @@ paths = paths.push ? paths : [paths];

// load scripts
for (i=0; i <= x - 1; i++) loadScript(paths[i], fn, async);
for (i=0; i < x; i++) loadFile(paths[i], fn, async);
}

@@ -154,3 +169,3 @@

if (bundleId in bundleIdCache) {
throw new Error("LoadJS: Bundle already defined");
throw new Error("LoadJS");
} else {

@@ -162,3 +177,3 @@ bundleIdCache[bundleId] = true;

// load scripts
loadScripts(paths, function(pathsNotFound) {
loadFiles(paths, function(pathsNotFound) {
// success and fail callbacks

@@ -165,0 +180,0 @@ if (pathsNotFound.length) (args.fail || devnull)(pathsNotFound);

@@ -1,1 +0,1 @@

loadjs=function(){function n(n,e){n=n.push?n:[n];var t,o,r,i,f=[],a=n.length,s=a;for(t=function(n,t){t.length&&f.push(n),s--,s||e(f)};a--;)o=n[a],r=u[o],r?t(o,r):(i=c[o]=c[o]||[],i.push(t))}function e(n,e){if(n){var t=c[n];if(u[n]=e,t)for(;t.length;)t[0](n,e),t.splice(0,1)}}function t(n,e,t){var o=document,r=o.createElement("script");r.src=n,r.async=void 0===t?!0:t,r.onload=r.onerror=r.onbeforeload=function(t){e(n,t.type[0],t.defaultPrevented)},o.head.appendChild(r)}function o(n,e,o){n=n.push?n:[n];var r,i,f=n.length,u=f,c=[];for(r=function(n,t,o){if("e"==t&&c.push(n),"b"==t){if(!o)return;c.push(n)}f--,f||e(c)},i=0;u-1>=i;i++)t(n[i],r,o)}function r(n,t,r){var u,c;if(t&&t.trim&&(u=t),c=(u?r:t)||{},u){if(u in f)throw new Error("LoadJS: Bundle already defined");f[u]=!0}o(n,function(n){n.length?(c.fail||i)(n):(c.success||i)(),e(u,n)},c.async)}var i=function(){},f={},u={},c={};return r.ready=function(e,t){return n(e,function(n){n.length?(t.fail||i)(n):(t.success||i)()}),r},r.done=function(n){e(n,[])},r}();
loadjs=function(){function n(n,e){n=n.push?n:[n];var t,r,o,i,c=[],u=n.length,a=u;for(t=function(n,t){t.length&&c.push(n),a--,a||e(c)};u--;)r=n[u],o=f[r],o?t(r,o):(i=s[r]=s[r]||[],i.push(t))}function e(n,e){if(n){var t=s[n];if(f[n]=e,t)for(;t.length;)t[0](n,e),t.splice(0,1)}}function t(n,e,t){var r,o=document;/\.css$/.test(n)?(r=o.createElement("link"),r.rel="stylesheet",r.href=n):(r=o.createElement("script"),r.src=n,r.async=void 0===t?!0:t),r.onload=r.onerror=r.onbeforeload=function(t){var o=t.type[0];r.sheet&&!r.sheet.cssRules.length&&(o="e"),e(n,o,t.defaultPrevented)},o.head.appendChild(r)}function r(n,e,r){n=n.push?n:[n];var o,i,c=n.length,f=c,s=[];for(o=function(n,t,r){if("e"==t&&s.push(n),"b"==t){if(!r)return;s.push(n)}c--,c||e(s)},i=0;f>i;i++)t(n[i],o,r)}function o(n,t,o){var f,s;if(t&&t.trim&&(f=t),s=(f?o:t)||{},f){if(f in c)throw new Error("LoadJS");c[f]=!0}r(n,function(n){n.length?(s.fail||i)(n):(s.success||i)(),e(f,n)},s.async)}var i=function(){},c={},f={},s={};return o.ready=function(e,t){return n(e,function(n){n.length?(t.fail||i)(n):(t.success||i)()}),o},o.done=function(n){e(n,[])},o}();

@@ -87,20 +87,35 @@ (function(root, factory) {

/**
* Load individual JavaScript file.
* Load individual file.
* @param {string} path - The file path
* @param {Function} callbackFn - The callback function
*/
function loadScript(path, callbackFn, async) {
function loadFile(path, callbackFn, async) {
var doc = document,
s = doc.createElement('script');
e;
s.src = path;
s.async = (async === undefined) ? true : async;
if (/\.css$/.test(path)) {
// css
e = doc.createElement('link');
e.rel = 'stylesheet';
e.href = path;
} else {
// javascript
e = doc.createElement('script');
e.src = path;
e.async = (async === undefined) ? true : async;
}
s.onload = s.onerror = s.onbeforeload = function(ev) {
e.onload = e.onerror = e.onbeforeload = function(ev) {
var result = ev.type[0];
// treat empty stylesheets as failures (to get around lack of onerror
// support in IE
if (e.sheet && !e.sheet.cssRules.length) result = 'e';
// execute callback
callbackFn(path, ev.type[0], ev.defaultPrevented);
callbackFn(path, result, ev.defaultPrevented);
};
// add to document
doc.head.appendChild(s);
doc.head.appendChild(e);
}

@@ -110,7 +125,7 @@

/**
* Load multiple JavaScript files.
* Load multiple files.
* @param {string[]} paths - The file paths
* @param {Function} callbackFn - The callback function
*/
function loadScripts(paths, callbackFn, async) {
function loadFiles(paths, callbackFn, async) {
// listify paths

@@ -138,3 +153,3 @@ paths = paths.push ? paths : [paths];

// load scripts
for (i=0; i <= x - 1; i++) loadScript(paths[i], fn, async);
for (i=0; i < x; i++) loadFile(paths[i], fn, async);
}

@@ -162,3 +177,3 @@

if (bundleId in bundleIdCache) {
throw new Error("LoadJS: Bundle already defined");
throw new Error("LoadJS");
} else {

@@ -170,3 +185,3 @@ bundleIdCache[bundleId] = true;

// load scripts
loadScripts(paths, function(pathsNotFound) {
loadFiles(paths, function(pathsNotFound) {
// success and fail callbacks

@@ -173,0 +188,0 @@ if (pathsNotFound.length) (args.fail || devnull)(pathsNotFound);

@@ -79,20 +79,35 @@ loadjs = (function () {

/**
* Load individual JavaScript file.
* Load individual file.
* @param {string} path - The file path
* @param {Function} callbackFn - The callback function
*/
function loadScript(path, callbackFn, async) {
function loadFile(path, callbackFn, async) {
var doc = document,
s = doc.createElement('script');
e;
s.src = path;
s.async = (async === undefined) ? true : async;
if (/\.css$/.test(path)) {
// css
e = doc.createElement('link');
e.rel = 'stylesheet';
e.href = path;
} else {
// javascript
e = doc.createElement('script');
e.src = path;
e.async = (async === undefined) ? true : async;
}
s.onload = s.onerror = s.onbeforeload = function(ev) {
e.onload = e.onerror = e.onbeforeload = function(ev) {
var result = ev.type[0];
// treat empty stylesheets as failures (to get around lack of onerror
// support in IE
if (e.sheet && !e.sheet.cssRules.length) result = 'e';
// execute callback
callbackFn(path, ev.type[0], ev.defaultPrevented);
callbackFn(path, result, ev.defaultPrevented);
};
// add to document
doc.head.appendChild(s);
doc.head.appendChild(e);
}

@@ -102,7 +117,7 @@

/**
* Load multiple JavaScript files.
* Load multiple files.
* @param {string[]} paths - The file paths
* @param {Function} callbackFn - The callback function
*/
function loadScripts(paths, callbackFn, async) {
function loadFiles(paths, callbackFn, async) {
// listify paths

@@ -130,3 +145,3 @@ paths = paths.push ? paths : [paths];

// load scripts
for (i=0; i <= x - 1; i++) loadScript(paths[i], fn, async);
for (i=0; i < x; i++) loadFile(paths[i], fn, async);
}

@@ -154,3 +169,3 @@

if (bundleId in bundleIdCache) {
throw new Error("LoadJS: Bundle already defined");
throw new Error("LoadJS");
} else {

@@ -162,3 +177,3 @@ bundleIdCache[bundleId] = true;

// load scripts
loadScripts(paths, function(pathsNotFound) {
loadFiles(paths, function(pathsNotFound) {
// success and fail callbacks

@@ -165,0 +180,0 @@ if (pathsNotFound.length) (args.fail || devnull)(pathsNotFound);

@@ -1,1 +0,1 @@

loadjs=function(){function n(n,e){n=n.push?n:[n];var t,o,r,i,f=[],a=n.length,s=a;for(t=function(n,t){t.length&&f.push(n),s--,s||e(f)};a--;)o=n[a],r=u[o],r?t(o,r):(i=c[o]=c[o]||[],i.push(t))}function e(n,e){if(n){var t=c[n];if(u[n]=e,t)for(;t.length;)t[0](n,e),t.splice(0,1)}}function t(n,e,t){var o=document,r=o.createElement("script");r.src=n,r.async=void 0===t?!0:t,r.onload=r.onerror=r.onbeforeload=function(t){e(n,t.type[0],t.defaultPrevented)},o.head.appendChild(r)}function o(n,e,o){n=n.push?n:[n];var r,i,f=n.length,u=f,c=[];for(r=function(n,t,o){if("e"==t&&c.push(n),"b"==t){if(!o)return;c.push(n)}f--,f||e(c)},i=0;u-1>=i;i++)t(n[i],r,o)}function r(n,t,r){var u,c;if(t&&t.trim&&(u=t),c=(u?r:t)||{},u){if(u in f)throw new Error("LoadJS: Bundle already defined");f[u]=!0}o(n,function(n){n.length?(c.fail||i)(n):(c.success||i)(),e(u,n)},c.async)}var i=function(){},f={},u={},c={};return r.ready=function(e,t){return n(e,function(n){n.length?(t.fail||i)(n):(t.success||i)()}),r},r.done=function(n){e(n,[])},r}();
loadjs=function(){function n(n,e){n=n.push?n:[n];var t,r,o,i,c=[],u=n.length,a=u;for(t=function(n,t){t.length&&c.push(n),a--,a||e(c)};u--;)r=n[u],o=f[r],o?t(r,o):(i=s[r]=s[r]||[],i.push(t))}function e(n,e){if(n){var t=s[n];if(f[n]=e,t)for(;t.length;)t[0](n,e),t.splice(0,1)}}function t(n,e,t){var r,o=document;/\.css$/.test(n)?(r=o.createElement("link"),r.rel="stylesheet",r.href=n):(r=o.createElement("script"),r.src=n,r.async=void 0===t?!0:t),r.onload=r.onerror=r.onbeforeload=function(t){var o=t.type[0];r.sheet&&!r.sheet.cssRules.length&&(o="e"),e(n,o,t.defaultPrevented)},o.head.appendChild(r)}function r(n,e,r){n=n.push?n:[n];var o,i,c=n.length,f=c,s=[];for(o=function(n,t,r){if("e"==t&&s.push(n),"b"==t){if(!r)return;s.push(n)}c--,c||e(s)},i=0;f>i;i++)t(n[i],o,r)}function o(n,t,o){var f,s;if(t&&t.trim&&(f=t),s=(f?o:t)||{},f){if(f in c)throw new Error("LoadJS");c[f]=!0}r(n,function(n){n.length?(s.fail||i)(n):(s.success||i)(),e(f,n)},s.async)}var i=function(){},c={},f={},s={};return o.ready=function(e,t){return n(e,function(n){n.length?(t.fail||i)(n):(t.success||i)()}),o},o.done=function(n){e(n,[])},o}();
{
"name": "loadjs",
"version": "2.0.0",
"version": "2.1.0",
"license": "MIT",

@@ -5,0 +5,0 @@ "description": "Tiny async loader for modern browsers",

@@ -5,3 +5,3 @@ # LoadJS

LoadJS is a tiny async loader for modern browsers (585 bytes).
LoadJS is a tiny async loader for modern browsers (630 bytes).

@@ -13,5 +13,5 @@ [![Dependency Status](https://david-dm.org/muicss/loadjs.svg)](https://david-dm.org/muicss/loadjs)

LoadJS is a tiny async loading library for modern browsers (IE10+). It has a simple yet powerful dependency management system that lets you fetch 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 in your &lt;html&gt; and then use the `loadjs` global to manage JavaScript dependencies after pageload.
LoadJS is a tiny async loading library for modern browsers (IE9+). It has a simple yet powerful dependency management system that lets you fetch JavaScrip and CSS 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 in your &lt;html&gt; and then use the `loadjs` global to manage JavaScript dependencies after pageload.
LoadJS is based on the excellent <a href="https://github.com/ded/script.js">$script</a> library by <a href="https://github.com/ded">Dustin Diaz</a>. We kept the behavior of the library the same but we re-wrote the code from scratch to add support for success/failure callbacks and to optimize the library for modern browsers. LoadJS is 586 bytes (minified + gzipped).
LoadJS is based on the excellent <a href="https://github.com/ded/script.js">$script</a> library by <a href="https://github.com/ded">Dustin Diaz</a>. We kept the behavior of the library the same but we re-wrote the code from scratch to add support for success/failure callbacks and to optimize the library for modern browsers. LoadJS is 630 bytes (minified + gzipped).

@@ -26,8 +26,4 @@ Here's an example of what you can do with LoadJS:

loadjs.ready('foobar', {
success: function() {
// foo.js & bar.js loaded
},
fail: function(depsNotFound) {
// foobar bundle load failed
}
success: function() { /* foo.js & bar.js loaded */ },
fail: function(depsNotFound) { /* foobar bundle load failed */ }
});

@@ -59,3 +55,3 @@ ```

* IE10+
* IE9+ (`async: false` support only works in IE10+)
* Opera 12+

@@ -73,2 +69,4 @@ * Safari 5+

Note: LoadJS treats empty CSS files as load failures (as a workaround for the lack of support for `<link>` onerror events in IE9-11).
## Documentation

@@ -84,3 +82,3 @@

2. Fetch files in parallel and load asynchronously
1. Fetch files in parallel and load them asynchronously

@@ -93,3 +91,3 @@ ```javascript

3. Fetch files in parallel and load in series
1. Fetch files in parallel and load them in series

@@ -103,4 +101,12 @@ ```javascript

4. Add a bundle id
1. Fetch JavaScript and CSS files
```javascript
loadjs(['/path/to/foo.css', '/path/to/bar.js'], {
success: function() { /* foo.css and bar.js loaded */ }
});
```
1. Add a bundle id
```javascript

@@ -113,3 +119,3 @@ // add a bundle id

5. Add a failure callback
1. Add a failure callback

@@ -123,3 +129,3 @@ ```javascript

6. Execute a callback after bundle finishes loading
1. Execute a callback after bundle finishes loading

@@ -134,3 +140,3 @@ ```javascript

7. Chain .ready() together
1. Chain .ready() together

@@ -150,3 +156,3 @@ ```javascript

8. Compose more complex dependency lists
1. Compose more complex dependency lists

@@ -171,3 +177,3 @@ ```javascript

9. Use .done() for more control
1. Use .done() for more control

@@ -174,0 +180,0 @@ ```javascript

@@ -78,20 +78,35 @@ /**

/**
* Load individual JavaScript file.
* Load individual file.
* @param {string} path - The file path
* @param {Function} callbackFn - The callback function
*/
function loadScript(path, callbackFn, async) {
function loadFile(path, callbackFn, async) {
var doc = document,
s = doc.createElement('script');
e;
s.src = path;
s.async = (async === undefined) ? true : async;
if (/\.css$/.test(path)) {
// css
e = doc.createElement('link');
e.rel = 'stylesheet';
e.href = path;
} else {
// javascript
e = doc.createElement('script');
e.src = path;
e.async = (async === undefined) ? true : async;
}
s.onload = s.onerror = s.onbeforeload = function(ev) {
e.onload = e.onerror = e.onbeforeload = function(ev) {
var result = ev.type[0];
// treat empty stylesheets as failures (to get around lack of onerror
// support in IE
if (e.sheet && !e.sheet.cssRules.length) result = 'e';
// execute callback
callbackFn(path, ev.type[0], ev.defaultPrevented);
callbackFn(path, result, ev.defaultPrevented);
};
// add to document
doc.head.appendChild(s);
doc.head.appendChild(e);
}

@@ -101,7 +116,7 @@

/**
* Load multiple JavaScript files.
* Load multiple files.
* @param {string[]} paths - The file paths
* @param {Function} callbackFn - The callback function
*/
function loadScripts(paths, callbackFn, async) {
function loadFiles(paths, callbackFn, async) {
// listify paths

@@ -129,3 +144,3 @@ paths = paths.push ? paths : [paths];

// load scripts
for (i=0; i <= x - 1; i++) loadScript(paths[i], fn, async);
for (i=0; i < x; i++) loadFile(paths[i], fn, async);
}

@@ -153,3 +168,3 @@

if (bundleId in bundleIdCache) {
throw new Error("LoadJS: Bundle already defined");
throw new Error("LoadJS");
} else {

@@ -161,3 +176,3 @@ bundleIdCache[bundleId] = true;

// load scripts
loadScripts(paths, function(pathsNotFound) {
loadFiles(paths, function(pathsNotFound) {
// success and fail callbacks

@@ -164,0 +179,0 @@ if (pathsNotFound.length) (args.fail || devnull)(pathsNotFound);

@@ -79,20 +79,35 @@ loadjs = (function () {

/**
* Load individual JavaScript file.
* Load individual file.
* @param {string} path - The file path
* @param {Function} callbackFn - The callback function
*/
function loadScript(path, callbackFn, async) {
function loadFile(path, callbackFn, async) {
var doc = document,
s = doc.createElement('script');
e;
s.src = path;
s.async = (async === undefined) ? true : async;
if (/\.css$/.test(path)) {
// css
e = doc.createElement('link');
e.rel = 'stylesheet';
e.href = path;
} else {
// javascript
e = doc.createElement('script');
e.src = path;
e.async = (async === undefined) ? true : async;
}
s.onload = s.onerror = s.onbeforeload = function(ev) {
e.onload = e.onerror = e.onbeforeload = function(ev) {
var result = ev.type[0];
// treat empty stylesheets as failures (to get around lack of onerror
// support in IE
if (e.sheet && !e.sheet.cssRules.length) result = 'e';
// execute callback
callbackFn(path, ev.type[0], ev.defaultPrevented);
callbackFn(path, result, ev.defaultPrevented);
};
// add to document
doc.head.appendChild(s);
doc.head.appendChild(e);
}

@@ -102,7 +117,7 @@

/**
* Load multiple JavaScript files.
* Load multiple files.
* @param {string[]} paths - The file paths
* @param {Function} callbackFn - The callback function
*/
function loadScripts(paths, callbackFn, async) {
function loadFiles(paths, callbackFn, async) {
// listify paths

@@ -130,3 +145,3 @@ paths = paths.push ? paths : [paths];

// load scripts
for (i=0; i <= x - 1; i++) loadScript(paths[i], fn, async);
for (i=0; i < x; i++) loadFile(paths[i], fn, async);
}

@@ -154,3 +169,3 @@

if (bundleId in bundleIdCache) {
throw new Error("LoadJS: Bundle already defined");
throw new Error("LoadJS");
} else {

@@ -162,3 +177,3 @@ bundleIdCache[bundleId] = true;

// load scripts
loadScripts(paths, function(pathsNotFound) {
loadFiles(paths, function(pathsNotFound) {
// success and fail callbacks

@@ -165,0 +180,0 @@ if (pathsNotFound.length) (args.fail || devnull)(pathsNotFound);

@@ -1,1 +0,1 @@

loadjs=function(){function n(n,e){n=n.push?n:[n];var t,o,r,i,f=[],a=n.length,s=a;for(t=function(n,t){t.length&&f.push(n),s--,s||e(f)};a--;)o=n[a],r=u[o],r?t(o,r):(i=c[o]=c[o]||[],i.push(t))}function e(n,e){if(n){var t=c[n];if(u[n]=e,t)for(;t.length;)t[0](n,e),t.splice(0,1)}}function t(n,e,t){var o=document,r=o.createElement("script");r.src=n,r.async=void 0===t?!0:t,r.onload=r.onerror=r.onbeforeload=function(t){e(n,t.type[0],t.defaultPrevented)},o.head.appendChild(r)}function o(n,e,o){n=n.push?n:[n];var r,i,f=n.length,u=f,c=[];for(r=function(n,t,o){if("e"==t&&c.push(n),"b"==t){if(!o)return;c.push(n)}f--,f||e(c)},i=0;u-1>=i;i++)t(n[i],r,o)}function r(n,t,r){var u,c;if(t&&t.trim&&(u=t),c=(u?r:t)||{},u){if(u in f)throw new Error("LoadJS: Bundle already defined");f[u]=!0}o(n,function(n){n.length?(c.fail||i)(n):(c.success||i)(),e(u,n)},c.async)}var i=function(){},f={},u={},c={};return r.ready=function(e,t){return n(e,function(n){n.length?(t.fail||i)(n):(t.success||i)()}),r},r.done=function(n){e(n,[])},r}();
loadjs=function(){function n(n,e){n=n.push?n:[n];var t,r,o,i,c=[],u=n.length,a=u;for(t=function(n,t){t.length&&c.push(n),a--,a||e(c)};u--;)r=n[u],o=f[r],o?t(r,o):(i=s[r]=s[r]||[],i.push(t))}function e(n,e){if(n){var t=s[n];if(f[n]=e,t)for(;t.length;)t[0](n,e),t.splice(0,1)}}function t(n,e,t){var r,o=document;/\.css$/.test(n)?(r=o.createElement("link"),r.rel="stylesheet",r.href=n):(r=o.createElement("script"),r.src=n,r.async=void 0===t?!0:t),r.onload=r.onerror=r.onbeforeload=function(t){var o=t.type[0];r.sheet&&!r.sheet.cssRules.length&&(o="e"),e(n,o,t.defaultPrevented)},o.head.appendChild(r)}function r(n,e,r){n=n.push?n:[n];var o,i,c=n.length,f=c,s=[];for(o=function(n,t,r){if("e"==t&&s.push(n),"b"==t){if(!r)return;s.push(n)}c--,c||e(s)},i=0;f>i;i++)t(n[i],o,r)}function o(n,t,o){var f,s;if(t&&t.trim&&(f=t),s=(f?o:t)||{},f){if(f in c)throw new Error("LoadJS");c[f]=!0}r(n,function(n){n.length?(s.fail||i)(n):(s.success||i)(),e(f,n)},s.async)}var i=function(){},c={},f={},s={};return o.ready=function(e,t){return n(e,function(n){n.length?(t.fail||i)(n):(t.success||i)()}),o},o.done=function(n){e(n,[])},o}();

@@ -7,2 +7,3 @@ /**

var pathsLoaded = null, // file register
testEl = null,
assert = chai.assert,

@@ -21,110 +22,123 @@ expect = chai.expect;

it('should call success callback on valid path', function(done) {
loadjs(['assets/file1.js'], {
success: function() {
assert.equal(pathsLoaded['file1.js'], true);
done();
}
});
});
// ==========================================================================
// JavaScript file loading tests
// ==========================================================================
it('should call fail callback on invalid path', function(done) {
loadjs(['assets/file-doesntexist.js'], {
success: function() {
throw "Executed success callback";
},
fail: function(pathsNotFound) {
assert.equal(pathsNotFound.length, 1);
assert.equal(pathsNotFound[0], 'assets/file-doesntexist.js');
done();
}
});
});
describe('JavaScript file loading tests', function() {
it('should call success callback on two valid paths', function(done) {
loadjs(['assets/file1.js', 'assets/file2.js'], {
success: function() {
assert.equal(pathsLoaded['file1.js'], true);
assert.equal(pathsLoaded['file2.js'], true);
done();
}
it('should call success callback on valid path', function(done) {
loadjs(['assets/file1.js'], {
success: function() {
assert.equal(pathsLoaded['file1.js'], true);
done();
}
});
});
});
it('should call fail callback on invalid path', function(done) {
loadjs(['assets/file-doesntexist.js'], {
success: function() {
throw "Executed success callback";
},
fail: function(pathsNotFound) {
assert.equal(pathsNotFound.length, 1);
assert.equal(pathsNotFound[0], 'assets/file-doesntexist.js');
done();
}
});
});
it('should call success callback on two valid paths', function(done) {
loadjs(['assets/file1.js', 'assets/file2.js'], {
success: function() {
assert.equal(pathsLoaded['file1.js'], true);
assert.equal(pathsLoaded['file2.js'], true);
done();
}
});
});
it('should call fail callback on one invalid path', function(done) {
loadjs(['assets/file1.js', 'assets/file-doesntexist.js'], {
success: function() {
throw "Executed success callback";
},
fail: function(pathsNotFound) {
assert.equal(pathsLoaded['file1.js'], true);
assert.equal(pathsNotFound.length, 1);
assert.equal(pathsNotFound[0], 'assets/file-doesntexist.js');
done();
}
it('should call fail callback on one invalid path', function(done) {
loadjs(['assets/file1.js', 'assets/file-doesntexist.js'], {
success: function() {
throw "Executed success callback";
},
fail: function(pathsNotFound) {
assert.equal(pathsLoaded['file1.js'], true);
assert.equal(pathsNotFound.length, 1);
assert.equal(pathsNotFound[0], 'assets/file-doesntexist.js');
done();
}
});
});
});
it('should throw an error if bundle is already defined', function() {
// define bundle
loadjs(['assets/file1.js'], 'bundle3');
it('should support async false', function(done) {
var numCompleted = 0,
numTests = 20,
paths = ['assets/asyncfalse1.js', 'assets/asyncfalse2.js'];
// run tests sequentially
var testFn = function(paths) {
// add cache busters
var pathsUncached = paths.slice(0);
pathsUncached[0] += '?_=' + Math.random();
pathsUncached[1] += '?_=' + Math.random();
// define bundle again
var fn = function() {
loadjs(['assets/file1.js'], 'bundle3');
};
expect(fn).to.throw(Error, "LoadJS: Bundle already defined");
});
it('should create a bundle id and a callback inline', function(done) {
loadjs(['assets/file1.js', 'assets/file2.js'], 'bundle4', {
success: function() {
assert.equal(pathsLoaded['file1.js'], true);
assert.equal(pathsLoaded['file2.js'], true);
done();
loadjs(pathsUncached, {
success: function() {
var f1 = paths[0].replace('assets/', '');
var f2 = paths[1].replace('assets/', '');
// check load order
assert.isTrue(pathsLoaded[f1]);
assert.isFalse(pathsLoaded[f2]);
// increment tests
numCompleted += 1;
if (numCompleted === numTests) {
// exit
done();
} else {
// reset register
pathsLoaded = {};
// run test again
paths.reverse();
testFn(paths);
}
},
async: false
});
}
// run tests
testFn(paths);
});
});
it('should chain loadjs object', function(done) {
function bothDone() {
if (pathsLoaded['file1.js'] && pathsLoaded['file2.js']) done();
}
// define bundles
loadjs('assets/file1.js', 'bundle5');
loadjs('assets/file2.js', 'bundle6');
loadjs
.ready('bundle5', {
// Un-'x' this for testing ad blocked scripts.
// Ghostery: Disallow "Google Adservices"
// AdBlock Plus: Add "www.googletagservices.com/tag/js/gpt.js" as a
// custom filter under Options
//
xit('it should report ad blocked scripts as missing', function(done) {
var blockedScript = 'https://www.googletagservices.com/tag/js/gpt.js';
loadjs([blockedScript, 'assets/file1.js'], {
success: function() {
throw "Executed success callback";
},
fail: function(pathsNotFound) {
assert.equal(pathsLoaded['file1.js'], true);
bothDone();
}})
.ready('bundle6', {
success: function() {
assert.equal(pathsLoaded['file2.js'], true);
bothDone();
assert.equal(pathsNotFound.length, 1);
assert.equal(pathsNotFound[0], blockedScript);
done();
}
});
});
it('should handle multiple dependencies', function(done) {
loadjs('assets/file1.js', 'bundle7');
loadjs('assets/file2.js', 'bundle8');
loadjs.ready(['bundle7', 'bundle8'], {
success: function() {
assert.equal(pathsLoaded['file1.js'], true);
assert.equal(pathsLoaded['file2.js'], true);
done();
}
});

@@ -134,140 +148,248 @@ });

it('should fail on missing depdendencies', function(done) {
loadjs('assets/file1.js', 'bundle9');
loadjs('assets/file-doesntexist.js', 'bundle10');
loadjs.ready(['bundle9', 'bundle10'], {
success: function() {
throw "Executed success callback";
},
fail: function(depsNotFound) {
assert.equal(pathsLoaded['file1.js'], true);
assert.equal(depsNotFound.length, 1);
assert.equal(depsNotFound[0], 'bundle10');
done();
}
});
});
// ==========================================================================
// CSS file loading tests
// ==========================================================================
describe('CSS file loading tests', function() {
it('should execute callbacks on .done()', function(done) {
// add handler
loadjs.ready('plugin1', {
success: function() {
done();
}
before(function() {
// add test div to body for css tests
testEl = document.createElement('div');
testEl.className = 'test-div';
testEl.style.display = 'inline-block';
document.body.appendChild(testEl);
});
// execute done
loadjs.done('plugin1');
});
afterEach(function() {
var els = document.getElementsByTagName('link'),
i = els.length,
el;
it('should execute callbacks created after .done()', function(done) {
// execute done
loadjs.done('plugin2');
// iteratete through stylesheets
while (i--) {
el = els[i];
// add handler
loadjs.ready('plugin2', {
success: function() {
done();
// remove test stylesheets
if (el.href.indexOf('assets/file') != -1) {
el.parentNode.removeChild(el);
}
}
});
});
it('should define bundles', function(done) {
// define bundle
loadjs(['assets/file1.js', 'assets/file2.js'], 'bundle1');
it('should load one file', function(done) {
loadjs(['assets/file1.css'], {
success: function() {
assert.equal(testEl.offsetWidth, 100);
done();
}
});
});
it('should load multiple files', function(done) {
loadjs(['assets/file1.css', 'assets/file2.css'], {
success: function() {
assert.equal(testEl.offsetWidth, 200);
done();
}
});
});
it('should call fail callback on one invalid path', function(done) {
loadjs(['assets/file1.css', 'assets/file-doesntexist.css'], {
success: function() {
throw "Executed success callback";
},
fail: function(pathsNotFound) {
assert.equal(testEl.offsetWidth, 100);
assert.equal(pathsNotFound.length, 1);
assert.equal(pathsNotFound[0], 'assets/file-doesntexist.css');
done();
}
});
});
// use 1 second delay to let files load
setTimeout(function() {
loadjs.ready('bundle1', {
it('should support mix of css and js', function(done) {
loadjs(['assets/file1.css', 'assets/file1.js'], {
success: function() {
assert.equal(pathsLoaded['file1.js'], true);
assert.equal(pathsLoaded['file2.js'], true);
assert.equal(testEl.offsetWidth, 100);
done();
}
});
}, 1000);
});
});
it('should allow bundle callbacks before definitions', function(done) {
// define callback
loadjs.ready('bundle2', {
success: function() {
assert.equal(pathsLoaded['file1.js'], true);
assert.equal(pathsLoaded['file2.js'], true);
done();
}
it('should call fail callback on empty css', function(done) {
loadjs(['assets/emptycss'], {
success: function() {
throw "Executed success callback";
},
fail: function(pathsNotFound) {
assert.equal(pathsNotFound.length, 1);
done();
}
});
});
// use 1 second delay
setTimeout(function() {
loadjs(['assets/file1.js', 'assets/file2.js'], 'bundle2');
}, 1000);
// teardown
return after(function() {
// remove test div
testEl.parentNode.removeChild(testEl);
});
});
it('should support async false', function(done) {
var numCompleted = 0,
numTests = 20,
paths = ['assets/asyncfalse1.js', 'assets/asyncfalse2.js'];
// run tests sequentially
var testFn = function(paths) {
loadjs(paths, {
success: function() {
var f1 = paths[0].replace('assets/', '');
var f2 = paths[1].replace('assets/', '');
// ==========================================================================
// API tests
// ==========================================================================
// check load order
assert.isTrue(pathsLoaded[f1]);
assert.isFalse(pathsLoaded[f2]);
describe('API tests', function() {
// increment tests
numCompleted += 1;
it('should throw an error if bundle is already defined', function() {
// define bundle
loadjs(['assets/file1.js'], 'bundle3');
// define bundle again
var fn = function() {
loadjs(['assets/file1.js'], 'bundle3');
};
expect(fn).to.throw(Error, "LoadJS");
});
it('should create a bundle id and a callback inline', function(done) {
loadjs(['assets/file1.js', 'assets/file2.js'], 'bundle4', {
success: function() {
assert.equal(pathsLoaded['file1.js'], true);
assert.equal(pathsLoaded['file2.js'], true);
done();
}
});
});
if (numCompleted === numTests) {
// exit
done();
} else {
// reset register
pathsLoaded = {};
// run test again
paths.reverse();
testFn(paths);
it('should chain loadjs object', function(done) {
function bothDone() {
if (pathsLoaded['file1.js'] && pathsLoaded['file2.js']) done();
}
// define bundles
loadjs('assets/file1.js', 'bundle5');
loadjs('assets/file2.js', 'bundle6');
loadjs
.ready('bundle5', {
success: function() {
assert.equal(pathsLoaded['file1.js'], true);
bothDone();
}})
.ready('bundle6', {
success: function() {
assert.equal(pathsLoaded['file2.js'], true);
bothDone();
}
});
});
it('should handle multiple dependencies', function(done) {
loadjs('assets/file1.js', 'bundle7');
loadjs('assets/file2.js', 'bundle8');
loadjs.ready(['bundle7', 'bundle8'], {
success: function() {
assert.equal(pathsLoaded['file1.js'], true);
assert.equal(pathsLoaded['file2.js'], true);
done();
}
});
});
it('should fail on missing depdendencies', function(done) {
loadjs('assets/file1.js', 'bundle9');
loadjs('assets/file-doesntexist.js', 'bundle10');
loadjs.ready(['bundle9', 'bundle10'], {
success: function() {
throw "Executed success callback";
},
async: false
fail: function(depsNotFound) {
assert.equal(pathsLoaded['file1.js'], true);
assert.equal(depsNotFound.length, 1);
assert.equal(depsNotFound[0], 'bundle10');
done();
}
});
}
// run tests
testFn(paths);
});
// Un-'x' this for testing ad blocked scripts.
// Ghostery: Disallow "Google Adservices"
// AdBlock Plus: Add "www.googletagservices.com/tag/js/gpt.js" as a
// custom filter under Options
//
xit('it should report ad blocked scripts as missing', function(done) {
var blockedScript = 'https://www.googletagservices.com/tag/js/gpt.js';
loadjs([blockedScript, 'assets/file1.js'], {
success: function() {
throw "Executed success callback";
},
fail: function(pathsNotFound) {
assert.equal(pathsLoaded['file1.js'], true);
assert.equal(pathsNotFound.length, 1);
assert.equal(pathsNotFound[0], blockedScript);
done();
}
});
it('should execute callbacks on .done()', function(done) {
// add handler
loadjs.ready('plugin1', {
success: function() {
done();
}
});
// execute done
loadjs.done('plugin1');
});
it('should execute callbacks created after .done()', function(done) {
// execute done
loadjs.done('plugin2');
// add handler
loadjs.ready('plugin2', {
success: function() {
done();
}
});
});
it('should define bundles', function(done) {
// define bundle
loadjs(['assets/file1.js', 'assets/file2.js'], 'bundle1');
// use 1 second delay to let files load
setTimeout(function() {
loadjs.ready('bundle1', {
success: function() {
assert.equal(pathsLoaded['file1.js'], true);
assert.equal(pathsLoaded['file2.js'], true);
done();
}
});
}, 1000);
});
it('should allow bundle callbacks before definitions', function(done) {
// define callback
loadjs.ready('bundle2', {
success: function() {
assert.equal(pathsLoaded['file1.js'], true);
assert.equal(pathsLoaded['file2.js'], true);
done();
}
});
// use 1 second delay
setTimeout(function() {
loadjs(['assets/file1.js', 'assets/file2.js'], 'bundle2');
}, 1000);
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc