web:add source loader
This commit is contained in:
30
doric-web/src/DoricBundleLoader.ts
Normal file
30
doric-web/src/DoricBundleLoader.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import axios from "axios"
|
||||
|
||||
export interface DoricJSLoader {
|
||||
filter(source: string): boolean;
|
||||
request(source: string): Promise<string>;
|
||||
}
|
||||
|
||||
|
||||
const loaders: DoricJSLoader[] = [
|
||||
{
|
||||
filter: () => true,
|
||||
request: async (source) => {
|
||||
const result = await axios.get(source)
|
||||
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}'`);
|
||||
|
||||
}
|
@@ -1,4 +1,5 @@
|
||||
import axios from 'axios'
|
||||
import { loadDoricJSBundle } from './DoricBundleLoader'
|
||||
import { DoricContext } from './DoricContext'
|
||||
|
||||
|
||||
@@ -31,8 +32,8 @@ export class DoricElement extends HTMLElement {
|
||||
|
||||
connectedCallback() {
|
||||
if (this.src && this.context === undefined) {
|
||||
axios.get(this.src).then(result => {
|
||||
this.load(result.data)
|
||||
loadDoricJSBundle(this.src).then(result => {
|
||||
this.load(result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user