55 lines
2.0 KiB
JavaScript
55 lines
2.0 KiB
JavaScript
(function(exports) {
|
|
"use strict";
|
|
const DEFAULT_VERSIONS = {
|
|
"state": "v1.0.11",
|
|
"bootstrap": "v1.0.1",
|
|
"base": "v1.0.7",
|
|
"dataTable": "v1.0.6",
|
|
"kanban": "v1.0.0",
|
|
"mindmap": "v1.0.0",
|
|
"chart": "v1.0.0"
|
|
};
|
|
const Loader = {
|
|
/**
|
|
* 加载指定模块并注入 importmap
|
|
* @param {...string} pkgs 模块名格式: 'name' 或 'name:version'
|
|
*/
|
|
load: (...pkgs) => {
|
|
if (typeof document === "undefined") return;
|
|
const currentScript = document.currentScript;
|
|
const currentUrl = currentScript ? currentScript.src : "";
|
|
let tpl = "https://apigo.cc/web/{project}/raw/tag/{tag}/dist/{project}.js";
|
|
if (currentUrl.includes("/loader/")) {
|
|
const match = currentUrl.match(/(.*\/web\/)loader\/raw\/tag\/([^\/]+)\//);
|
|
if (match) {
|
|
const baseUrl = match[1];
|
|
const loaderTag = match[2];
|
|
tpl = `${baseUrl}{project}/raw/tag/{tag}/dist/{project}.js`;
|
|
Object.keys(DEFAULT_VERSIONS).forEach((k) => {
|
|
if (DEFAULT_VERSIONS[k] === "v1.0.0") DEFAULT_VERSIONS[k] = loaderTag;
|
|
});
|
|
}
|
|
}
|
|
const importMap = { imports: {} };
|
|
pkgs.forEach((pkg) => {
|
|
let [name, version] = pkg.split(":");
|
|
version = version || DEFAULT_VERSIONS[name] || "main";
|
|
const url = tpl.replace(/{project}/g, name).replace(/{tag}/g, version);
|
|
importMap.imports[`@web/${name}`] = url;
|
|
});
|
|
const script = document.createElement("script");
|
|
script.type = "importmap";
|
|
script.textContent = JSON.stringify(importMap);
|
|
if (currentScript) {
|
|
currentScript.parentNode.insertBefore(script, currentScript);
|
|
} else {
|
|
document.head.appendChild(script);
|
|
}
|
|
}
|
|
};
|
|
globalThis.Loader = Loader;
|
|
exports.Loader = Loader;
|
|
exports.default = Loader;
|
|
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
})(this.Loader = this.Loader || {});
|