-
Notifications
You must be signed in to change notification settings - Fork 0
/
i.js
53 lines (43 loc) · 1.82 KB
/
i.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Dynamically import custom elements via an import map.
*
* @example <script type="importmap">{ "imports": {}, "myElements": { "use": [ "my-map" ] } }</script>
* @license MIT
*/
import { elemToClass } from './src/Options.js';
const base = '.';
try {
const importMapElement = document.querySelector('script[ type = importmap ]');
const importMap = importMapElement ? JSON.parse(importMapElement.textContent) : null;
const myElements = importMap ? importMap.myElements : null;
const ELEM = myElements ? myElements.use : [];
const KLASS = ELEM.map((el) => {
const klass = elemToClass(el);
const path = `${base}/src/components/${klass}.js`;
return { el, klass, path };
});
const PR = KLASS.map(async ({ path }) => await import(path));
await Promise.all(PR);
console.assert(KLASS.length, 'No custom elements imported.');
console.debug('i.js:', KLASS, import.meta.url);
} catch (err) {
console.error(err);
}
/*
const importMapElement = document.querySelector('script[ type = importmap ]');
console.assert(importMapElement, 'Expecting an importmap `<script>` element. None found.');
const importMap = importMapElement ? JSON.parse(importMapElement.textContent) : null;
console.assert(importMap, 'Expecting an importmap JSON object.');
const { myElements } = importMap;
console.assert(myElements, 'Expecting a `myElements` object on importmap');
console.assert(myElements && myElements.use && myElements.use.constructor.name === 'Array', 'Expecting a `myElements.use` array.');
const ELEM = myElements ? myElements.use : [];
const KLASS = ELEM.map((el) => {
const klass = elemToClass(el);
const path = `${base}/src/components/${klass}.js`;
return { el, klass, path };
});
const PR = KLASS.map(async ({ path }) => await import(path));
await Promise.all(PR);
console.debug('i.js:', KLASS, import.meta.url);
*/