dopant 6.0.1 → 6.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -9
- package/dist/index.cjs +31 -33
- package/package.json +6 -5
- package/src/index.js +40 -42
package/README.md
CHANGED
|
@@ -28,15 +28,21 @@ import dopant from 'dopant';
|
|
|
28
28
|
await dopant(
|
|
29
29
|
'/assets/css/layout.css',
|
|
30
30
|
'/assets/js/main.js',
|
|
31
|
-
[
|
|
32
|
-
|
|
31
|
+
[
|
|
32
|
+
'/assets/js/importmap.json',
|
|
33
|
+
{ type: 'importmap' },
|
|
34
|
+
],
|
|
35
|
+
[
|
|
36
|
+
'/assets/js/module.js',
|
|
37
|
+
{ defer: true, type: 'module' },
|
|
38
|
+
],
|
|
33
39
|
[
|
|
34
40
|
'/assets/webfonts/font.woff2',
|
|
35
41
|
{
|
|
36
42
|
as: 'font',
|
|
37
43
|
rel: 'preload',
|
|
38
44
|
type: 'font/woff2',
|
|
39
|
-
}
|
|
45
|
+
},
|
|
40
46
|
],
|
|
41
47
|
);
|
|
42
48
|
```
|
|
@@ -45,15 +51,15 @@ await dopant(
|
|
|
45
51
|
|
|
46
52
|
#### `dopant(...resources)`
|
|
47
53
|
|
|
48
|
-
* `...resources` **{string | [string,
|
|
49
|
-
* **Returns:** Promise that resolves to a list of resolutions
|
|
54
|
+
* `...resources` **{string | [string, Object]}** Resource URL, w/wo extra attributes.
|
|
55
|
+
* **Returns:** Promise that resolves to a list of resolutions.
|
|
50
56
|
|
|
51
57
|
### Behavior
|
|
52
58
|
|
|
53
|
-
* CSS files default to `rel="stylesheet"
|
|
54
|
-
* If `rel` is provided, a `<link>` element is created
|
|
55
|
-
* Otherwise, a `<script>` element is created
|
|
56
|
-
* Scripts default to `async: true` (unless overridden or `defer: true` is set)
|
|
59
|
+
* CSS files default to `rel="stylesheet"`.
|
|
60
|
+
* If `rel` is provided, a `<link>` element is created.
|
|
61
|
+
* Otherwise, a `<script>` element is created.
|
|
62
|
+
* Scripts default to `async: true` (unless overridden or `defer: true` is set).
|
|
57
63
|
|
|
58
64
|
---
|
|
59
65
|
|
package/dist/index.cjs
CHANGED
|
@@ -4,38 +4,36 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
var _default = (...args) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
var _default = (...args) => Promise.allSettled(args.map(async it => {
|
|
8
|
+
const [url, attrs = {}] = Array.isArray(it) ? it : [it];
|
|
9
|
+
if (url?.constructor !== String || attrs?.constructor !== Object) {
|
|
10
|
+
throw new TypeError('Invalid input');
|
|
11
|
+
}
|
|
12
|
+
const isLink = attrs.rel || /\.\bcss\b/i.test(url);
|
|
13
|
+
const el = document.createElement(isLink ? 'link' : 'script');
|
|
14
|
+
if (/\bimportmap\b|\bspeculationrules\b/i.test(attrs.type)) {
|
|
15
|
+
const res = await fetch(url);
|
|
16
|
+
if (!res.ok) {
|
|
17
|
+
throw new Error(`Failed to fetch ${attrs.type}: ${url}`);
|
|
12
18
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
});
|
|
34
|
-
return new Promise((res, rej) => {
|
|
35
|
-
el.onerror = ev => (ev.target.remove(), rej(ev.target));
|
|
36
|
-
el.onload = ev => res(ev.target);
|
|
37
|
-
document.head.append(el);
|
|
38
|
-
});
|
|
39
|
-
}));
|
|
40
|
-
};
|
|
19
|
+
el.textContent = await res.text();
|
|
20
|
+
el.type = attrs.type;
|
|
21
|
+
document.head.append(el);
|
|
22
|
+
return el;
|
|
23
|
+
}
|
|
24
|
+
Object.assign(el, isLink ? {
|
|
25
|
+
...attrs,
|
|
26
|
+
href: url,
|
|
27
|
+
rel: attrs.rel || 'stylesheet'
|
|
28
|
+
} : {
|
|
29
|
+
...attrs,
|
|
30
|
+
async: attrs.async ?? !attrs.defer,
|
|
31
|
+
src: url
|
|
32
|
+
});
|
|
33
|
+
return new Promise((res, rej) => {
|
|
34
|
+
el.onerror = ev => (ev.target.remove(), rej(ev.target));
|
|
35
|
+
el.onload = ev => res(ev.target);
|
|
36
|
+
document.head.append(el);
|
|
37
|
+
});
|
|
38
|
+
}));
|
|
41
39
|
exports.default = _default;
|
package/package.json
CHANGED
|
@@ -12,9 +12,10 @@
|
|
|
12
12
|
"@babel/cli": "^7.28.6",
|
|
13
13
|
"@babel/core": "^7.29.0",
|
|
14
14
|
"@babel/preset-env": "^7.29.0",
|
|
15
|
+
"@eslint/markdown": "^7.5.1",
|
|
15
16
|
"c8": "^10.1.3",
|
|
16
|
-
"eslint": "^10.0.
|
|
17
|
-
"eslint-config-ultra-refined": "^4.
|
|
17
|
+
"eslint": "^10.0.1",
|
|
18
|
+
"eslint-config-ultra-refined": "^4.1.2",
|
|
18
19
|
"mocha": "^11.7.5",
|
|
19
20
|
"playwright-chromium": "^1.58.2"
|
|
20
21
|
},
|
|
@@ -42,14 +43,14 @@
|
|
|
42
43
|
"url": "git+https://github.com/bricss/dopant.git"
|
|
43
44
|
},
|
|
44
45
|
"scripts": {
|
|
45
|
-
"build": "rm -rf dist && npx babel src --out-dir dist --out-file-extension .cjs",
|
|
46
|
+
"build": "rm -rf dist && npx babel src --out-dir dist --out-file-extension .cjs && sh misc.sh",
|
|
46
47
|
"lint": "eslint --concurrency=auto",
|
|
47
|
-
"prepack": "npm run build &&
|
|
48
|
+
"prepack": "npm run build && npm run lint",
|
|
48
49
|
"pretest": "rm -rf coverage",
|
|
49
50
|
"test": "mocha",
|
|
50
51
|
"test:bail": "mocha --bail",
|
|
51
52
|
"test:cover": "npm test && c8 report --reporter=lcov --reporter=text"
|
|
52
53
|
},
|
|
53
54
|
"type": "module",
|
|
54
|
-
"version": "6.0.
|
|
55
|
+
"version": "6.0.2"
|
|
55
56
|
}
|
package/src/index.js
CHANGED
|
@@ -1,49 +1,47 @@
|
|
|
1
|
-
export default (...args) =>
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const [url, attrs = {}] = Array.isArray(it) ? it : [it];
|
|
1
|
+
export default (...args) => Promise.allSettled(
|
|
2
|
+
args.map(async (it) => {
|
|
3
|
+
const [url, attrs = {}] = Array.isArray(it) ? it : [it];
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
if (url?.constructor !== String || attrs?.constructor !== Object) {
|
|
6
|
+
throw new TypeError('Invalid input');
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const isLink = attrs.rel || /\.\bcss\b/i.test(url);
|
|
10
|
+
const el = document.createElement(isLink ? 'link' : 'script');
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
+
if (/\bimportmap\b|\bspeculationrules\b/i.test(attrs.type)) {
|
|
13
|
+
const res = await fetch(url);
|
|
12
14
|
|
|
13
|
-
if (
|
|
14
|
-
|
|
15
|
+
if (!res.ok) {
|
|
16
|
+
throw new Error(`Failed to fetch ${ attrs.type }: ${ url }`);
|
|
17
|
+
}
|
|
15
18
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
el.textContent = await res.text();
|
|
20
|
+
el.type = attrs.type;
|
|
21
|
+
document.head.append(el);
|
|
19
22
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
document.head.append(el);
|
|
23
|
+
return el;
|
|
24
|
+
}
|
|
23
25
|
|
|
24
|
-
|
|
26
|
+
Object.assign(
|
|
27
|
+
el,
|
|
28
|
+
isLink
|
|
29
|
+
? {
|
|
30
|
+
...attrs,
|
|
31
|
+
href: url,
|
|
32
|
+
rel: attrs.rel || 'stylesheet',
|
|
25
33
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
);
|
|
41
|
-
|
|
42
|
-
return new Promise((res, rej) => {
|
|
43
|
-
el.onerror = (ev) => (ev.target.remove(), rej(ev.target));
|
|
44
|
-
el.onload = (ev) => res(ev.target);
|
|
45
|
-
document.head.append(el);
|
|
46
|
-
});
|
|
47
|
-
}),
|
|
48
|
-
);
|
|
49
|
-
};
|
|
34
|
+
: {
|
|
35
|
+
...attrs,
|
|
36
|
+
async: attrs.async ?? !attrs.defer,
|
|
37
|
+
src: url,
|
|
38
|
+
},
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
return new Promise((res, rej) => {
|
|
42
|
+
el.onerror = (ev) => (ev.target.remove(), rej(ev.target));
|
|
43
|
+
el.onload = (ev) => res(ev.target);
|
|
44
|
+
document.head.append(el);
|
|
45
|
+
});
|
|
46
|
+
}),
|
|
47
|
+
);
|