Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Script tag support for TypeScript
## Usage
Add the following lines at the bottom of your page:
```html
<script src="https://rawgit.com/Microsoft/TypeScript/master/lib/typescriptServices.js"></script>
<script src="https://rawgit.com/basarat/typescript-script/master/transpiler.js"></script>
<script src="https://raw.githack.com/Microsoft/TypeScript/master/lib/typescriptServices.js"></script>
<script src="https://raw.githack.com/basarat/typescript-script/master/transpiler.js"></script>
```

And then you can use script tags that load `.ts` files or even have `typescript` inline:
Expand All @@ -16,6 +16,21 @@ And then you can use script tags that load `.ts` files or even have `typescript`
</script>
```

Optionally you can pass `compilerOptions` to the TypeScript transpiler:
```html
<script type="text/typescript" data-compiler-options='{ "target": "es5", "module": "none"}' src="script.ts"></script>
<script type="text/typescript">
setTimeout(()=>console.log('hello'));
</script>
```

If you are not using a module loader and just need a quick hack if you attempt to export from your module add this to the bottom instead:
```html
<script>var exports = {};</script>
<script src="https://raw.githack.com/Microsoft/TypeScript/master/lib/typescriptServices.js"></script>
<script src="https://raw.githack.com/basarat/typescript-script/master/transpiler.js"></script>
```

## Sample
### Plunker
http://plnkr.co/edit/j2pzXw?p=preview
14 changes: 9 additions & 5 deletions transpiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
total: 0, //total number of scripts to be loaded
loaded: 0, //current number of loaded scripts
data: [], //file data
name: [] //file name
name: [], //file name
config: [] //file tsconfig compiler options
};

//Function loads each script and pushes its content into scripts.data
var load = function (url) {
var load = function (url, compilerOptions) {
var xhr = window.ActiveXObject ? new window.ActiveXObject('Microsoft.XMLHTTP') : new window.XMLHttpRequest();;
xhr.open('GET', url, true);
if ('overrideMimeType' in xhr) xhr.overrideMimeType('text/plain');
Expand All @@ -21,6 +22,7 @@
scripts.loaded++;
scripts.data.push(xhr.responseText);
scripts.name.push(url);
scripts.config.push(JSON.parse(compilerOptions || "{}"));
if (scripts.loaded === scripts.total) compile();
return xhr.responseText;
} else {
Expand Down Expand Up @@ -57,7 +59,8 @@
for (num = 0; num < scripts.data.length; num++) {
filename = scripts.name[num] = scripts.name[num].slice(scripts.name[num].lastIndexOf('/') + 1);
var src = scripts.data[num];
source += ts.transpile(src);
var compilerOptions = scripts.config[num];
source += ts.transpile(src, compilerOptions);
}
})();
}
Expand All @@ -77,11 +80,12 @@
for (i = 0; i < script.length; i++) {
if (script[i].type == 'text/typescript') {
if (script[i].src) {
scripts.total++
load(script[i].src);
scripts.total++;
load(script[i].src, script[i].dataset.compilerOptions);
} else {
scripts.data.push(script[i].innerHTML);
scripts.name.push('innerHTML'+scripts.total);
scripts.config.push(JSON.parse(script[i].dataset.compilerOptions || "{}"));
scripts.total++;
scripts.loaded++;
}
Expand Down