-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
38 lines (29 loc) · 1.17 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import S from 's-js';
import * as Surplus from 'surplus';
import { compile } from 'surplus/compiler/es';
// export packages to global namespace
const global = window as any;
global.S = S;
global.Surplus = Surplus;
document.addEventListener("DOMContentLoaded", compileSurplus);
function compileSurplus() {
let scriptIn : HTMLScriptElement | null,
scriptOut : HTMLScriptElement,
source : string,
compiled : string;
while (scriptIn = document.querySelector("script[type='text/jsx']")) {
scriptIn.type += '-processed';
source = scriptIn.textContent || scriptIn.innerText || scriptIn.innerHTML;
compiled = compile(source, { sourcemap: 'append' }) as string;
scriptOut = document.createElement('script');
scriptOut.type = 'text/javascript';
scriptOut.src = 'data:text/javascript;charset=utf-8,' + encodeURIComponent(compiled);
scriptOut.async = scriptIn.async;
scriptOut.defer = scriptIn.defer;
if (scriptIn.nextSibling) {
scriptIn.parentNode!.insertBefore(scriptOut, scriptIn.nextSibling);
} else {
scriptIn.parentNode!.appendChild(scriptOut);
}
}
}