@bigcommerce/script-loader
Advanced tools
Comparing version 0.1.6 to 0.2.0
@@ -5,2 +5,13 @@ # Change Log | ||
<a name="0.2.0"></a> | ||
# [0.2.0](https://github.com/bigcommerce/script-loader-js/compare/v0.1.6...v0.2.0) (2019-09-20) | ||
### Features | ||
* **common:** CHECKOUT-4400 Add StylesheetLoader for loading stylesheets ([06fd92b](https://github.com/bigcommerce/script-loader-js/commit/06fd92b)) | ||
* **common:** CHECKOUT-4400 Load multiple scripts in parallel and run them in series ([4d140a6](https://github.com/bigcommerce/script-loader-js/commit/4d140a6)) | ||
<a name="0.1.6"></a> | ||
@@ -7,0 +18,0 @@ ## [0.1.6](https://github.com/bigcommerce/script-loader-js/compare/v0.1.5...v0.1.6) (2018-08-23) |
export { default as ScriptLoader } from './script-loader'; | ||
export { default as createScriptLoader } from './create-script-loader'; | ||
export { default as getScriptLoader } from './get-script-loader'; | ||
export { default as StylesheetLoader } from './stylesheet-loader'; | ||
export { default as createStylesheetLoader } from './create-stylesheet-loader'; | ||
export { default as getStylesheetLoader } from './get-stylesheet-loader'; |
@@ -9,2 +9,8 @@ "use strict"; | ||
exports.getScriptLoader = get_script_loader_1.default; | ||
var stylesheet_loader_1 = require("./stylesheet-loader"); | ||
exports.StylesheetLoader = stylesheet_loader_1.default; | ||
var create_stylesheet_loader_1 = require("./create-stylesheet-loader"); | ||
exports.createStylesheetLoader = create_stylesheet_loader_1.default; | ||
var get_stylesheet_loader_1 = require("./get-stylesheet-loader"); | ||
exports.getStylesheetLoader = get_stylesheet_loader_1.default; | ||
//# sourceMappingURL=index.js.map |
export default class ScriptLoader { | ||
private _scripts; | ||
private _preloadedScripts; | ||
loadScript(src: string): Promise<Event>; | ||
loadScripts(urls: string[]): Promise<Event[]>; | ||
preloadScript(url: string): Promise<Event>; | ||
preloadScripts(urls: string[]): Promise<Event[]>; | ||
} |
@@ -6,2 +6,3 @@ "use strict"; | ||
this._scripts = {}; | ||
this._preloadedScripts = {}; | ||
} | ||
@@ -26,2 +27,48 @@ ScriptLoader.prototype.loadScript = function (src) { | ||
}; | ||
ScriptLoader.prototype.loadScripts = function (urls) { | ||
var _this = this; | ||
var events = []; | ||
var promise; | ||
return this.preloadScripts(urls) | ||
.then(function () { | ||
urls.forEach(function (url) { | ||
if (promise) { | ||
promise = promise.then(function () { return _this.loadScript(url); }); | ||
} | ||
else { | ||
promise = _this.loadScript(url); | ||
} | ||
promise = promise.then(function (event) { | ||
events.push(event); | ||
return event; | ||
}); | ||
}); | ||
return promise; | ||
}) | ||
.then(function () { return events; }); | ||
}; | ||
ScriptLoader.prototype.preloadScript = function (url) { | ||
var _this = this; | ||
if (!this._preloadedScripts[url]) { | ||
this._preloadedScripts[url] = new Promise(function (resolve, reject) { | ||
var preloadedScript = document.createElement('link'); | ||
preloadedScript.as = 'script'; | ||
preloadedScript.rel = 'preload'; | ||
preloadedScript.href = url; | ||
preloadedScript.onload = function (event) { | ||
resolve(event); | ||
}; | ||
preloadedScript.onerror = function (event) { | ||
delete _this._preloadedScripts[url]; | ||
reject(event); | ||
}; | ||
document.head.appendChild(preloadedScript); | ||
}); | ||
} | ||
return this._preloadedScripts[url]; | ||
}; | ||
ScriptLoader.prototype.preloadScripts = function (urls) { | ||
var _this = this; | ||
return Promise.all(urls.map(function (url) { return _this.preloadScript(url); })); | ||
}; | ||
return ScriptLoader; | ||
@@ -28,0 +75,0 @@ }()); |
{ | ||
"name": "@bigcommerce/script-loader", | ||
"version": "0.1.6", | ||
"version": "0.2.0", | ||
"description": "A library for loading JavaScript files asynchronously", | ||
@@ -35,3 +35,3 @@ "license": "MIT", | ||
"dependencies": { | ||
"tslib": "^1.8.0" | ||
"tslib": "^1.10.0" | ||
}, | ||
@@ -46,5 +46,5 @@ "devDependencies": { | ||
"ts-jest": "^21.2.3", | ||
"tslint": "^5.9.1", | ||
"typescript": "^2.7.1" | ||
"tslint": "^5.18.0", | ||
"typescript": "^3.5.3" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24525
25
190
Updatedtslib@^1.10.0