mirror of
https://github.com/web3privacy/w3ps1.git
synced 2024-10-15 16:26:26 +02:00
47 lines
1.4 KiB
JavaScript
47 lines
1.4 KiB
JavaScript
const scriptRel = "modulepreload";
|
|
const assetsURL = function(dep, importerUrl) {
|
|
return new URL(dep, importerUrl).href;
|
|
};
|
|
const seen = {};
|
|
const __vitePreload = function preload(baseModule, deps, importerUrl) {
|
|
if (!deps || deps.length === 0) {
|
|
return baseModule();
|
|
}
|
|
const links = document.getElementsByTagName("link");
|
|
return Promise.all(deps.map((dep) => {
|
|
dep = assetsURL(dep, importerUrl);
|
|
if (dep in seen)
|
|
return;
|
|
seen[dep] = true;
|
|
const isCss = dep.endsWith(".css");
|
|
const cssSelector = isCss ? '[rel="stylesheet"]' : "";
|
|
const isBaseRelative = !!importerUrl;
|
|
if (isBaseRelative) {
|
|
for (let i = links.length - 1; i >= 0; i--) {
|
|
const link2 = links[i];
|
|
if (link2.href === dep && (!isCss || link2.rel === "stylesheet")) {
|
|
return;
|
|
}
|
|
}
|
|
} else if (document.querySelector(`link[href="${dep}"]${cssSelector}`)) {
|
|
return;
|
|
}
|
|
const link = document.createElement("link");
|
|
link.rel = isCss ? "stylesheet" : scriptRel;
|
|
if (!isCss) {
|
|
link.as = "script";
|
|
link.crossOrigin = "";
|
|
}
|
|
link.href = dep;
|
|
document.head.appendChild(link);
|
|
if (isCss) {
|
|
return new Promise((res, rej) => {
|
|
link.addEventListener("load", res);
|
|
link.addEventListener("error", () => rej(new Error(`Unable to preload CSS for ${dep}`)));
|
|
});
|
|
}
|
|
})).then(() => baseModule());
|
|
};
|
|
export {
|
|
__vitePreload as _
|
|
};
|