nbhi 0.6.16 → 0.6.17
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/javascript/index.js +51 -43
- package/package.json +1 -1
package/javascript/index.js
CHANGED
|
@@ -7,7 +7,7 @@ export { initialize, update, validate, onceVisible };
|
|
|
7
7
|
|
|
8
8
|
async function initialize({ prefix = 'db', source = '/components' } = {}) {
|
|
9
9
|
const registerCache = new Set();
|
|
10
|
-
const fileCache = new
|
|
10
|
+
const fileCache = new Map();
|
|
11
11
|
|
|
12
12
|
if (typeof prefix !== 'string' || prefix === '') {
|
|
13
13
|
throw new Error('prefix option needs to be a string of at least one character');
|
|
@@ -34,54 +34,62 @@ async function initialize({ prefix = 'db', source = '/components' } = {}) {
|
|
|
34
34
|
});
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
function fromFile(root) {
|
|
38
|
-
const
|
|
37
|
+
function fromFile(root, ancestors = []) {
|
|
38
|
+
const names = [...root.querySelectorAll(':not(:defined)')]
|
|
39
39
|
.filter(element => {
|
|
40
40
|
const tagName = element.localName;
|
|
41
41
|
return element.nodeType === 1 && tagName.includes('-') && tagName.startsWith(prefix);
|
|
42
42
|
})
|
|
43
|
-
.map(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
function copyScript(template) {
|
|
56
|
-
const script = template.content.querySelector('script');
|
|
57
|
-
return new Promise(async resolve => {
|
|
58
|
-
if (script) {
|
|
59
|
-
const blob = new Blob([script.textContent], { type: 'text/javascript' });
|
|
60
|
-
const blobUrl = URL.createObjectURL(blob);
|
|
61
|
-
resolve((await import(blobUrl)).default);
|
|
62
|
-
URL.revokeObjectURL(blobUrl);
|
|
63
|
-
} else {
|
|
64
|
-
resolve({});
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
async function fetchFromFile(name) {
|
|
71
|
-
const fileName = name.split('-').slice(1).join('-');
|
|
72
|
-
const relativePath = typeof source === 'string'
|
|
73
|
-
? `${ source }/${ fileName }.html` : source[fileName];
|
|
74
|
-
const response = await fetch(relativePath);
|
|
75
|
-
if (!response.ok) {
|
|
76
|
-
throw new Error(`Could not find component ${ name }`);
|
|
77
|
-
}
|
|
78
|
-
const templateAsString = await response.text();
|
|
79
|
-
const document = new DOMParser().parseFromString(templateAsString, 'text/html');
|
|
80
|
-
return document.querySelector('body');
|
|
81
|
-
}
|
|
82
|
-
});
|
|
43
|
+
.map(element => element.localName)
|
|
44
|
+
.filter(name => !ancestors.includes(name));
|
|
45
|
+
|
|
46
|
+
// A component that another branch is still loading is awaited rather than
|
|
47
|
+
// skipped, so a parent is never defined before the components inside it
|
|
48
|
+
const promises = [...new Set(names)].map(name => {
|
|
49
|
+
if (!fileCache.has(name)) {
|
|
50
|
+
fileCache.set(name, load(name));
|
|
51
|
+
}
|
|
52
|
+
return fileCache.get(name);
|
|
53
|
+
});
|
|
83
54
|
|
|
84
55
|
return Promise.all(promises);
|
|
56
|
+
|
|
57
|
+
async function load(name) {
|
|
58
|
+
const html = await fetchFromFile(name);
|
|
59
|
+
const template = document.createElement('template');
|
|
60
|
+
template.content.append(...html.children);
|
|
61
|
+
const script = await copyScript(template);
|
|
62
|
+
await fromFile(template.content, [...ancestors, name]);
|
|
63
|
+
inPage(template.content, name);
|
|
64
|
+
register({ name, template, script });
|
|
65
|
+
|
|
66
|
+
function copyScript(template) {
|
|
67
|
+
const script = template.content.querySelector('script');
|
|
68
|
+
return new Promise(async resolve => {
|
|
69
|
+
if (script) {
|
|
70
|
+
const blob = new Blob([script.textContent], { type: 'text/javascript' });
|
|
71
|
+
const blobUrl = URL.createObjectURL(blob);
|
|
72
|
+
resolve((await import(blobUrl)).default);
|
|
73
|
+
URL.revokeObjectURL(blobUrl);
|
|
74
|
+
} else {
|
|
75
|
+
resolve({});
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function fetchFromFile(name) {
|
|
81
|
+
const fileName = name.split('-').slice(1).join('-');
|
|
82
|
+
const relativePath = typeof source === 'string'
|
|
83
|
+
? `${ source }/${ fileName }.html` : source[fileName];
|
|
84
|
+
const response = await fetch(relativePath);
|
|
85
|
+
if (!response.ok) {
|
|
86
|
+
throw new Error(`Could not find component ${ name }`);
|
|
87
|
+
}
|
|
88
|
+
const templateAsString = await response.text();
|
|
89
|
+
const document = new DOMParser().parseFromString(templateAsString, 'text/html');
|
|
90
|
+
return document.querySelector('body');
|
|
91
|
+
}
|
|
92
|
+
}
|
|
85
93
|
}
|
|
86
94
|
|
|
87
95
|
function register({ name, template, script }) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nbhi",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.17",
|
|
4
4
|
"description": "No-Build HTML Includes (NBHI) is a simple library that allows you to include html files into other html files and turns them into [web components](https://developer.mozilla.org/en-US/docs/Web/API/Web_components). Lazy initialization of content (when entering the viewport) and a one line call to update/hydrate content in your html pages. No build tools required, no `package.json` needed.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"web-components",
|