├── .gitignore ├── index.html ├── README.md └── transpiler.js /.gitignore: -------------------------------------------------------------------------------- 1 | /.DS_Store -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | MoonBit Script Test 8 | 9 | 10 | 11 | 12 | 13 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # moonbit-script 2 | Script tag support for Moonbit 3 | 4 | ## Usage 5 | Add the following lines at the bottom of your page: 6 | ```html 7 | 8 | ``` 9 | 10 | And then you can use script tags that load `.mbt` files or even have `moonbit` inline: 11 | ```html 12 | 13 | 16 | ``` 17 | -------------------------------------------------------------------------------- /transpiler.js: -------------------------------------------------------------------------------- 1 | // BASED on https://github.com/basarat/typescript-script 2 | import { create, eval as eval_mb } from "https://obscloud.ulearning.cn/resources/web/moonbit-eval.js" 3 | (function () { 4 | //Keep track of the number of scripts to be pulled, and fire the compiler 5 | //after the number of loaded reaches the total 6 | var scripts = { 7 | total: 0, //total number of scripts to be loaded 8 | loaded: 0, //current number of loaded scripts 9 | data: [], //file data 10 | name: [] //file name 11 | }; 12 | 13 | //Function loads each script and pushes its content into scripts.data 14 | var load = function (url) { 15 | var xhr = window.ActiveXObject ? new window.ActiveXObject('Microsoft.XMLHTTP') : new window.XMLHttpRequest();; 16 | xhr.open('GET', url, true); 17 | if ('overrideMimeType' in xhr) xhr.overrideMimeType('text/plain'); 18 | xhr.onreadystatechange = function () { 19 | if (xhr.readyState !== 4) return; 20 | if (xhr.status === 0 || xhr.status === 200) { 21 | scripts.loaded++; 22 | scripts.data.push(xhr.responseText); 23 | scripts.name.push(url); 24 | if (scripts.loaded === scripts.total) compile(); 25 | return xhr.responseText; 26 | } else { 27 | console.log('Could not load ' + url); 28 | } //end if 29 | }; //end xhr.onreadystatechange() 30 | return xhr.send(null); 31 | }; 32 | 33 | //Compiles each of the scripts found within scripts.data 34 | var compile = function () { 35 | if (scripts.data.length == 0 || scripts.data.length != scripts.name.length) return; //no reason to compile when there are no scripts 36 | var source = ''; 37 | scripts.total = 0; //clear the 'queue' incase the xhr response was super quick and happened before the initializer finished 38 | var hashCode = function (s) { 39 | var hsh = 0, 40 | chr, i; 41 | if (s.length == 0) { 42 | return hsh; 43 | } 44 | for (i = 0; i < s.length; i++) { 45 | chr = s.charCodeAt(i); 46 | hsh = (hsh << 5) - hsh + chr; 47 | hsh = hsh & hsh; //Convert to 32bit integer 48 | } 49 | return hsh; 50 | }; 51 | if (window.sessionStorage && sessionStorage.getItem('moonbit' + hashCode(scripts.data.join('')))) { 52 | source = sessionStorage.getItem('moonbit' + hashCode(scripts.data.join(''))); 53 | } else { 54 | (function () { 55 | var filename; 56 | for (var num = 0; num < scripts.data.length; num++) { 57 | filename = scripts.name[num] = scripts.name[num].slice(scripts.name[num].lastIndexOf('/') + 1); 58 | var src = scripts.data[num]; 59 | const mb = create(true); 60 | eval_mb(mb, src, false, false); 61 | } 62 | })(); 63 | } 64 | }; 65 | 66 | (function () { 67 | //Polyfill for older browsers 68 | if (!window.console) window.console = { 69 | log: function () { } 70 | }; 71 | var script = document.getElementsByTagName('script'); 72 | for (var i = 0; i < script.length; i++) { 73 | if (script[i].type == 'text/moonbit') { 74 | if (script[i].src) { 75 | scripts.total++ 76 | load(script[i].src); 77 | } else { 78 | scripts.data.push(script[i].innerHTML); 79 | scripts.name.push('innerHTML' + scripts.total); 80 | scripts.total++; 81 | scripts.loaded++; 82 | } 83 | } 84 | } 85 | if (scripts.loaded === scripts.total) compile(); //only fires if all scripts are innerHTML, else this is fired on XHR response 86 | })(); 87 | })(); --------------------------------------------------------------------------------