summaryrefslogtreecommitdiff
path: root/src/lib/include.js
blob: 896c6d5b5dd3979b35bf3be995fc052c13af9d49 (plain)
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
var include = (() =>
{
  let  _inclusions = new Map();

  function scriptLoader(src) {
    let script = document.createElement("script");
    script.src = src;
    return script;
  }

  function styleLoader(src) {
    let style = document.createElement("link");
    style.rel = "stylesheet";
    style.type = "text/css";
    style.href = src;
    return style;
  }

  return async function include(src) {
    if (_inclusions.has(src)) return await _inclusions.get(src);
    if (Array.isArray(src)) {
      return await Promise.all(src.map(s => include(s)));
    }
    debug("Including", src);

    let loading = new Promise((resolve, reject) => {
      let inc = src.endsWith(".css") ? styleLoader(src) : scriptLoader(src);
      inc.onload = () => resolve(inc);
      inc.onerror = () => reject(new Error(`Failed to load ${src}`));
      document.head.appendChild(inc);
    });
    _inclusions.set(src, loading);
    return await (loading);
  }
})();