feat: migrate to starlight

This commit is contained in:
DuroCodes
2024-05-06 17:15:30 -04:00
parent 767acedea7
commit bb190f2d81
15140 changed files with 2828326 additions and 35408 deletions

35
node_modules/astro/dist/prefetch/index.d.ts generated vendored Normal file
View File

@@ -0,0 +1,35 @@
interface InitOptions {
defaultStrategy?: string;
prefetchAll?: boolean;
}
/**
* Initialize the prefetch script, only works once.
*
* @param defaultOpts Default options for prefetching if not already set by the user config.
*/
export declare function init(defaultOpts?: InitOptions): void;
export interface PrefetchOptions {
/**
* How the prefetch should prioritize the URL. (default `'link'`)
* - `'link'`: use `<link rel="prefetch">`, has lower loading priority.
* - `'fetch'`: use `fetch()`, has higher loading priority.
*/
with?: 'link' | 'fetch';
/**
* Should prefetch even on data saver mode or slow connection. (default `false`)
*/
ignoreSlowConnection?: boolean;
}
/**
* Prefetch a URL so it's cached when the user navigates to it.
*
* @param url A full or partial URL string based on the current `location.href`. They are only fetched if:
* - The user is online
* - The user is not in data saver mode
* - The URL is within the same origin
* - The URL is not the current page
* - The URL has not already been prefetched
* @param opts Additional options for prefetching.
*/
export declare function prefetch(url: string, opts?: PrefetchOptions): void;
export {};

219
node_modules/astro/dist/prefetch/index.js generated vendored Normal file
View File

@@ -0,0 +1,219 @@
const debug = import.meta.env.DEV ? console.debug : void 0;
const inBrowser = import.meta.env.SSR === false;
const prefetchedUrls = /* @__PURE__ */ new Set();
const listenedAnchors = /* @__PURE__ */ new WeakSet();
let prefetchAll = __PREFETCH_PREFETCH_ALL__;
let defaultStrategy = __PREFETCH_DEFAULT_STRATEGY__;
let clientPrerender = __EXPERIMENTAL_CLIENT_PRERENDER__;
let inited = false;
function init(defaultOpts) {
if (!inBrowser)
return;
if (inited)
return;
inited = true;
debug?.(`[astro] Initializing prefetch script`);
prefetchAll ??= defaultOpts?.prefetchAll ?? false;
defaultStrategy ??= defaultOpts?.defaultStrategy ?? "hover";
initTapStrategy();
initHoverStrategy();
initViewportStrategy();
initLoadStrategy();
}
function initTapStrategy() {
for (const event of ["touchstart", "mousedown"]) {
document.body.addEventListener(
event,
(e) => {
if (elMatchesStrategy(e.target, "tap")) {
prefetch(e.target.href, { with: "fetch", ignoreSlowConnection: true });
}
},
{ passive: true }
);
}
}
function initHoverStrategy() {
let timeout;
document.body.addEventListener(
"focusin",
(e) => {
if (elMatchesStrategy(e.target, "hover")) {
handleHoverIn(e);
}
},
{ passive: true }
);
document.body.addEventListener("focusout", handleHoverOut, { passive: true });
onPageLoad(() => {
for (const anchor of document.getElementsByTagName("a")) {
if (listenedAnchors.has(anchor))
continue;
if (elMatchesStrategy(anchor, "hover")) {
listenedAnchors.add(anchor);
anchor.addEventListener("mouseenter", handleHoverIn, { passive: true });
anchor.addEventListener("mouseleave", handleHoverOut, { passive: true });
}
}
});
function handleHoverIn(e) {
const href = e.target.href;
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(() => {
prefetch(href, { with: "fetch" });
}, 80);
}
function handleHoverOut() {
if (timeout) {
clearTimeout(timeout);
timeout = 0;
}
}
}
function initViewportStrategy() {
let observer;
onPageLoad(() => {
for (const anchor of document.getElementsByTagName("a")) {
if (listenedAnchors.has(anchor))
continue;
if (elMatchesStrategy(anchor, "viewport")) {
listenedAnchors.add(anchor);
observer ??= createViewportIntersectionObserver();
observer.observe(anchor);
}
}
});
}
function createViewportIntersectionObserver() {
const timeouts = /* @__PURE__ */ new WeakMap();
return new IntersectionObserver((entries, observer) => {
for (const entry of entries) {
const anchor = entry.target;
const timeout = timeouts.get(anchor);
if (entry.isIntersecting) {
if (timeout) {
clearTimeout(timeout);
}
timeouts.set(
anchor,
setTimeout(() => {
observer.unobserve(anchor);
timeouts.delete(anchor);
prefetch(anchor.href, { with: "link" });
}, 300)
);
} else {
if (timeout) {
clearTimeout(timeout);
timeouts.delete(anchor);
}
}
}
});
}
function initLoadStrategy() {
onPageLoad(() => {
for (const anchor of document.getElementsByTagName("a")) {
if (elMatchesStrategy(anchor, "load")) {
prefetch(anchor.href, { with: "link" });
}
}
});
}
function prefetch(url, opts) {
const ignoreSlowConnection = opts?.ignoreSlowConnection ?? false;
if (!canPrefetchUrl(url, ignoreSlowConnection))
return;
prefetchedUrls.add(url);
const priority = opts?.with ?? "link";
debug?.(`[astro] Prefetching ${url} with ${priority}`);
if (clientPrerender && HTMLScriptElement.supports && HTMLScriptElement.supports("speculationrules")) {
appendSpeculationRules(url);
} else if (priority === "link") {
const link = document.createElement("link");
link.rel = "prefetch";
link.setAttribute("href", url);
document.head.append(link);
} else {
fetch(url).catch((e) => {
console.log(`[astro] Failed to prefetch ${url}`);
console.error(e);
});
}
}
function canPrefetchUrl(url, ignoreSlowConnection) {
if (!navigator.onLine)
return false;
if (!ignoreSlowConnection && isSlowConnection())
return false;
try {
const urlObj = new URL(url, location.href);
return location.origin === urlObj.origin && (location.pathname !== urlObj.pathname || location.search !== urlObj.search) && !prefetchedUrls.has(url);
} catch {
}
return false;
}
function elMatchesStrategy(el, strategy) {
if (el?.tagName !== "A")
return false;
const attrValue = el.dataset.astroPrefetch;
if (attrValue === "false") {
return false;
}
if (strategy === "tap" && (attrValue != null || prefetchAll) && isSlowConnection()) {
return true;
}
if (attrValue == null && prefetchAll || attrValue === "") {
return strategy === defaultStrategy;
}
if (attrValue === strategy) {
return true;
}
return false;
}
function isSlowConnection() {
if ("connection" in navigator) {
const conn = navigator.connection;
return conn.saveData || /2g/.test(conn.effectiveType);
}
return false;
}
function onPageLoad(cb) {
cb();
let firstLoad = false;
document.addEventListener("astro:page-load", () => {
if (!firstLoad) {
firstLoad = true;
return;
}
cb();
});
}
function appendSpeculationRules(url) {
const script = document.createElement("script");
script.type = "speculationrules";
script.textContent = JSON.stringify({
prerender: [
{
source: "list",
urls: [url]
}
],
// Currently, adding `prefetch` is required to fallback if `prerender` fails.
// Possibly will be automatic in the future, in which case it can be removed.
// https://github.com/WICG/nav-speculation/issues/162#issuecomment-1977818473
prefetch: [
{
source: "list",
urls: [url]
}
]
});
document.head.append(script);
}
export {
init,
prefetch
};

View File

@@ -0,0 +1,5 @@
import type * as vite from 'vite';
import type { AstroSettings } from '../@types/astro.js';
export default function astroPrefetch({ settings }: {
settings: AstroSettings;
}): vite.Plugin;

View File

@@ -0,0 +1,45 @@
const virtualModuleId = "astro:prefetch";
const resolvedVirtualModuleId = "\0" + virtualModuleId;
const prefetchInternalModuleFsSubpath = "astro/dist/prefetch/index.js";
const prefetchCode = `import { init } from 'astro/virtual-modules/prefetch.js';init()`;
function astroPrefetch({ settings }) {
const prefetchOption = settings.config.prefetch;
const prefetch = prefetchOption ? typeof prefetchOption === "object" ? prefetchOption : {} : void 0;
if (prefetch && settings.scripts.every((s) => s.content !== prefetchCode)) {
settings.scripts.push({
stage: "page",
content: `import { init } from 'astro/virtual-modules/prefetch.js';init()`
});
}
const throwPrefetchNotEnabledError = () => {
throw new Error("You need to enable the `prefetch` Astro config to import `astro:prefetch`");
};
return {
name: "astro:prefetch",
async resolveId(id) {
if (id === virtualModuleId) {
if (!prefetch)
throwPrefetchNotEnabledError();
return resolvedVirtualModuleId;
}
},
load(id) {
if (id === resolvedVirtualModuleId) {
if (!prefetch)
throwPrefetchNotEnabledError();
return `export { prefetch } from "astro/virtual-modules/prefetch.js";`;
}
},
transform(code, id) {
if (id.includes(prefetchInternalModuleFsSubpath)) {
return code.replace("__PREFETCH_PREFETCH_ALL__", JSON.stringify(prefetch?.prefetchAll)).replace("__PREFETCH_DEFAULT_STRATEGY__", JSON.stringify(prefetch?.defaultStrategy)).replace(
"__EXPERIMENTAL_CLIENT_PRERENDER__",
JSON.stringify(settings.config.experimental.clientPrerender)
);
}
}
};
}
export {
astroPrefetch as default
};