@@ -27,6 +27,10 @@ const theme = window.matchMedia &&
27
27
28
28
const value = `
29
29
const a = 1 + 1;
30
+
31
+ export function Example() {
32
+ return a + 4 ;
33
+ }
30
34
`.trim();
31
35
32
36
const types = fetch("https://workers.cloudflare.com/index.d.ts", { cache : 'force-cache' } )
@@ -54,19 +58,34 @@ require(["vs/editor/editor.main"], function () {
54
58
theme ,
55
59
minimap: false
56
60
} ) ;
57
- console . log ( input ) ;
58
61
const output = monaco . editor . create ( document . getElementById ( 'output' ) , {
59
62
language: 'javascript' ,
60
63
value: '//' ,
61
64
theme,
62
65
readOnly: true ,
63
66
minimap: false
64
67
} ) ;
68
+ const statusEl = document . getElementById ( 'status' ) ;
69
+ const resultEl = document . getElementById ( 'result' ) ;
65
70
const onEdit = ( ) => {
66
71
const body = input . getValue ( ) ;
72
+
73
+ const encoder = new TextEncoder ( ) ;
74
+ const data = encoder . encode ( body ) ;
75
+ crypto . subtle . digest ( 'SHA-256' , data ) . then ( hash => {
76
+ const hex = Array . from ( new Uint8Array ( hash ) , chunk => chunk . toString ( 16 ) . padStart ( 2 , '0' ) ) . join ( '' ) ;
77
+ statusEl. textContent = `SHA256: ${ hex } `;
78
+ } ) ;
79
+
67
80
esbuildPromise
68
- . then ( esbuild => esbuild . transform ( body , { loader: 'jsx' } ) )
69
- . then ( content => output . getModel ( ) . setValue ( content . code ) )
81
+ . then ( esbuild => esbuild . transform ( body , { loader: 'jsx' , format: 'iife' , globalName: 'exports' , } ) )
82
+ . then ( content => {
83
+ output . getModel ( ) . setValue ( content . code ) ;
84
+
85
+ const executor = new Function ( `${ content. code } ; return exports . Example ( ) ; `) ;
86
+ console . log ( 'executor' , executor , executor ( ) ) ;
87
+ resultEl . textContent = JSON . stringify ( executor ( ) ) ;
88
+ } )
70
89
.catch ( ( err ) => output . getModel ( ) . setValue ( err . message . replace ( / ^ / gm , '// $&' ) ) ) ;
71
90
/ *fetch ( '/upload' , { method: 'POST' , body } )
72
91
. then ( async ( response ) => {
@@ -81,6 +100,8 @@ require(["vs/editor/editor.main"], function () {
81
100
model . setValue ( input . getValue ( ) + "\n " ) ;
82
101
} );
83
102
</ script >
103
+ < output id = status class = "block text-xs opacity-50 " > </ output >
104
+ < output id = result class = "block text-xs opacity-50 " > </ output >
84
105
< div class = "flex-container " id = "container " style = "display: flex; min-height: 100vh; " >
85
106
< div id = "input " style = "flex: 1; " > </ div >
86
107
< div id = "output " style = "flex: 1; " > </ div >
0 commit comments