This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-web/src/DoricBundleLoader.ts

30 lines
753 B
TypeScript
Raw Normal View History

2021-04-13 16:49:38 +08:00
import axios from "axios"
export interface DoricJSLoader {
filter(source: string): boolean;
request(source: string): Promise<string>;
}
const loaders: DoricJSLoader[] = [
{
filter: () => true,
request: async (source) => {
2021-10-13 15:45:31 +08:00
const result = await axios.get<string>(source)
2021-04-13 16:49:38 +08:00
return result.data;
}
}
];
export function registerDoricJSLoader(loader: DoricJSLoader) {
loaders.push(loader);
}
export async function loadDoricJSBundle(source: string): Promise<string> {
const matched = loaders.filter(e => e.filter(source))
if (matched.length > 0) {
return matched[matched.length - 1].request(source)
}
throw new Error(`Cannot find matched loader for '${source}'`);
}