├── LICENSE ├── README.md └── transpiler.js /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Basarat Ali Syed 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # typescript-script 2 | Script tag support for TypeScript 3 | 4 | ## Usage 5 | Add the following lines at the bottom of your page: 6 | ```html 7 | 8 | 9 | ``` 10 | 11 | And then you can use script tags that load `.ts` files or even have `typescript` inline: 12 | ```html 13 | 14 | 17 | ``` 18 | 19 | ## Sample 20 | ### Plunker 21 | http://plnkr.co/edit/j2pzXw?p=preview 22 | -------------------------------------------------------------------------------- /transpiler.js: -------------------------------------------------------------------------------- 1 | // BASED on https://github.com/niutech/typescript-compile but using 1.5 transpile function 2 | 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 elem, source = '', 37 | body = document.getElementsByTagName('body')[0]; 38 | scripts.total = 0; //clear the 'queue' incase the xhr response was super quick and happened before the initializer finished 39 | var hashCode = function (s) { 40 | var hsh = 0, 41 | chr, i; 42 | if (s.length == 0) { 43 | return hsh; 44 | } 45 | for (i = 0; i < s.length; i++) { 46 | chr = s.charCodeAt(i); 47 | hsh = (hsh << 5) - hsh + chr; 48 | hsh = hsh & hsh; //Convert to 32bit integer 49 | } 50 | return hsh; 51 | }; 52 | if (window.sessionStorage && sessionStorage.getItem('typescript' + hashCode(scripts.data.join('')))) { 53 | source = sessionStorage.getItem('typescript' + hashCode(scripts.data.join(''))); 54 | } else { 55 | (function () { 56 | var filename; 57 | for (var num = 0; num < scripts.data.length; num++) { 58 | filename = scripts.name[num] = scripts.name[num].slice(scripts.name[num].lastIndexOf('/') + 1); 59 | var src = scripts.data[num]; 60 | source += ts.transpile(src); 61 | } 62 | })(); 63 | } 64 | elem = document.createElement('script'); 65 | elem.type = 'text/javascript'; 66 | elem.innerHTML = '//Compiled TypeScript\n\n' + source; 67 | body.appendChild(elem); 68 | }; 69 | 70 | (function () { 71 | //Polyfill for older browsers 72 | if (!window.console) window.console = { 73 | log: function () {} 74 | }; 75 | var script = document.getElementsByTagName('script'); 76 | for (var i = 0; i < script.length; i++) { 77 | if (script[i].type == 'text/typescript') { 78 | if (script[i].src) { 79 | scripts.total++ 80 | load(script[i].src); 81 | } else { 82 | scripts.data.push(script[i].innerHTML); 83 | scripts.name.push('innerHTML'+scripts.total); 84 | scripts.total++; 85 | scripts.loaded++; 86 | } 87 | } 88 | } 89 | if (scripts.loaded === scripts.total) compile(); //only fires if all scripts are innerHTML, else this is fired on XHR response 90 | })(); 91 | })(); 92 | --------------------------------------------------------------------------------