- Linting (ESLint): Remove now unneeded config and add per new update

- Refactoring: Prefer for-of, event.key (newly enforced linting)
- Refactoring: Better var. names
- npm: Update devDeps and update local copies
This commit is contained in:
Brett Zamir
2019-05-24 20:17:17 +08:00
parent f6b5360a58
commit 4177146acd
38 changed files with 2450 additions and 1476 deletions

File diff suppressed because one or more lines are too long

View File

@@ -19,7 +19,7 @@ function toAbsoluteURL (url) {
/**
* Add any of the whitelisted attributes to the script tag.
* @param {HTMLScriptElement} script
* @param {PlainObject.<string, string>} atts
* @param {PlainObject<string, string>} atts
* @returns {void}
*/
function addScriptAtts (script, atts) {
@@ -36,15 +36,14 @@ function addScriptAtts (script, atts) {
* @property {string} global The variable name to set on `window` (when not using the modular version)
* @property {boolean} [returnDefault=false]
*/
/* eslint-disable jsdoc/check-types */
/**
* @function module:importModule.importSetGlobalDefault
* @param {string|string[]} url
* @param {string|GenericArray<Any>} url
* @param {module:importModule.ImportConfig} config
* @returns {Promise<*>} The value to which it resolves depends on the export of the targeted module.
* @returns {Promise<Any>} The value to which it resolves depends on the export of the targeted module.
*/
export function importSetGlobalDefault (url, config) {
/* eslint-enable jsdoc/check-types */
return importSetGlobal(url, {...config, returnDefault: true});
}
/**
@@ -113,18 +112,16 @@ export function importScript (url, atts = {}) {
});
}
/* eslint-disable jsdoc/check-types */
/**
*
* @param {string|string[]} url
* @param {PlainObject} [atts={}]
* @param {PlainObject} opts
* @param {boolean} [opts.returnDefault=false} = {}]
* @returns {Promise<*>} Resolves to value of loading module or rejects with
* @returns {Promise<Any>} Resolves to value of loading module or rejects with
* `Error` upon a script loading error.
*/
export function importModule (url, atts = {}, {returnDefault = false} = {}) {
/* eslint-enable jsdoc/check-types */
if (Array.isArray(url)) {
return Promise.all(url.map((u) => {
return importModule(u, atts);

File diff suppressed because it is too large Load Diff

View File

@@ -1,87 +1,154 @@
function loadStylesheets(stylesheets, {
before: beforeDefault, after: afterDefault, favicon: faviconDefault,
canvas: canvasDefault, image: imageDefault = true,
acceptErrors
} = {}) {
stylesheets = Array.isArray(stylesheets) ? stylesheets : [stylesheets];
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
}
function setupLink(stylesheetURL) {
let options = {};
if (Array.isArray(stylesheetURL)) {
[stylesheetURL, options = {}] = stylesheetURL;
}
let { favicon = faviconDefault } = options;
const {
before = beforeDefault,
after = afterDefault,
canvas = canvasDefault,
image = imageDefault
} = options;
function addLink() {
if (before) {
before.before(link);
} else if (after) {
after.after(link);
} else {
document.head.appendChild(link);
}
}
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
const link = document.createElement('link');
return new Promise((resolve, reject) => {
let rej = reject;
if (acceptErrors) {
rej = typeof acceptErrors === 'function' ? error => {
acceptErrors({ error, stylesheetURL, options, resolve, reject });
} : resolve;
}
if (stylesheetURL.endsWith('.css')) {
favicon = false;
} else if (stylesheetURL.endsWith('.ico')) {
favicon = true;
}
if (favicon) {
link.rel = 'shortcut icon';
link.type = 'image/x-icon';
function _iterableToArrayLimit(arr, i) {
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
if (image === false) {
link.href = stylesheetURL;
addLink();
resolve(link);
return;
}
try {
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
const cnv = document.createElement('canvas');
cnv.width = 16;
cnv.height = 16;
const context = cnv.getContext('2d');
const img = document.createElement('img');
img.addEventListener('error', error => {
reject(error);
});
img.addEventListener('load', () => {
context.drawImage(img, 0, 0);
link.href = canvas ? cnv.toDataURL('image/x-icon') : stylesheetURL;
addLink();
resolve(link);
});
img.src = stylesheetURL;
return;
}
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = stylesheetURL;
addLink();
link.addEventListener('error', error => {
rej(error);
});
link.addEventListener('load', () => {
resolve(link);
});
});
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
function loadStylesheets(stylesheets) {
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
beforeDefault = _ref.before,
afterDefault = _ref.after,
faviconDefault = _ref.favicon,
canvasDefault = _ref.canvas,
_ref$image = _ref.image,
imageDefault = _ref$image === void 0 ? true : _ref$image,
acceptErrors = _ref.acceptErrors;
stylesheets = Array.isArray(stylesheets) ? stylesheets : [stylesheets];
function setupLink(stylesheetURL) {
var options = {};
if (Array.isArray(stylesheetURL)) {
var _stylesheetURL = stylesheetURL;
var _stylesheetURL2 = _slicedToArray(_stylesheetURL, 2);
stylesheetURL = _stylesheetURL2[0];
var _stylesheetURL2$ = _stylesheetURL2[1];
options = _stylesheetURL2$ === void 0 ? {} : _stylesheetURL2$;
}
return Promise.all(stylesheets.map(setupLink));
var _options = options,
_options$favicon = _options.favicon,
favicon = _options$favicon === void 0 ? faviconDefault : _options$favicon;
var _options2 = options,
_options2$before = _options2.before,
before = _options2$before === void 0 ? beforeDefault : _options2$before,
_options2$after = _options2.after,
after = _options2$after === void 0 ? afterDefault : _options2$after,
_options2$canvas = _options2.canvas,
canvas = _options2$canvas === void 0 ? canvasDefault : _options2$canvas,
_options2$image = _options2.image,
image = _options2$image === void 0 ? imageDefault : _options2$image;
function addLink() {
if (before) {
before.before(link);
} else if (after) {
after.after(link);
} else {
document.head.appendChild(link);
}
}
var link = document.createElement('link');
return new Promise(function (resolve, reject) {
var rej = reject;
if (acceptErrors) {
rej = typeof acceptErrors === 'function' ? function (error) {
acceptErrors({
error: error,
stylesheetURL: stylesheetURL,
options: options,
resolve: resolve,
reject: reject
});
} : resolve;
}
if (stylesheetURL.endsWith('.css')) {
favicon = false;
} else if (stylesheetURL.endsWith('.ico')) {
favicon = true;
}
if (favicon) {
link.rel = 'shortcut icon';
link.type = 'image/x-icon';
if (image === false) {
link.href = stylesheetURL;
addLink();
resolve(link);
return;
}
var cnv = document.createElement('canvas');
cnv.width = 16;
cnv.height = 16;
var context = cnv.getContext('2d');
var img = document.createElement('img');
img.addEventListener('error', function (error) {
reject(error);
});
img.addEventListener('load', function () {
context.drawImage(img, 0, 0);
link.href = canvas ? cnv.toDataURL('image/x-icon') : stylesheetURL;
addLink();
resolve(link);
});
img.src = stylesheetURL;
return;
}
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = stylesheetURL;
addLink();
link.addEventListener('error', function (error) {
rej(error);
});
link.addEventListener('load', function () {
resolve(link);
});
});
}
return Promise.all(stylesheets.map(setupLink));
}
export default loadStylesheets;