Skip to content

Commit 009c832

Browse files
committed
refactor: prepare basic-loader for new features
1 parent 8dbb9e6 commit 009c832

4 files changed

Lines changed: 236 additions & 70 deletions

File tree

lib/basic-loader-amd.js

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,27 @@ define(["exports"], function (_exports) {
66
});
77
_exports.default = void 0;
88

9+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
10+
11+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
12+
13+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
14+
15+
var BODY = 'body';
16+
var HEAD = 'head';
17+
918
var _load = function _load(tag) {
1019
// attributes example: { 'data-test': 'new-attribute-here' }
1120
return function (url) {
1221
var attributes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
13-
// This promise will be used by Promise.all to determine success or failure
22+
// this promise will be used by Promise.all to determine success or failure
1423
return new Promise(function (resolve, reject) {
24+
// need to set different attributes depending on tag type
25+
var _tagToTagDetailsFuncs = tagToTagDetailsFuncs[tag](url, attributes),
26+
parent = _tagToTagDetailsFuncs.parent,
27+
tagAttributes = _tagToTagDetailsFuncs.attributes;
28+
1529
var element = document.createElement(tag);
16-
var parent = 'body';
17-
var attr = 'src'; // Important success and error for the promise
1830

1931
element.onload = function () {
2032
return resolve(url);
@@ -23,31 +35,54 @@ define(["exports"], function (_exports) {
2335
element.onerror = function () {
2436
return reject(url);
2537
}; // maybe we should remove the broken node, who knows
26-
// Need to set different attributes depending on tag type
2738

2839

29-
switch (tag) {
30-
case 'script':
31-
element.async = true;
32-
break;
33-
34-
case 'link':
35-
element.type = 'text/css';
36-
element.rel = 'stylesheet';
37-
attr = 'href';
38-
parent = 'head';
39-
}
40-
41-
element[attr] = url;
42-
Object.keys(attributes).forEach(function (name) {
43-
return element.setAttribute(name, attributes[name]);
44-
}); // Inject into document to kick off loading
40+
Object.keys(tagAttributes).forEach(function (name) {
41+
return element.setAttribute(name, tagAttributes[name]);
42+
}); // inject into document to kick off loading
4543

4644
document[parent].appendChild(element);
4745
});
4846
};
49-
}; // exporting a "default" would render the amd package to work differently
47+
};
5048

49+
var getScriptTagDetails = function getScriptTagDetails(url, attributes) {
50+
var hasNoAsyncOrDefer = !('async' in attributes || 'defer' in attributes);
51+
return {
52+
parent: BODY,
53+
attributes: _objectSpread(_objectSpread({
54+
src: url
55+
}, hasNoAsyncOrDefer && {
56+
async: ''
57+
}), attributes)
58+
};
59+
};
60+
61+
var getLinkTagDetails = function getLinkTagDetails(url, attributes) {
62+
return {
63+
parent: HEAD,
64+
attributes: _objectSpread({
65+
href: url,
66+
type: 'text/css',
67+
rel: 'stylesheet'
68+
}, attributes)
69+
};
70+
};
71+
72+
var getImgTagDetails = function getImgTagDetails(url, attributes) {
73+
return {
74+
parent: BODY,
75+
attributes: _objectSpread({
76+
src: url
77+
}, attributes)
78+
};
79+
};
80+
81+
var tagToTagDetailsFuncs = {
82+
script: getScriptTagDetails,
83+
link: getLinkTagDetails,
84+
img: getImgTagDetails
85+
}; // exporting a "default" would render the amd package to work differently
5186

5287
var _default = {
5388
css: _load('link'),

lib/basic-loader.js

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,27 @@ Object.defineProperty(exports, "__esModule", {
55
});
66
exports.default = void 0;
77

8+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
9+
10+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
11+
12+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
13+
14+
var BODY = 'body';
15+
var HEAD = 'head';
16+
817
var _load = function _load(tag) {
918
// attributes example: { 'data-test': 'new-attribute-here' }
1019
return function (url) {
1120
var attributes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
12-
// This promise will be used by Promise.all to determine success or failure
21+
// this promise will be used by Promise.all to determine success or failure
1322
return new Promise(function (resolve, reject) {
23+
// need to set different attributes depending on tag type
24+
var _tagToTagDetailsFuncs = tagToTagDetailsFuncs[tag](url, attributes),
25+
parent = _tagToTagDetailsFuncs.parent,
26+
tagAttributes = _tagToTagDetailsFuncs.attributes;
27+
1428
var element = document.createElement(tag);
15-
var parent = 'body';
16-
var attr = 'src'; // Important success and error for the promise
1729

1830
element.onload = function () {
1931
return resolve(url);
@@ -22,31 +34,54 @@ var _load = function _load(tag) {
2234
element.onerror = function () {
2335
return reject(url);
2436
}; // maybe we should remove the broken node, who knows
25-
// Need to set different attributes depending on tag type
2637

2738

28-
switch (tag) {
29-
case 'script':
30-
element.async = true;
31-
break;
32-
33-
case 'link':
34-
element.type = 'text/css';
35-
element.rel = 'stylesheet';
36-
attr = 'href';
37-
parent = 'head';
38-
}
39-
40-
element[attr] = url;
41-
Object.keys(attributes).forEach(function (name) {
42-
return element.setAttribute(name, attributes[name]);
43-
}); // Inject into document to kick off loading
39+
Object.keys(tagAttributes).forEach(function (name) {
40+
return element.setAttribute(name, tagAttributes[name]);
41+
}); // inject into document to kick off loading
4442

4543
document[parent].appendChild(element);
4644
});
4745
};
48-
}; // exporting a "default" would render the amd package to work differently
46+
};
4947

48+
var getScriptTagDetails = function getScriptTagDetails(url, attributes) {
49+
var hasNoAsyncOrDefer = !('async' in attributes || 'defer' in attributes);
50+
return {
51+
parent: BODY,
52+
attributes: _objectSpread(_objectSpread({
53+
src: url
54+
}, hasNoAsyncOrDefer && {
55+
async: ''
56+
}), attributes)
57+
};
58+
};
59+
60+
var getLinkTagDetails = function getLinkTagDetails(url, attributes) {
61+
return {
62+
parent: HEAD,
63+
attributes: _objectSpread({
64+
href: url,
65+
type: 'text/css',
66+
rel: 'stylesheet'
67+
}, attributes)
68+
};
69+
};
70+
71+
var getImgTagDetails = function getImgTagDetails(url, attributes) {
72+
return {
73+
parent: BODY,
74+
attributes: _objectSpread({
75+
src: url
76+
}, attributes)
77+
};
78+
};
79+
80+
var tagToTagDetailsFuncs = {
81+
script: getScriptTagDetails,
82+
link: getLinkTagDetails,
83+
img: getImgTagDetails
84+
}; // exporting a "default" would render the amd package to work differently
5085

5186
var _default = {
5287
css: _load('link'),

src/basic-loader.js

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,63 @@
1+
const BODY = 'body';
2+
const HEAD = 'head';
3+
14
const _load = (tag) => {
25
// attributes example: { 'data-test': 'new-attribute-here' }
36
return (url, attributes = {}) => {
4-
// This promise will be used by Promise.all to determine success or failure
7+
// this promise will be used by Promise.all to determine success or failure
58
return new Promise((resolve, reject) => {
9+
// need to set different attributes depending on tag type
10+
const { parent, attributes: tagAttributes } = tagToTagDetailsFuncs[tag](url, attributes);
611
const element = document.createElement(tag);
7-
let parent = 'body';
8-
let attr = 'src';
9-
10-
// Important success and error for the promise
1112
element.onload = () => resolve(url);
1213
element.onerror = () => reject(url); // maybe we should remove the broken node, who knows
14+
Object.keys(tagAttributes).forEach((name) => element.setAttribute(name, tagAttributes[name]));
15+
// inject into document to kick off loading
16+
document[parent].appendChild(element);
17+
});
18+
};
19+
};
1320

14-
// Need to set different attributes depending on tag type
15-
switch (tag) {
16-
case 'script':
17-
element.async = true;
18-
break;
19-
case 'link':
20-
element.type = 'text/css';
21-
element.rel = 'stylesheet';
22-
attr = 'href';
23-
parent = 'head';
24-
}
21+
const getScriptTagDetails = (url, attributes) => {
22+
const hasNoAsyncOrDefer = !('async' in attributes || 'defer' in attributes);
23+
return {
24+
parent: BODY,
25+
attributes: {
26+
src: url,
27+
...(hasNoAsyncOrDefer && { async: '' }),
28+
...attributes,
29+
},
30+
};
31+
};
2532

26-
element[attr] = url;
27-
Object.keys(attributes).forEach((name) => element.setAttribute(name, attributes[name]));
33+
const getLinkTagDetails = (url, attributes) => {
34+
return {
35+
parent: HEAD,
36+
attributes: {
37+
href: url,
38+
type: 'text/css',
39+
rel: 'stylesheet',
40+
...attributes,
41+
},
42+
};
43+
};
2844

29-
// Inject into document to kick off loading
30-
document[parent].appendChild(element);
31-
});
45+
const getImgTagDetails = (url, attributes) => {
46+
return {
47+
parent: BODY,
48+
attributes: {
49+
src: url,
50+
...attributes,
51+
},
3252
};
3353
};
3454

55+
const tagToTagDetailsFuncs = {
56+
script: getScriptTagDetails,
57+
link: getLinkTagDetails,
58+
img: getImgTagDetails,
59+
};
60+
3561
export default {
3662
css: _load('link'),
3763
js: _load('script'),

0 commit comments

Comments
 (0)