2025-11-30 11:44:47 -08:00
|
|
|
import type { ScriptLoader } from "./types";
|
|
|
|
|
import { getUrlForPath } from "../loaders";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a script loader for browser environments that fetches scripts
|
|
|
|
|
* using the manifest-based URL resolution.
|
|
|
|
|
*/
|
|
|
|
|
export function createScriptLoader(): ScriptLoader {
|
|
|
|
|
return async (path: string): Promise<string | null> => {
|
|
|
|
|
let url: string;
|
|
|
|
|
try {
|
|
|
|
|
url = getUrlForPath(path);
|
|
|
|
|
} catch (err) {
|
2025-12-02 22:06:20 -08:00
|
|
|
console.warn(`Script not in manifest: ${path} (${err})`);
|
2025-11-30 11:44:47 -08:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(url);
|
|
|
|
|
if (!response.ok) {
|
2025-12-02 22:06:20 -08:00
|
|
|
console.error(`Script fetch failed: ${path} (${response.status})`);
|
2025-11-30 11:44:47 -08:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return await response.text();
|
|
|
|
|
} catch (err) {
|
2025-12-02 22:06:20 -08:00
|
|
|
console.error(`Script fetch error: ${path}`);
|
|
|
|
|
console.error(err);
|
2025-11-30 11:44:47 -08:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|