add DoricVMElement

This commit is contained in:
pengfei.zhou
2019-12-19 10:42:57 +08:00
parent dce677e9a1
commit f5f23f611e
9 changed files with 1832 additions and 3 deletions

41
src/DoricElement.ts Normal file
View File

@@ -0,0 +1,41 @@
import axios from 'axios'
import { jsCallReject } from 'doric/src/runtime/sandbox'
declare const doricLibBundle: string
let contexId = 0
function getContextId() {
return `${contexId++}`
}
function initDoric() {
jsCallReject('', '')
}
console.log(doricLibBundle)
initDoric()
export class DoricElement extends HTMLElement {
source: string
alias: string
constructor() {
super()
this.source = this.getAttribute('src') || ""
this.alias = this.getAttribute('alias') || this.source
axios.get<string>(this.source).then(result => {
this.load(result.data)
})
}
load(content: string) {
const script = document.createElement('script');
const contextId = getContextId();
script.text = `Reflect.apply(function(doric,context,Entry,require,exports){
${content}
},doric.jsObtainContext("${contextId}"),[undefined,doric.jsObtainContext("${contextId}"),doric.jsObtainEntry("${contextId}"),doric.__require__,{}]);`
this.append(script)
}
}