loadjs
Advanced tools
Comparing version 4.2.0 to 4.3.0-rc1
@@ -107,4 +107,5 @@ loadjs = (function () { | ||
pathname = path.replace(/[\?|#].*$/, ''), | ||
pathStripped = path.replace(/^(css|img)!/, ''), | ||
pathStripped = path.replace(/^(css|img|module|nomodule)!/, ''), | ||
isLegacyIECss, | ||
hasModuleSupport, | ||
e; | ||
@@ -136,4 +137,17 @@ | ||
e = doc.createElement('script'); | ||
e.src = path; | ||
e.src = pathStripped; | ||
e.async = async === undefined ? true : async; | ||
// handle es modules | ||
// modern browsers: | ||
// module: add to dom with type="module" | ||
// nomodule: call success() callback without adding to dom | ||
// legacy browsers: | ||
// module: call success() callback without adding to dom | ||
// nomodule: add to dom with default type ("text/javascript") | ||
hasModuleSupport = 'noModule' in e; | ||
if (/^module!/.test(pathname)) { | ||
if (!hasModuleSupport) return callbackFn(path, 'l'); | ||
e.type = "module"; | ||
} else if (/^nomodule!/.test(pathname) && hasModuleSupport) return callbackFn(path, 'l'); | ||
} | ||
@@ -140,0 +154,0 @@ |
@@ -1,1 +0,1 @@ | ||
loadjs=function(){var h=function(){},c={},u={},f={};function o(e,n){if(e){var r=f[e];if(u[e]=n,r)for(;r.length;)r[0](e,n),r.splice(0,1)}}function l(e,n){e.call&&(e={success:e}),n.length?(e.error||h)(n):(e.success||h)(e)}function d(r,t,s,i){var c,o,e=document,n=s.async,u=(s.numRetries||0)+1,f=s.before||h,l=r.replace(/[\?|#].*$/,""),a=r.replace(/^(css|img)!/,"");i=i||0,/(^css!|\.css$)/.test(l)?((o=e.createElement("link")).rel="stylesheet",o.href=a,(c="hideFocus"in o)&&o.relList&&(c=0,o.rel="preload",o.as="style")):/(^img!|\.(png|gif|jpg|svg|webp)$)/.test(l)?(o=e.createElement("img")).src=a:((o=e.createElement("script")).src=r,o.async=void 0===n||n),!(o.onload=o.onerror=o.onbeforeload=function(e){var n=e.type[0];if(c)try{o.sheet.cssText.length||(n="e")}catch(e){18!=e.code&&(n="e")}if("e"==n){if((i+=1)<u)return d(r,t,s,i)}else if("preload"==o.rel&&"style"==o.as)return o.rel="stylesheet";t(r,n,e.defaultPrevented)})!==f(r,o)&&e.head.appendChild(o)}function r(e,n,r){var t,s;if(n&&n.trim&&(t=n),s=(t?r:n)||{},t){if(t in c)throw"LoadJS";c[t]=!0}function i(n,r){!function(e,t,n){var r,s,i=(e=e.push?e:[e]).length,c=i,o=[];for(r=function(e,n,r){if("e"==n&&o.push(e),"b"==n){if(!r)return;o.push(e)}--i||t(o)},s=0;s<c;s++)d(e[s],r,n)}(e,function(e){l(s,e),n&&l({success:n,error:r},e),o(t,e)},s)}if(s.returnPromise)return new Promise(i);i()}return r.ready=function(e,n){return function(e,r){e=e.push?e:[e];var n,t,s,i=[],c=e.length,o=c;for(n=function(e,n){n.length&&i.push(e),--o||r(i)};c--;)t=e[c],(s=u[t])?n(t,s):(f[t]=f[t]||[]).push(n)}(e,function(e){l(n,e)}),r},r.done=function(e){o(e,[])},r.reset=function(){c={},u={},f={}},r.isDefined=function(e){return e in c},r}(); | ||
loadjs=function(){var h=function(){},o={},c={},f={};function u(e,n){if(e){var t=f[e];if(c[e]=n,t)for(;t.length;)t[0](e,n),t.splice(0,1)}}function l(e,n){e.call&&(e={success:e}),n.length?(e.error||h)(n):(e.success||h)(e)}function p(t,r,i,s){var o,e,u,n=document,c=i.async,f=(i.numRetries||0)+1,l=i.before||h,a=t.replace(/[\?|#].*$/,""),d=t.replace(/^(css|img|module|nomodule)!/,"");if(s=s||0,/(^css!|\.css$)/.test(a))(u=n.createElement("link")).rel="stylesheet",u.href=d,(o="hideFocus"in u)&&u.relList&&(o=0,u.rel="preload",u.as="style");else if(/(^img!|\.(png|gif|jpg|svg|webp)$)/.test(a))(u=n.createElement("img")).src=d;else if((u=n.createElement("script")).src=d,u.async=void 0===c||c,e="noModule"in u,/^module!/.test(a)){if(!e)return r(t,"l");u.type="module"}else if(/^nomodule!/.test(a)&&e)return r(t,"l");!(u.onload=u.onerror=u.onbeforeload=function(e){var n=e.type[0];if(o)try{u.sheet.cssText.length||(n="e")}catch(e){18!=e.code&&(n="e")}if("e"==n){if((s+=1)<f)return p(t,r,i,s)}else if("preload"==u.rel&&"style"==u.as)return u.rel="stylesheet";r(t,n,e.defaultPrevented)})!==l(t,u)&&n.head.appendChild(u)}function t(e,n,t){var r,i;if(n&&n.trim&&(r=n),i=(r?t:n)||{},r){if(r in o)throw"LoadJS";o[r]=!0}function s(n,t){!function(e,r,n){var t,i,s=(e=e.push?e:[e]).length,o=s,u=[];for(t=function(e,n,t){if("e"==n&&u.push(e),"b"==n){if(!t)return;u.push(e)}--s||r(u)},i=0;i<o;i++)p(e[i],t,n)}(e,function(e){l(i,e),n&&l({success:n,error:t},e),u(r,e)},i)}if(i.returnPromise)return new Promise(s);s()}return t.ready=function(e,n){return function(e,t){e=e.push?e:[e];var n,r,i,s=[],o=e.length,u=o;for(n=function(e,n){n.length&&s.push(e),--u||t(s)};o--;)r=e[o],(i=c[r])?n(r,i):(f[r]=f[r]||[]).push(n)}(e,function(e){l(n,e)}),t},t.done=function(e){u(e,[])},t.reset=function(){o={},c={},f={}},t.isDefined=function(e){return e in o},t}(); |
@@ -115,4 +115,5 @@ (function(root, factory) { | ||
pathname = path.replace(/[\?|#].*$/, ''), | ||
pathStripped = path.replace(/^(css|img)!/, ''), | ||
pathStripped = path.replace(/^(css|img|module|nomodule)!/, ''), | ||
isLegacyIECss, | ||
hasModuleSupport, | ||
e; | ||
@@ -144,4 +145,17 @@ | ||
e = doc.createElement('script'); | ||
e.src = path; | ||
e.src = pathStripped; | ||
e.async = async === undefined ? true : async; | ||
// handle es modules | ||
// modern browsers: | ||
// module: add to dom with type="module" | ||
// nomodule: call success() callback without adding to dom | ||
// legacy browsers: | ||
// module: call success() callback without adding to dom | ||
// nomodule: add to dom with default type ("text/javascript") | ||
hasModuleSupport = 'noModule' in e; | ||
if (/^module!/.test(pathname)) { | ||
if (!hasModuleSupport) return callbackFn(path, 'l'); | ||
e.type = "module"; | ||
} else if (/^nomodule!/.test(pathname) && hasModuleSupport) return callbackFn(path, 'l'); | ||
} | ||
@@ -148,0 +162,0 @@ |
@@ -107,4 +107,5 @@ loadjs = (function () { | ||
pathname = path.replace(/[\?|#].*$/, ''), | ||
pathStripped = path.replace(/^(css|img)!/, ''), | ||
pathStripped = path.replace(/^(css|img|module|nomodule)!/, ''), | ||
isLegacyIECss, | ||
hasModuleSupport, | ||
e; | ||
@@ -136,4 +137,17 @@ | ||
e = doc.createElement('script'); | ||
e.src = path; | ||
e.src = pathStripped; | ||
e.async = async === undefined ? true : async; | ||
// handle es modules | ||
// modern browsers: | ||
// module: add to dom with type="module" | ||
// nomodule: call success() callback without adding to dom | ||
// legacy browsers: | ||
// module: call success() callback without adding to dom | ||
// nomodule: add to dom with default type ("text/javascript") | ||
hasModuleSupport = 'noModule' in e; | ||
if (/^module!/.test(pathname)) { | ||
if (!hasModuleSupport) return callbackFn(path, 'l'); | ||
e.type = "module"; | ||
} else if (/^nomodule!/.test(pathname) && hasModuleSupport) return callbackFn(path, 'l'); | ||
} | ||
@@ -140,0 +154,0 @@ |
@@ -1,1 +0,1 @@ | ||
loadjs=function(){var h=function(){},c={},u={},f={};function o(e,n){if(e){var r=f[e];if(u[e]=n,r)for(;r.length;)r[0](e,n),r.splice(0,1)}}function l(e,n){e.call&&(e={success:e}),n.length?(e.error||h)(n):(e.success||h)(e)}function d(r,t,s,i){var c,o,e=document,n=s.async,u=(s.numRetries||0)+1,f=s.before||h,l=r.replace(/[\?|#].*$/,""),a=r.replace(/^(css|img)!/,"");i=i||0,/(^css!|\.css$)/.test(l)?((o=e.createElement("link")).rel="stylesheet",o.href=a,(c="hideFocus"in o)&&o.relList&&(c=0,o.rel="preload",o.as="style")):/(^img!|\.(png|gif|jpg|svg|webp)$)/.test(l)?(o=e.createElement("img")).src=a:((o=e.createElement("script")).src=r,o.async=void 0===n||n),!(o.onload=o.onerror=o.onbeforeload=function(e){var n=e.type[0];if(c)try{o.sheet.cssText.length||(n="e")}catch(e){18!=e.code&&(n="e")}if("e"==n){if((i+=1)<u)return d(r,t,s,i)}else if("preload"==o.rel&&"style"==o.as)return o.rel="stylesheet";t(r,n,e.defaultPrevented)})!==f(r,o)&&e.head.appendChild(o)}function r(e,n,r){var t,s;if(n&&n.trim&&(t=n),s=(t?r:n)||{},t){if(t in c)throw"LoadJS";c[t]=!0}function i(n,r){!function(e,t,n){var r,s,i=(e=e.push?e:[e]).length,c=i,o=[];for(r=function(e,n,r){if("e"==n&&o.push(e),"b"==n){if(!r)return;o.push(e)}--i||t(o)},s=0;s<c;s++)d(e[s],r,n)}(e,function(e){l(s,e),n&&l({success:n,error:r},e),o(t,e)},s)}if(s.returnPromise)return new Promise(i);i()}return r.ready=function(e,n){return function(e,r){e=e.push?e:[e];var n,t,s,i=[],c=e.length,o=c;for(n=function(e,n){n.length&&i.push(e),--o||r(i)};c--;)t=e[c],(s=u[t])?n(t,s):(f[t]=f[t]||[]).push(n)}(e,function(e){l(n,e)}),r},r.done=function(e){o(e,[])},r.reset=function(){c={},u={},f={}},r.isDefined=function(e){return e in c},r}(); | ||
loadjs=function(){var h=function(){},o={},c={},f={};function u(e,n){if(e){var t=f[e];if(c[e]=n,t)for(;t.length;)t[0](e,n),t.splice(0,1)}}function l(e,n){e.call&&(e={success:e}),n.length?(e.error||h)(n):(e.success||h)(e)}function p(t,r,i,s){var o,e,u,n=document,c=i.async,f=(i.numRetries||0)+1,l=i.before||h,a=t.replace(/[\?|#].*$/,""),d=t.replace(/^(css|img|module|nomodule)!/,"");if(s=s||0,/(^css!|\.css$)/.test(a))(u=n.createElement("link")).rel="stylesheet",u.href=d,(o="hideFocus"in u)&&u.relList&&(o=0,u.rel="preload",u.as="style");else if(/(^img!|\.(png|gif|jpg|svg|webp)$)/.test(a))(u=n.createElement("img")).src=d;else if((u=n.createElement("script")).src=d,u.async=void 0===c||c,e="noModule"in u,/^module!/.test(a)){if(!e)return r(t,"l");u.type="module"}else if(/^nomodule!/.test(a)&&e)return r(t,"l");!(u.onload=u.onerror=u.onbeforeload=function(e){var n=e.type[0];if(o)try{u.sheet.cssText.length||(n="e")}catch(e){18!=e.code&&(n="e")}if("e"==n){if((s+=1)<f)return p(t,r,i,s)}else if("preload"==u.rel&&"style"==u.as)return u.rel="stylesheet";r(t,n,e.defaultPrevented)})!==l(t,u)&&n.head.appendChild(u)}function t(e,n,t){var r,i;if(n&&n.trim&&(r=n),i=(r?t:n)||{},r){if(r in o)throw"LoadJS";o[r]=!0}function s(n,t){!function(e,r,n){var t,i,s=(e=e.push?e:[e]).length,o=s,u=[];for(t=function(e,n,t){if("e"==n&&u.push(e),"b"==n){if(!t)return;u.push(e)}--s||r(u)},i=0;i<o;i++)p(e[i],t,n)}(e,function(e){l(i,e),n&&l({success:n,error:t},e),u(r,e)},i)}if(i.returnPromise)return new Promise(s);s()}return t.ready=function(e,n){return function(e,t){e=e.push?e:[e];var n,r,i,s=[],o=e.length,u=o;for(n=function(e,n){n.length&&s.push(e),--u||t(s)};o--;)r=e[o],(i=c[r])?n(r,i):(f[r]=f[r]||[]).push(n)}(e,function(e){l(n,e)}),t},t.done=function(e){u(e,[])},t.reset=function(){o={},c={},f={}},t.isDefined=function(e){return e in o},t}(); |
{ | ||
"name": "loadjs", | ||
"version": "4.2.0", | ||
"version": "4.3.0-rc1", | ||
"license": "MIT", | ||
@@ -33,7 +33,7 @@ "description": "Tiny async loader for modern browsers", | ||
"gulp-jshint": "^2.1.0", | ||
"gulp-rename": "^1.4.0", | ||
"gulp-rename": "^2.0.0", | ||
"gulp-uglify": "^3.0.2", | ||
"gulp-umd": "^2.0.0", | ||
"jshint": "^2.10.2" | ||
"jshint": "^2.11.0" | ||
} | ||
} |
@@ -5,3 +5,3 @@ # LoadJS | ||
LoadJS is a tiny async loader for modern browsers (899 bytes). | ||
LoadJS is a tiny async loader for modern browsers (961 bytes). | ||
@@ -16,29 +16,35 @@ [![Dependency Status](https://david-dm.org/muicss/loadjs.svg)](https://david-dm.org/muicss/loadjs) | ||
LoadJS is based on the excellent [$script](https://github.com/ded/script.js) library by [Dustin Diaz](https://github.com/ded). 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 899 bytes (minified + gzipped). | ||
LoadJS is based on the excellent [$script](https://github.com/ded/script.js) library by [Dustin Diaz](https://github.com/ded). 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: | ||
```javascript | ||
// define a dependency bundle and execute code when it loads | ||
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar'); | ||
```html | ||
<script src="//unpkg.com/loadjs@latest/dist/loadjs.min.js"></script> | ||
<script> | ||
// define a dependency bundle and execute code when it loads | ||
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar'); | ||
loadjs.ready('foobar', function() { | ||
/* foo.js & bar.js loaded */ | ||
}); | ||
loadjs.ready('foobar', function() { | ||
/* foo.js & bar.js loaded */ | ||
}); | ||
</script> | ||
``` | ||
You can also use more advanced syntax for more options: | ||
```javascript | ||
// define a dependency bundle with advanced options | ||
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', { | ||
before: function(path, scriptEl) { /* execute code before fetch */ }, | ||
async: true, // load files synchronously or asynchronously (default: true) | ||
numRetries: 3 // see caveats about using numRetries with async:false (default: 0), | ||
returnPromise: false // return Promise object (default: false) | ||
}); | ||
```html | ||
<script src="//unpkg.com/loadjs@latest/dist/loadjs.min.js"></script> | ||
<script> | ||
// define a dependency bundle with advanced options | ||
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', { | ||
before: function(path, scriptEl) { /* execute code before fetch */ }, | ||
async: true, // load files synchronously or asynchronously (default: true) | ||
numRetries: 3 // see caveats about using numRetries with async:false (default: 0), | ||
returnPromise: false // return Promise object (default: false) | ||
}); | ||
loadjs.ready('foobar', { | ||
success: function() { /* foo.js & bar.js loaded */ }, | ||
error: function(depsNotFound) { /* foobar bundle load failed */ }, | ||
}); | ||
loadjs.ready('foobar', { | ||
success: function() { /* foo.js & bar.js loaded */ }, | ||
error: function(depsNotFound) { /* foobar bundle load failed */ }, | ||
}); | ||
</script> | ||
``` | ||
@@ -50,2 +56,11 @@ | ||
It's also available from these public CDNs: | ||
* UNPKG | ||
* [https://unpkg.com/loadjs@4.2.0/dist/loadjs.js](https://unpkg.com/loadjs@4.2.0/dist/loadjs.js) (for development) | ||
* [https://unpkg.com/loadjs@4.2.0/dist/loadjs.min.js](https://unpkg.com/loadjs@4.2.0/dist/loadjs.min.js) (for production) | ||
* CDNJS | ||
* [https://cdnjs.cloudflare.com/ajax/libs/loadjs/4.2.0/loadjs.js](https://cdnjs.cloudflare.com/ajax/libs/loadjs/4.2.0/loadjs.js) (for development) | ||
* [https://cdnjs.cloudflare.com/ajax/libs/loadjs/4.2.0/loadjs.min.js](https://cdnjs.cloudflare.com/ajax/libs/loadjs/4.2.0/loadjs.min.js) (for production) | ||
You can also use it as a CJS or AMD module: | ||
@@ -126,2 +141,19 @@ | ||
1. Load JavaScript files as modules with non-module fallbacks (in browsers without module support) | ||
```javascript | ||
loadjs(['module!/path/to/foo.js', 'nomodule!/path/to/bar.js'], function() { | ||
/* foo.js loaded with type="module" in browsers with module support, skipped silently in browsers without */ | ||
/* bar.js loaded with type="text/javascript" in browsers without module support, skipped silently in browsers with */ | ||
}); | ||
``` | ||
1. Load a JavaScript file (only in browsers without Module support) | ||
```javascript | ||
loadjs('nomodule!/path/to/foo.js', function() { | ||
/* foo.js loaded in browsers with module support, skipped in browsers without module support */ | ||
}); | ||
``` | ||
1. Add a bundle id | ||
@@ -128,0 +160,0 @@ |
@@ -106,4 +106,5 @@ /** | ||
pathname = path.replace(/[\?|#].*$/, ''), | ||
pathStripped = path.replace(/^(css|img)!/, ''), | ||
pathStripped = path.replace(/^(css|img|module|nomodule)!/, ''), | ||
isLegacyIECss, | ||
hasModuleSupport, | ||
e; | ||
@@ -135,4 +136,17 @@ | ||
e = doc.createElement('script'); | ||
e.src = path; | ||
e.src = pathStripped; | ||
e.async = async === undefined ? true : async; | ||
// handle es modules | ||
// modern browsers: | ||
// module: add to dom with type="module" | ||
// nomodule: call success() callback without adding to dom | ||
// legacy browsers: | ||
// module: call success() callback without adding to dom | ||
// nomodule: add to dom with default type ("text/javascript") | ||
hasModuleSupport = 'noModule' in e; | ||
if (/^module!/.test(pathname)) { | ||
if (!hasModuleSupport) return callbackFn(path, 'l'); | ||
e.type = "module"; | ||
} else if (/^nomodule!/.test(pathname) && hasModuleSupport) return callbackFn(path, 'l'); | ||
} | ||
@@ -139,0 +153,0 @@ |
@@ -107,4 +107,5 @@ loadjs = (function () { | ||
pathname = path.replace(/[\?|#].*$/, ''), | ||
pathStripped = path.replace(/^(css|img)!/, ''), | ||
pathStripped = path.replace(/^(css|img|module|nomodule)!/, ''), | ||
isLegacyIECss, | ||
hasModuleSupport, | ||
e; | ||
@@ -136,4 +137,17 @@ | ||
e = doc.createElement('script'); | ||
e.src = path; | ||
e.src = pathStripped; | ||
e.async = async === undefined ? true : async; | ||
// handle es modules | ||
// modern browsers: | ||
// module: add to dom with type="module" | ||
// nomodule: call success() callback without adding to dom | ||
// legacy browsers: | ||
// module: call success() callback without adding to dom | ||
// nomodule: add to dom with default type ("text/javascript") | ||
hasModuleSupport = 'noModule' in e; | ||
if (/^module!/.test(pathname)) { | ||
if (!hasModuleSupport) return callbackFn(path, 'l'); | ||
e.type = "module"; | ||
} else if (/^nomodule!/.test(pathname) && hasModuleSupport) return callbackFn(path, 'l'); | ||
} | ||
@@ -140,0 +154,0 @@ |
@@ -1,1 +0,1 @@ | ||
loadjs=function(){var h=function(){},c={},u={},f={};function o(e,n){if(e){var r=f[e];if(u[e]=n,r)for(;r.length;)r[0](e,n),r.splice(0,1)}}function l(e,n){e.call&&(e={success:e}),n.length?(e.error||h)(n):(e.success||h)(e)}function d(r,t,s,i){var c,o,e=document,n=s.async,u=(s.numRetries||0)+1,f=s.before||h,l=r.replace(/[\?|#].*$/,""),a=r.replace(/^(css|img)!/,"");i=i||0,/(^css!|\.css$)/.test(l)?((o=e.createElement("link")).rel="stylesheet",o.href=a,(c="hideFocus"in o)&&o.relList&&(c=0,o.rel="preload",o.as="style")):/(^img!|\.(png|gif|jpg|svg|webp)$)/.test(l)?(o=e.createElement("img")).src=a:((o=e.createElement("script")).src=r,o.async=void 0===n||n),!(o.onload=o.onerror=o.onbeforeload=function(e){var n=e.type[0];if(c)try{o.sheet.cssText.length||(n="e")}catch(e){18!=e.code&&(n="e")}if("e"==n){if((i+=1)<u)return d(r,t,s,i)}else if("preload"==o.rel&&"style"==o.as)return o.rel="stylesheet";t(r,n,e.defaultPrevented)})!==f(r,o)&&e.head.appendChild(o)}function r(e,n,r){var t,s;if(n&&n.trim&&(t=n),s=(t?r:n)||{},t){if(t in c)throw"LoadJS";c[t]=!0}function i(n,r){!function(e,t,n){var r,s,i=(e=e.push?e:[e]).length,c=i,o=[];for(r=function(e,n,r){if("e"==n&&o.push(e),"b"==n){if(!r)return;o.push(e)}--i||t(o)},s=0;s<c;s++)d(e[s],r,n)}(e,function(e){l(s,e),n&&l({success:n,error:r},e),o(t,e)},s)}if(s.returnPromise)return new Promise(i);i()}return r.ready=function(e,n){return function(e,r){e=e.push?e:[e];var n,t,s,i=[],c=e.length,o=c;for(n=function(e,n){n.length&&i.push(e),--o||r(i)};c--;)t=e[c],(s=u[t])?n(t,s):(f[t]=f[t]||[]).push(n)}(e,function(e){l(n,e)}),r},r.done=function(e){o(e,[])},r.reset=function(){c={},u={},f={}},r.isDefined=function(e){return e in c},r}(); | ||
loadjs=function(){var h=function(){},o={},c={},f={};function u(e,n){if(e){var t=f[e];if(c[e]=n,t)for(;t.length;)t[0](e,n),t.splice(0,1)}}function l(e,n){e.call&&(e={success:e}),n.length?(e.error||h)(n):(e.success||h)(e)}function p(t,r,i,s){var o,e,u,n=document,c=i.async,f=(i.numRetries||0)+1,l=i.before||h,a=t.replace(/[\?|#].*$/,""),d=t.replace(/^(css|img|module|nomodule)!/,"");if(s=s||0,/(^css!|\.css$)/.test(a))(u=n.createElement("link")).rel="stylesheet",u.href=d,(o="hideFocus"in u)&&u.relList&&(o=0,u.rel="preload",u.as="style");else if(/(^img!|\.(png|gif|jpg|svg|webp)$)/.test(a))(u=n.createElement("img")).src=d;else if((u=n.createElement("script")).src=d,u.async=void 0===c||c,e="noModule"in u,/^module!/.test(a)){if(!e)return r(t,"l");u.type="module"}else if(/^nomodule!/.test(a)&&e)return r(t,"l");!(u.onload=u.onerror=u.onbeforeload=function(e){var n=e.type[0];if(o)try{u.sheet.cssText.length||(n="e")}catch(e){18!=e.code&&(n="e")}if("e"==n){if((s+=1)<f)return p(t,r,i,s)}else if("preload"==u.rel&&"style"==u.as)return u.rel="stylesheet";r(t,n,e.defaultPrevented)})!==l(t,u)&&n.head.appendChild(u)}function t(e,n,t){var r,i;if(n&&n.trim&&(r=n),i=(r?t:n)||{},r){if(r in o)throw"LoadJS";o[r]=!0}function s(n,t){!function(e,r,n){var t,i,s=(e=e.push?e:[e]).length,o=s,u=[];for(t=function(e,n,t){if("e"==n&&u.push(e),"b"==n){if(!t)return;u.push(e)}--s||r(u)},i=0;i<o;i++)p(e[i],t,n)}(e,function(e){l(i,e),n&&l({success:n,error:t},e),u(r,e)},i)}if(i.returnPromise)return new Promise(s);s()}return t.ready=function(e,n){return function(e,t){e=e.push?e:[e];var n,r,i,s=[],o=e.length,u=o;for(n=function(e,n){n.length&&s.push(e),--u||t(s)};o--;)r=e[o],(i=c[r])?n(r,i):(f[r]=f[r]||[]).push(n)}(e,function(e){l(n,e)}),t},t.done=function(e){u(e,[])},t.reset=function(){o={},c={},f={}},t.isDefined=function(e){return e in o},t}(); |
@@ -7,10 +7,10 @@ /** | ||
var pathsLoaded = null, // file register | ||
testEl = null, | ||
assert = chai.assert, | ||
expect = chai.expect; | ||
testEl = null, | ||
assert = chai.assert, | ||
expect = chai.expect; | ||
describe('LoadJS tests', function() { | ||
describe('LoadJS tests', function () { | ||
beforeEach(function() { | ||
beforeEach(function () { | ||
// reset register | ||
@@ -27,7 +27,7 @@ pathsLoaded = {}; | ||
describe('JavaScript file loading tests', function() { | ||
describe('JavaScript file loading tests', function () { | ||
it('should call success callback on valid path', function(done) { | ||
it('should call success callback on valid path', function (done) { | ||
loadjs(['assets/file1.js'], { | ||
success: function() { | ||
success: function () { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
@@ -40,8 +40,8 @@ done(); | ||
it('should call error callback on invalid path', function(done) { | ||
it('should call error callback on invalid path', function (done) { | ||
loadjs(['assets/file-doesntexist.js'], { | ||
success: function() { | ||
success: function () { | ||
throw "Executed success callback"; | ||
}, | ||
error: function(pathsNotFound) { | ||
error: function (pathsNotFound) { | ||
assert.equal(pathsNotFound.length, 1); | ||
@@ -55,7 +55,7 @@ assert.equal(pathsNotFound[0], 'assets/file-doesntexist.js'); | ||
it('should call before callback before embedding into document', function(done) { | ||
it('should call before callback before embedding into document', function (done) { | ||
var scriptTags = []; | ||
loadjs(['assets/file1.js', 'assets/file2.js'], { | ||
before: function(path, el) { | ||
before: function (path, el) { | ||
scriptTags.push({ | ||
@@ -71,3 +71,3 @@ path: path, | ||
}, | ||
success: function() { | ||
success: function () { | ||
assert.equal(scriptTags[0].path, 'assets/file1.js'); | ||
@@ -85,5 +85,5 @@ assert.equal(scriptTags[1].path, 'assets/file2.js'); | ||
it('should bypass insertion if before returns `false`', function(done) { | ||
it('should bypass insertion if before returns `false`', function (done) { | ||
loadjs(['assets/file1.js'], { | ||
before: function(path, el) { | ||
before: function (path, el) { | ||
// append to body (instead of head) | ||
@@ -95,10 +95,10 @@ document.body.appendChild(el); | ||
}, | ||
success: function() { | ||
success: function () { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
// verify that file was added to body | ||
var els = document.body.querySelectorAll('script'), | ||
el; | ||
el; | ||
for (var i=0; i < els.length; i++) { | ||
for (var i = 0; i < els.length; i++) { | ||
el = els[i]; | ||
@@ -112,5 +112,5 @@ if (el.src.indexOf('assets/file1.js') !== -1) done(); | ||
it('should call success callback on two valid paths', function(done) { | ||
it('should call success callback on two valid paths', function (done) { | ||
loadjs(['assets/file1.js', 'assets/file2.js'], { | ||
success: function() { | ||
success: function () { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
@@ -124,8 +124,8 @@ assert.equal(pathsLoaded['file2.js'], true); | ||
it('should call error callback on one invalid path', function(done) { | ||
it('should call error callback on one invalid path', function (done) { | ||
loadjs(['assets/file1.js', 'assets/file-doesntexist.js'], { | ||
success: function() { | ||
success: function () { | ||
throw "Executed success callback"; | ||
}, | ||
error: function(pathsNotFound) { | ||
error: function (pathsNotFound) { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
@@ -140,11 +140,11 @@ assert.equal(pathsNotFound.length, 1); | ||
it('should support async false', function(done) { | ||
it('should support async false', function (done) { | ||
this.timeout(5000); | ||
var numCompleted = 0, | ||
numTests = 20, | ||
paths = ['assets/asyncfalse1.js', 'assets/asyncfalse2.js']; | ||
numTests = 20, | ||
paths = ['assets/asyncfalse1.js', 'assets/asyncfalse2.js']; | ||
// run tests sequentially | ||
var testFn = function(paths) { | ||
var testFn = function (paths) { | ||
// add cache busters | ||
@@ -156,3 +156,3 @@ var pathsUncached = paths.slice(0); | ||
loadjs(pathsUncached, { | ||
success: function() { | ||
success: function () { | ||
var f1 = paths[0].replace('assets/', ''); | ||
@@ -189,8 +189,8 @@ var f2 = paths[1].replace('assets/', ''); | ||
it('should support multiple tries', function(done) { | ||
it('should support multiple tries', function (done) { | ||
loadjs('assets/file-numretries.js', { | ||
error: function() { | ||
error: function () { | ||
// check number of scripts in document | ||
var selector = 'script[src="assets/file-numretries.js"]', | ||
scripts = document.querySelectorAll(selector); | ||
scripts = document.querySelectorAll(selector); | ||
if (scripts.length === 2) done(); | ||
@@ -202,3 +202,78 @@ }, | ||
// tests for browsers with/without module support | ||
if (caniuseModule()) { | ||
describe('module tests for browsers with module support', function () { | ||
it('should support loading modules with "module!" modifier', function (done) { | ||
loadjs(['module!assets/module1.js'], { | ||
success: function () { | ||
assert.equal(pathsLoaded['module1.js'], true); | ||
done(); | ||
} | ||
}); | ||
}); | ||
it('should call error callback on invalid module! path', function (done) { | ||
loadjs(['module!assets/module-doesntexist.js'], { | ||
success: function () { | ||
throw "Executed success callback"; | ||
}, | ||
error: function (pathsNotFound) { | ||
assert.equal(pathsNotFound.length, 1); | ||
assert.equal(pathsNotFound[0], 'module!assets/module-doesntexist.js'); | ||
done(); | ||
} | ||
}); | ||
}); | ||
it('should support bypassing loading files with "nomodule!" modifier', function (done) { | ||
loadjs(['nomodule!assets/file1.js'], { | ||
success: function () { | ||
assert.equal(pathsLoaded['file1.js'], undefined); | ||
done(); | ||
} | ||
}); | ||
}); | ||
}); | ||
} else { | ||
describe('module tests for browsers without module support', function () { | ||
it('should support loading modules with "nomodule!" modifier', function (done) { | ||
loadjs(['nomodule!assets/file1.js'], { | ||
success: function () { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
done(); | ||
} | ||
}); | ||
}); | ||
it('should call error callback on invalid nomodule! path', function (done) { | ||
loadjs(['nomodule!assets/file-doesntexist.js'], { | ||
success: function () { | ||
throw "Executed success callback"; | ||
}, | ||
error: function (pathsNotFound) { | ||
assert.equal(pathsNotFound.length, 1); | ||
assert.equal(pathsNotFound[0], 'nomodule!assets/file-doesntexist.js'); | ||
done(); | ||
} | ||
}); | ||
}); | ||
it('should support bypassing loading files with "module!" modifier', function (done) { | ||
loadjs(['module!assets/module1.js'], { | ||
success: function () { | ||
assert.equal(pathsLoaded['module1.js'], undefined); | ||
done(); | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
// Un-'x' this for testing ad blocked scripts. | ||
@@ -209,11 +284,11 @@ // Ghostery: Disallow "Google Adservices" | ||
// | ||
xit('it should report ad blocked scripts as missing', function(done) { | ||
xit('it should report ad blocked scripts as missing', function (done) { | ||
var s1 = 'https://www.googletagservices.com/tag/js/gpt.js', | ||
s2 = 'https://munchkin.marketo.net/munchkin-beta.js'; | ||
s2 = 'https://munchkin.marketo.net/munchkin-beta.js'; | ||
loadjs([s1, s2, 'assets/file1.js'], { | ||
success: function() { | ||
success: function () { | ||
throw new Error('Executed success callback'); | ||
}, | ||
error: function(pathsNotFound) { | ||
error: function (pathsNotFound) { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
@@ -234,5 +309,5 @@ assert.equal(pathsNotFound.length, 2); | ||
describe('CSS file loading tests', function() { | ||
describe('CSS file loading tests', function () { | ||
before(function() { | ||
before(function () { | ||
// add test div to body for css tests | ||
@@ -246,6 +321,6 @@ testEl = document.createElement('div'); | ||
afterEach(function() { | ||
afterEach(function () { | ||
var els = document.getElementsByTagName('link'), | ||
i = els.length, | ||
el; | ||
i = els.length, | ||
el; | ||
@@ -264,5 +339,5 @@ // iteratete through stylesheets | ||
it('should load one file', function(done) { | ||
it('should load one file', function (done) { | ||
loadjs(['assets/file1.css'], { | ||
success: function() { | ||
success: function () { | ||
assert.equal(testEl.offsetWidth, 100); | ||
@@ -275,5 +350,5 @@ done(); | ||
it('should load multiple files', function(done) { | ||
it('should load multiple files', function (done) { | ||
loadjs(['assets/file1.css', 'assets/file2.css'], { | ||
success: function() { | ||
success: function () { | ||
assert.equal(testEl.offsetWidth, 200); | ||
@@ -286,8 +361,8 @@ done(); | ||
it('should call error callback on one invalid path', function(done) { | ||
it('should call error callback on one invalid path', function (done) { | ||
loadjs(['assets/file1.css', 'assets/file-doesntexist.css'], { | ||
success: function() { | ||
success: function () { | ||
throw new Error('Executed success callback'); | ||
}, | ||
error: function(pathsNotFound) { | ||
error: function (pathsNotFound) { | ||
assert.equal(testEl.offsetWidth, 100); | ||
@@ -302,5 +377,5 @@ assert.equal(pathsNotFound.length, 1); | ||
it('should support mix of css and js', function(done) { | ||
it('should support mix of css and js', function (done) { | ||
loadjs(['assets/file1.css', 'assets/file1.js'], { | ||
success: function() { | ||
success: function () { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
@@ -314,9 +389,9 @@ assert.equal(testEl.offsetWidth, 100); | ||
it('should support forced "css!" files', function(done) { | ||
it('should support forced "css!" files', function (done) { | ||
loadjs(['css!assets/file1.css'], { | ||
success: function() { | ||
success: function () { | ||
// loop through files | ||
var els = document.getElementsByTagName('link'), | ||
i = els.length, | ||
el; | ||
i = els.length, | ||
el; | ||
@@ -331,5 +406,5 @@ while (i--) { | ||
it('supports urls with query arguments', function(done) { | ||
it('supports urls with query arguments', function (done) { | ||
loadjs(['assets/file1.css?x=x'], { | ||
success: function() { | ||
success: function () { | ||
assert.equal(testEl.offsetWidth, 100); | ||
@@ -342,5 +417,5 @@ done(); | ||
it('supports urls with anchor tags', function(done) { | ||
it('supports urls with anchor tags', function (done) { | ||
loadjs(['assets/file1.css#anchortag'], { | ||
success: function() { | ||
success: function () { | ||
assert.equal(testEl.offsetWidth, 100); | ||
@@ -353,5 +428,5 @@ done(); | ||
it('supports urls with query arguments and anchor tags', function(done) { | ||
it('supports urls with query arguments and anchor tags', function (done) { | ||
loadjs(['assets/file1.css?x=x#anchortag'], { | ||
success: function() { | ||
success: function () { | ||
assert.equal(testEl.offsetWidth, 100); | ||
@@ -363,8 +438,8 @@ done(); | ||
it('should load external css files', function(done) { | ||
it('should load external css files', function (done) { | ||
this.timeout(0); | ||
loadjs(['//cdn.muicss.com/mui-0.6.8/css/mui.min.css'], { | ||
success: function() { | ||
success: function () { | ||
var styleObj = getComputedStyle(testEl); | ||
@@ -379,10 +454,10 @@ | ||
it('should call error on missing external file', function(done) { | ||
it('should call error on missing external file', function (done) { | ||
this.timeout(0); | ||
loadjs(['//cdn.muicss.com/mui-0.6.8/css/mui-doesnotexist.min.css'], { | ||
success: function() { | ||
success: function () { | ||
throw new Error('Executed success callback'); | ||
}, | ||
error: function(pathsNotFound) { | ||
error: function (pathsNotFound) { | ||
var styleObj = getComputedStyle(testEl); | ||
@@ -397,5 +472,4 @@ | ||
// teardown | ||
return after(function() { | ||
return after(function () { | ||
// remove test div | ||
@@ -406,3 +480,2 @@ testEl.parentNode.removeChild(testEl); | ||
// ========================================================================== | ||
@@ -412,3 +485,3 @@ // Image file loading tests | ||
describe('Image file loading tests', function() { | ||
describe('Image file loading tests', function () { | ||
@@ -419,3 +492,3 @@ function assertLoaded(src) { | ||
Array.prototype.slice.call(imgs).forEach(function(img) { | ||
Array.prototype.slice.call(imgs).forEach(function (img) { | ||
// verify image was loaded | ||
@@ -426,3 +499,3 @@ if (img.src === src) assert.equal(img.naturalWidth > 0, true); | ||
function assertNotLoaded(src) { | ||
@@ -432,3 +505,3 @@ // loop through images | ||
Array.prototype.slice.call(imgs).forEach(function(img) { | ||
Array.prototype.slice.call(imgs).forEach(function (img) { | ||
// fail if image was loaded | ||
@@ -440,6 +513,6 @@ if (img.src === src) assert.equal(img.naturalWidth, 0); | ||
it('should load one file', function(done) { | ||
it('should load one file', function (done) { | ||
loadjs(['assets/flash.png'], { | ||
success: function() { | ||
assertLoaded('assets/flash.png'); | ||
success: function () { | ||
assertLoaded('assets/flash.png'); | ||
done(); | ||
@@ -451,6 +524,6 @@ } | ||
it('should load multiple files', function(done) { | ||
it('should load multiple files', function (done) { | ||
loadjs(['assets/flash.png', 'assets/flash.jpg'], { | ||
success: function() { | ||
assertLoaded('assets/flash.png'); | ||
success: function () { | ||
assertLoaded('assets/flash.png'); | ||
assertLoaded('assets/flash.jpg'); | ||
@@ -463,3 +536,3 @@ done(); | ||
it('detects png|gif|jpg|svg|webp extensions', function(done) { | ||
it('detects png|gif|jpg|svg extensions', function (done) { | ||
let files = [ | ||
@@ -469,8 +542,9 @@ 'assets/flash.png', | ||
'assets/flash.jpg', | ||
'assets/flash.svg', | ||
'assets/flash.webp' | ||
'assets/flash.svg' | ||
]; | ||
loadjs(files, function() { | ||
files.forEach(file => {assertLoaded(file);}); | ||
loadjs(files, function () { | ||
files.forEach(function (file) { | ||
assertLoaded(file); | ||
}); | ||
done(); | ||
@@ -480,8 +554,8 @@ }); | ||
it('supports urls with query arguments', function(done) { | ||
it('supports urls with query arguments', function (done) { | ||
var src = 'assets/flash.png?' + Math.random(); | ||
loadjs([src], { | ||
success: function() { | ||
success: function () { | ||
assertLoaded(src); | ||
@@ -494,7 +568,7 @@ done(); | ||
it('supports urls with anchor tags', function(done) { | ||
it('supports urls with anchor tags', function (done) { | ||
var src = 'assets/flash.png#' + Math.random(); | ||
loadjs([src], { | ||
success: function() { | ||
success: function () { | ||
assertLoaded(src); | ||
@@ -507,3 +581,3 @@ done(); | ||
it('supports urls with query arguments and anchor tags', function(done) { | ||
it('supports urls with query arguments and anchor tags', function (done) { | ||
var src = 'assets/flash.png'; | ||
@@ -514,3 +588,3 @@ src += '?' + Math.random(); | ||
loadjs([src], { | ||
success: function() { | ||
success: function () { | ||
assertLoaded(src); | ||
@@ -521,9 +595,9 @@ done(); | ||
}); | ||
it('should support forced "img!" files', function(done) { | ||
it('should support forced "img!" files', function (done) { | ||
var src = 'assets/flash.png?' + Math.random(); | ||
loadjs(['img!' + src], { | ||
success: function() { | ||
success: function () { | ||
assertLoaded(src); | ||
@@ -536,11 +610,11 @@ done(); | ||
it('should call error callback on one invalid path', function(done) { | ||
it('should call error callback on one invalid path', function (done) { | ||
var src1 = 'assets/flash.png?' + Math.random(), | ||
src2 = 'assets/flash-doesntexist.png?' + Math.random(); | ||
src2 = 'assets/flash-doesntexist.png?' + Math.random(); | ||
loadjs(['img!' + src1, 'img!' + src2], { | ||
success: function() { | ||
success: function () { | ||
throw new Error('Executed success callback'); | ||
}, | ||
error: function(pathsNotFound) { | ||
error: function (pathsNotFound) { | ||
assert.equal(pathsNotFound.length, 1); | ||
@@ -555,7 +629,7 @@ assertLoaded(src1); | ||
it('should support mix of img and js', function(done) { | ||
it('should support mix of img and js', function (done) { | ||
var src = 'assets/flash.png?' + Math.random(); | ||
loadjs(['img!' + src, 'assets/file1.js'], { | ||
success: function() { | ||
success: function () { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
@@ -569,3 +643,3 @@ assertLoaded(src); | ||
it('should load external img files', function(done) { | ||
it('should load external img files', function (done) { | ||
this.timeout(0); | ||
@@ -577,3 +651,3 @@ | ||
loadjs(['img!' + src], { | ||
success: function() { | ||
success: function () { | ||
assertLoaded(src); | ||
@@ -586,3 +660,3 @@ done(); | ||
it('should call error on missing external file', function(done) { | ||
it('should call error on missing external file', function (done) { | ||
this.timeout(0); | ||
@@ -592,8 +666,8 @@ | ||
src += 'mui-logo-doesntexist.png?' + Math.random(); | ||
loadjs(['img!' + src], { | ||
success: function() { | ||
success: function () { | ||
throw new Error('Executed success callback'); | ||
}, | ||
error: function(pathsNotFound) { | ||
error: function (pathsNotFound) { | ||
assertNotLoaded(src); | ||
@@ -604,2 +678,29 @@ done(); | ||
}); | ||
if (caniuseWebp()) { | ||
describe('tests for browsers with webp support', function () { | ||
it('detects webp extensions', function (done) { | ||
loadjs('assets/flash.webp', function () { | ||
assertLoaded('assets/flash.webp'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
} else { | ||
describe('tests for browsers without webp support', function () { | ||
it('executes error callback when browser loads webp file', function (done) { | ||
loadjs('assets/flash.webp', { | ||
error: function (pathsNotFound) { | ||
assertNotLoaded('assets/flash.webp'); | ||
done(); | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
}); | ||
@@ -612,5 +713,5 @@ | ||
describe('API tests', function() { | ||
describe('API tests', function () { | ||
it('should throw an error if bundle is already defined', function() { | ||
it('should throw an error if bundle is already defined', function () { | ||
// define bundle | ||
@@ -620,3 +721,3 @@ loadjs(['assets/file1.js'], 'bundle'); | ||
// define bundle again | ||
var fn = function() { | ||
var fn = function () { | ||
loadjs(['assets/file1.js'], 'bundle'); | ||
@@ -629,5 +730,5 @@ }; | ||
it('should create a bundle id and a callback inline', function(done) { | ||
it('should create a bundle id and a callback inline', function (done) { | ||
loadjs(['assets/file1.js', 'assets/file2.js'], 'bundle', { | ||
success: function() { | ||
success: function () { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
@@ -641,3 +742,3 @@ assert.equal(pathsLoaded['file2.js'], true); | ||
it('should chain loadjs object', function(done) { | ||
it('should chain loadjs object', function (done) { | ||
function bothDone() { | ||
@@ -653,8 +754,9 @@ if (pathsLoaded['file1.js'] && pathsLoaded['file2.js']) done(); | ||
.ready('bundle1', { | ||
success: function() { | ||
success: function () { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
bothDone(); | ||
}}) | ||
} | ||
}) | ||
.ready('bundle2', { | ||
success: function() { | ||
success: function () { | ||
assert.equal(pathsLoaded['file2.js'], true); | ||
@@ -667,3 +769,3 @@ bothDone(); | ||
it('should handle multiple dependencies', function(done) { | ||
it('should handle multiple dependencies', function (done) { | ||
loadjs('assets/file1.js', 'bundle1'); | ||
@@ -673,3 +775,3 @@ loadjs('assets/file2.js', 'bundle2'); | ||
loadjs.ready(['bundle1', 'bundle2'], { | ||
success: function() { | ||
success: function () { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
@@ -683,3 +785,3 @@ assert.equal(pathsLoaded['file2.js'], true); | ||
it('should error on missing depdendencies', function(done) { | ||
it('should error on missing depdendencies', function (done) { | ||
loadjs('assets/file1.js', 'bundle1'); | ||
@@ -689,6 +791,6 @@ loadjs('assets/file-doesntexist.js', 'bundle2'); | ||
loadjs.ready(['bundle1', 'bundle2'], { | ||
success: function() { | ||
success: function () { | ||
throw "Executed success callback"; | ||
}, | ||
error: function(depsNotFound) { | ||
error: function (depsNotFound) { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
@@ -703,6 +805,6 @@ assert.equal(depsNotFound.length, 1); | ||
it('should execute callbacks on .done()', function(done) { | ||
it('should execute callbacks on .done()', function (done) { | ||
// add handler | ||
loadjs.ready('plugin', { | ||
success: function() { | ||
success: function () { | ||
done(); | ||
@@ -717,3 +819,3 @@ } | ||
it('should execute callbacks created after .done()', function(done) { | ||
it('should execute callbacks created after .done()', function (done) { | ||
// execute done | ||
@@ -724,3 +826,3 @@ loadjs.done('plugin'); | ||
loadjs.ready('plugin', { | ||
success: function() { | ||
success: function () { | ||
done(); | ||
@@ -732,3 +834,3 @@ } | ||
it('should define bundles', function(done) { | ||
it('should define bundles', function (done) { | ||
// define bundle | ||
@@ -738,5 +840,5 @@ loadjs(['assets/file1.js', 'assets/file2.js'], 'bundle'); | ||
// use 1 second delay to let files load | ||
setTimeout(function() { | ||
setTimeout(function () { | ||
loadjs.ready('bundle', { | ||
success: function() { | ||
success: function () { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
@@ -751,6 +853,6 @@ assert.equal(pathsLoaded['file2.js'], true); | ||
it('should allow bundle callbacks before definitions', function(done) { | ||
it('should allow bundle callbacks before definitions', function (done) { | ||
// define callback | ||
loadjs.ready('bundle', { | ||
success: function() { | ||
success: function () { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
@@ -763,3 +865,3 @@ assert.equal(pathsLoaded['file2.js'], true); | ||
// use 1 second delay | ||
setTimeout(function() { | ||
setTimeout(function () { | ||
loadjs(['assets/file1.js', 'assets/file2.js'], 'bundle'); | ||
@@ -770,3 +872,3 @@ }, 1000); | ||
it('should reset dependencies statuses', function() { | ||
it('should reset dependencies statuses', function () { | ||
loadjs(['assets/file1.js'], 'cleared'); | ||
@@ -776,3 +878,3 @@ loadjs.reset(); | ||
// define bundle again | ||
var fn = function() { | ||
var fn = function () { | ||
loadjs(['assets/file1.js'], 'cleared'); | ||
@@ -785,3 +887,3 @@ }; | ||
it('should indicate if bundle has already been defined', function() { | ||
it('should indicate if bundle has already been defined', function () { | ||
loadjs(['assets/file1/js'], 'bundle1'); | ||
@@ -794,4 +896,4 @@ | ||
it('should accept success callback functions to loadjs()', function(done) { | ||
loadjs('assets/file1.js', function() { | ||
it('should accept success callback functions to loadjs()', function (done) { | ||
loadjs('assets/file1.js', function () { | ||
done(); | ||
@@ -802,5 +904,5 @@ }); | ||
it('should accept success callback functions to .ready()', function(done) { | ||
it('should accept success callback functions to .ready()', function (done) { | ||
loadjs.done('plugin'); | ||
loadjs.ready('plugin', function() { | ||
loadjs.ready('plugin', function () { | ||
done(); | ||
@@ -811,77 +913,106 @@ }); | ||
it('should return Promise object if returnPromise is true', function() { | ||
var prom = loadjs(['assets/file1.js'], {returnPromise: true}); | ||
if (caniusePromise()) { | ||
describe('tests for browsers with Promise support', function () { | ||
// verify that response object is a Promise | ||
assert.equal(prom instanceof Promise, true); | ||
}); | ||
it('should return Promise object if returnPromise is true', function () { | ||
var prom = loadjs(['assets/file1.js'], { returnPromise: true }); | ||
// verify that response object is a Promise | ||
assert.equal(prom instanceof Promise, true); | ||
}); | ||
it('Promise object should support resolutions', function(done) { | ||
var prom = loadjs(['assets/file1.js'], {returnPromise: true}); | ||
prom.then(function() { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
done(); | ||
}); | ||
}); | ||
it('Promise object should support resolutions', function (done) { | ||
var prom = loadjs(['assets/file1.js'], { returnPromise: true }); | ||
prom.then(function () { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
done(); | ||
}); | ||
}); | ||
it('Promise object should support rejections', function(done) { | ||
var prom = loadjs(['assets/file-doesntexist.js'], {returnPromise: true}); | ||
prom.then( | ||
function(){}, | ||
function(pathsNotFound) { | ||
assert.equal(pathsNotFound.length, 1); | ||
assert.equal(pathsNotFound[0], 'assets/file-doesntexist.js'); | ||
done(); | ||
} | ||
); | ||
}); | ||
it('Promise object should support rejections', function (done) { | ||
var prom = loadjs(['assets/file-doesntexist.js'], { returnPromise: true }); | ||
prom.then( | ||
function () { }, | ||
function (pathsNotFound) { | ||
assert.equal(pathsNotFound.length, 1); | ||
assert.equal(pathsNotFound[0], 'assets/file-doesntexist.js'); | ||
done(); | ||
} | ||
); | ||
}); | ||
it('Promise object should support catches', function(done) { | ||
var prom = loadjs(['assets/file-doesntexist.js'], {returnPromise: true}); | ||
prom | ||
.catch(function(pathsNotFound) { | ||
assert.equal(pathsNotFound.length, 1); | ||
assert.equal(pathsNotFound[0], 'assets/file-doesntexist.js'); | ||
done(); | ||
it('Promise object should support catches', function (done) { | ||
var prom = loadjs(['assets/file-doesntexist.js'], { returnPromise: true }); | ||
prom | ||
.catch(function (pathsNotFound) { | ||
assert.equal(pathsNotFound.length, 1); | ||
assert.equal(pathsNotFound[0], 'assets/file-doesntexist.js'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('supports Promises and success callbacks', function(done) { | ||
var numCompleted = 0; | ||
it('supports Promises and success callbacks', function (done) { | ||
var numCompleted = 0; | ||
function completedFn() { | ||
numCompleted += 1; | ||
if (numCompleted === 2) done(); | ||
}; | ||
var prom = loadjs('assets/file1.js', { | ||
success: completedFn, | ||
returnPromise: true | ||
}); | ||
function completedFn() { | ||
numCompleted += 1; | ||
if (numCompleted === 2) done(); | ||
}; | ||
prom.then(completedFn); | ||
}); | ||
var prom = loadjs('assets/file1.js', { | ||
success: completedFn, | ||
returnPromise: true | ||
}); | ||
prom.then(completedFn); | ||
}); | ||
it('supports Promises and bundle ready events', function(done) { | ||
var numCompleted = 0; | ||
function completedFn() { | ||
numCompleted += 1; | ||
if (numCompleted === 2) done(); | ||
}; | ||
loadjs('assets/file1.js', 'bundle1', {returnPromise: true}) | ||
.then(completedFn); | ||
it('supports Promises and bundle ready events', function (done) { | ||
var numCompleted = 0; | ||
loadjs.ready('bundle1', completedFn); | ||
}); | ||
function completedFn() { | ||
numCompleted += 1; | ||
if (numCompleted === 2) done(); | ||
}; | ||
loadjs('assets/file1.js', 'bundle1', { returnPromise: true }) | ||
.then(completedFn); | ||
loadjs.ready('bundle1', completedFn); | ||
}); | ||
}); | ||
} | ||
}); | ||
}); | ||
// ========================================================================== | ||
// Utilities | ||
// ========================================================================== | ||
// https://stackoverflow.com/questions/5573096/detecting-webp-support | ||
function caniuseWebp() { | ||
var elem = document.createElement('canvas'); | ||
if (!!(elem.getContext && elem.getContext('2d'))) { | ||
return elem.toDataURL('image/webp').indexOf('data:image/webp') == 0; | ||
} else { | ||
return false; | ||
} | ||
} | ||
function caniuseModule() { | ||
return 'noModule' in document.createElement('script'); | ||
} | ||
function caniusePromise() { | ||
return typeof Promise !== "undefined"; | ||
} |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances in 1 package
57
25599
416
955319
1
22