|
| 1 | + |
| 2 | + |
| 3 | +<!-- headers |
| 4 | + cross-origin-isolate |
| 5 | +--> |
| 6 | + |
| 7 | + |
| 8 | +<html> |
| 9 | + |
| 10 | + |
| 11 | + <head> |
| 12 | + <meta charset=utf-8> |
| 13 | + |
| 14 | + <title> |
| 15 | + </title> |
| 16 | + |
| 17 | + <base href='https://ext-code.com/utils/editors/srcdoc/'> |
| 18 | + <link rel=canonical href='https://ext-code.com/utils/editors/srcdoc/srcdoc.html'> |
| 19 | + |
| 20 | + <meta name=viewport content='width=device-width, initial-scale=1'> |
| 21 | + <link rel=icon type='image/png' href='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAsCAMAAAAgsQpJAAAAWlBMVEVHcExXpP9XpP9XpP9XpP9XpP9XpP9bpv9WpP9XpP9XpP9XpP9XpP9Wo/9XpP9XpP9XpP9XpP9XpP9XpP9Zpv9WpP9XpP+cyv+Txf9/uv91tf9iqv+KwP9rr/9r+fBYAAAAF3RSTlMAHeFk6nAK/Xs0BNNJwicTpLfdP/WSXXgcQWoAAAGfSURBVDjLvZRZtoMgEAWFqCgqGDCRqfe/zQeSAQIZvl7lHPNTQjf2pWkC6HqqMvdNRn8mdbZLbiJhVA1NSG4iofcablvbp9lzzoQulztEeXqYbJ0mQWyJUbsjy7klQxT9K0ONhYBSBgDsEpfsMGpqXIiFsP1uUpFmRBFcNFORreeEpTtEqw0JZiqiy5pyirV7iH7ZugJljEn8g/j4EKlIUQ1aiuOES6axFHltyq68FH+u8Wexl3OJ7CvNiLZEVJqhY43+P5rhXWVyO1SKbJlKlrHhUaTPZnzAMsbjfOSMjXMwzZLReo1XvEVu18DWDrSMQrg4wOUYgYoorKzpF/NyE2jMiihcUCIqozLxhSgq0Lu2GtwX0VnrdgMKrH6KlD8DwOkh6hBqZY3f3UfxLvooiBshAUEE4rd0x8NadRf5tbsTEhBEB35nbVUs4VONyq8JEL0vXWt9lKiSrtMo+AQ8zlGrlwNPoyDGD1+mzxNAV6tzYOLV6RnPcSTF1t5mcw5jJlnBKA/mdojzGWZ8xi1+R4ulH8Hj5//l6T2SPsr5A6avRJ8t3CbuAAAAAElFTkSuQmCC'> |
| 22 | + |
| 23 | + <script src='https://libs.ext-code.com/js/dom/component/v3.0/component.js?hdr'></script> |
| 24 | + |
| 25 | + <script init> |
| 26 | + console.clear(); |
| 27 | + console.log('srcdoc.html'); |
| 28 | + console.log(); |
| 29 | + console.json=v=>console.log(JSON.stringify(v)); |
| 30 | + var df=true,version='v2.0' |
| 31 | + ; |
| 32 | + |
| 33 | + var ext; |
| 34 | + var $; |
| 35 | + var datatype; |
| 36 | + var menumod; |
| 37 | + var keydown; |
| 38 | + var debug; |
| 39 | + var github; |
| 40 | + |
| 41 | + var menu; |
| 42 | + |
| 43 | + |
| 44 | + var term; |
| 45 | + var webcontainer; |
| 46 | + var files = {}; |
| 47 | + |
| 48 | + |
| 49 | + var packages = ['espree']; |
| 50 | + var filename = 'espree.m.js'; |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + async function ready(){ |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + var {Terminal} = await import('https://cdn.jsdelivr.net/npm/@xterm/xterm/+esm'); |
| 60 | + var {FitAddon} = await import('https://cdn.jsdelivr.net/npm/@xterm/addon-fit/+esm'); |
| 61 | + |
| 62 | + term = new Terminal(); |
| 63 | + var fitAddon = new FitAddon(); |
| 64 | + term.loadAddon(fitAddon); |
| 65 | + term.open(terminal); |
| 66 | + fitAddon.fit(); |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + for(var key in files){ |
| 72 | + |
| 73 | + files[key] = {file:{contents:files[key]}}; |
| 74 | + |
| 75 | + }//for |
| 76 | + |
| 77 | + |
| 78 | + console.log('download ...'); |
| 79 | + var {WebContainer} = await import('https://cdn.jsdelivr.net/npm/@webcontainer/api/+esm'); |
| 80 | + |
| 81 | + console.log('booting ...'); |
| 82 | + webcontainer = await WebContainer.boot(); |
| 83 | + |
| 84 | + console.log('mounting file system ...'); |
| 85 | + await webcontainer.mount(files); |
| 86 | + |
| 87 | + |
| 88 | + await install(); |
| 89 | + await package_json(); |
| 90 | + await install_rollup(); |
| 91 | + |
| 92 | + |
| 93 | + await rollup(); |
| 94 | + |
| 95 | + await server(); |
| 96 | + console.log('done.'); |
| 97 | + |
| 98 | + |
| 99 | + async function install(){ |
| 100 | + var str = packages.join(' '); |
| 101 | + console.log('npm install',str,'...'); |
| 102 | + packages.unshift('install'); |
| 103 | + var process = await webcontainer.spawn('npm',packages); |
| 104 | + var stream = new WritableStream({write(data){term.write(data)}}); |
| 105 | + process.output.pipeTo(stream) |
| 106 | + var code = await process.exit; |
| 107 | + if(code!=0){ |
| 108 | + console.log('an error occurred'); |
| 109 | + } |
| 110 | + return code; |
| 111 | + |
| 112 | + }//install |
| 113 | + |
| 114 | + |
| 115 | + async function package_json(){ |
| 116 | + console.log('npm install ( package.json ) ...'); |
| 117 | + var process = await webcontainer.spawn('npm',['install']); |
| 118 | + var stream = new WritableStream({write(data){term.write(data)}}); |
| 119 | + process.output.pipeTo(stream) |
| 120 | + var code = await process.exit; |
| 121 | + if(code!=0){ |
| 122 | + console.log('an error occurred'); |
| 123 | + } |
| 124 | + return code; |
| 125 | + |
| 126 | + }//package_json |
| 127 | + |
| 128 | + |
| 129 | + async function install_rollup(){ |
| 130 | + |
| 131 | + var packages = [ |
| 132 | + 'rollup', |
| 133 | + '@rollup/plugin-commonjs', |
| 134 | + '@rollup/plugin-node-resolve', |
| 135 | + '@rollup/plugin-json', |
| 136 | + 'rollup-plugin-polyfill-node' |
| 137 | + ]; |
| 138 | + console.log('npm install',packages.join(' '),'...'); |
| 139 | + packages.unshift('install'); |
| 140 | + |
| 141 | + var process = await webcontainer.spawn('npm',packages); |
| 142 | + var stream = new WritableStream({write(data){term.write(data)}}); |
| 143 | + process.output.pipeTo(stream) |
| 144 | + var code = await process.exit; |
| 145 | + if(code!=0){ |
| 146 | + console.log('an error occurred'); |
| 147 | + } |
| 148 | + return code; |
| 149 | + |
| 150 | + }//install_rollup |
| 151 | + |
| 152 | + |
| 153 | + async function rollup(){ |
| 154 | + console.log('perform rollup ...'); |
| 155 | + var process = await webcontainer.spawn('npx',['-y','rollup','--config','rollup.config.js']); |
| 156 | + |
| 157 | + var stream = new WritableStream({write(data){term.write(data)}}); |
| 158 | + process.output.pipeTo(stream); |
| 159 | + |
| 160 | + var code = await process.exit; |
| 161 | + if(code!=0){ |
| 162 | + console.log('an error occurred'); |
| 163 | + } |
| 164 | + return code; |
| 165 | + |
| 166 | + }//rollup |
| 167 | + |
| 168 | + |
| 169 | + async function server(){ |
| 170 | + console.log('launching server ...'); |
| 171 | + webcontainer.on('server-ready',(port,url)=>{ |
| 172 | + console.log('server : ',url,port); |
| 173 | + iframe.src = url; |
| 174 | + |
| 175 | + }); |
| 176 | + |
| 177 | + var process = await webcontainer.spawn('node',['server.js']); |
| 178 | + |
| 179 | + var stream = new WritableStream({write(data){term.write(data)}}); |
| 180 | + process.output.pipeTo(stream); |
| 181 | + |
| 182 | + var code = await process.exit; |
| 183 | + if(code!=0){ |
| 184 | + console.log('an error occurred'); |
| 185 | + } |
| 186 | + return code; |
| 187 | + |
| 188 | + }//server |
| 189 | + |
| 190 | + |
| 191 | + |
| 192 | + }//ready |
| 193 | + |
| 194 | + |
| 195 | + //: |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + files['entry.js'] = ` |
| 200 | + |
| 201 | + import * as espree from 'espree'; |
| 202 | + export {espree}; |
| 203 | + |
| 204 | + //export default espree; // iife / umd |
| 205 | + |
| 206 | + `; |
| 207 | + |
| 208 | + |
| 209 | + files['package.json'] = ` |
| 210 | + |
| 211 | + { |
| 212 | + "name": "node-test", |
| 213 | + "version": "1.0.0", |
| 214 | + "scripts": { |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + `; |
| 219 | + |
| 220 | + |
| 221 | + files['rollup.config.js'] = ` |
| 222 | + |
| 223 | + import resolve from '@rollup/plugin-node-resolve'; |
| 224 | + import commonjs from '@rollup/plugin-commonjs'; |
| 225 | + import json from '@rollup/plugin-json'; |
| 226 | + import nodePolyfills from 'rollup-plugin-polyfill-node'; |
| 227 | + |
| 228 | + export default { |
| 229 | + input : 'entry.js', |
| 230 | + output : { |
| 231 | + file : '${filename}', |
| 232 | + format : 'es' |
| 233 | + |
| 234 | + //format : 'iife', // or 'umd' |
| 235 | + //name : 'espree', // This becomes window.espree |
| 236 | + //exports : 'default', |
| 237 | + |
| 238 | + }, |
| 239 | + plugins : [ |
| 240 | + commonjs(), |
| 241 | + json(), |
| 242 | + nodePolyfills(), |
| 243 | + resolve({preferBuiltins:false}) |
| 244 | + ] |
| 245 | + }; |
| 246 | + |
| 247 | + `; |
| 248 | + |
| 249 | + |
| 250 | + files['server.js'] = ` |
| 251 | + |
| 252 | + var port = 3000; |
| 253 | + |
| 254 | + require('http').createServer(request).listen(port); |
| 255 | + |
| 256 | + var fs = require('fs'); |
| 257 | + console.log('listening',port); |
| 258 | + |
| 259 | + function request(req,res){ |
| 260 | + console.log(req.method,req.url); |
| 261 | + if(cors(req,res))return; |
| 262 | + |
| 263 | + if(req.url==='/${filename}'){ |
| 264 | + var stream = fs.createReadStream('${filename}'); |
| 265 | + res.writeHead(200,{'content-type':'text/javascript'}); |
| 266 | + stream.pipe(res); |
| 267 | + return; |
| 268 | + } |
| 269 | + |
| 270 | + if(req.url==='/test.html'){ |
| 271 | + var stream = fs.createReadStream('test.html'); |
| 272 | + res.writeHead(200,{'content-type':'text/html'}); |
| 273 | + stream.pipe(res); |
| 274 | + return; |
| 275 | + } |
| 276 | + if(req.url==='/test2.html'){ |
| 277 | + var stream = fs.createReadStream('test2.html'); |
| 278 | + res.writeHead(200,{'content-type':'text/html'}); |
| 279 | + stream.pipe(res); |
| 280 | + return; |
| 281 | + } |
| 282 | + |
| 283 | + var stream = fs.createReadStream('index.html'); |
| 284 | + res.writeHead(200,{'content-type':'text/html'}); |
| 285 | + stream.pipe(res); |
| 286 | + |
| 287 | + }//request |
| 288 | + |
| 289 | + |
| 290 | + function cors(req,res){ |
| 291 | + |
| 292 | + res.setHeader('access-control-allow-origin','*'); |
| 293 | + |
| 294 | + if(req.method!=='OPTIONS'){ |
| 295 | + return; |
| 296 | + } |
| 297 | + |
| 298 | + res.writeHead(204); |
| 299 | + res.end(); |
| 300 | + return true; |
| 301 | + |
| 302 | + }//cors |
| 303 | + |
| 304 | + |
| 305 | + `; |
| 306 | + |
| 307 | + |
| 308 | + <script> |
| 309 | + |
| 310 | + |
| 311 | + <style> |
| 312 | + |
| 313 | + |
| 314 | + </style> |
| 315 | + |
| 316 | + |
| 317 | + </head> |
| 318 | + |
| 319 | + |
| 320 | + <body> |
| 321 | + |
| 322 | + |
| 323 | + </body> |
| 324 | + |
| 325 | + |
| 326 | +</html> |
0 commit comments