Created
November 12, 2012 19:55
-
-
Save suisho/4061517 to your computer and use it in GitHub Desktop.
typescript compiler that custom library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
failed | |
$ tsc sample.ts | |
success | |
$ npm install typescript | |
$ node my_ts_compiler.js sample.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare function exampleFunc() : int; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
function createCompiler(outfile, outerr, libFiles){ | |
// npm install typescript 前提 | |
var nodeModulePath = 'node_modules/typescript/bin/typescript.js'; | |
require('vm').runInThisContext(fs.readFileSync(nodeModulePath), 'typescript.js'); | |
var ts = TypeScript; | |
var compiler = new ts.TypeScriptCompiler(outfile, outerr); | |
libFiles.forEach(function(file){ | |
var srcCode = fs.readFileSync(file).toString(); | |
compiler.addUnit(srcCode,file, false); | |
}) | |
return compiler; | |
} | |
function complieTs(src, libFiles){ | |
var outfile = { | |
source: '', | |
Write: function(s) {this.source += s;}, | |
WriteLine: function(s) {this.source += s + "\r\n";}, | |
Close: function(){} | |
}; | |
var outerr = { | |
Write: function(s) {}, | |
WriteLine: function(s) {}, | |
Close: function() {}, | |
}; | |
var compiler = createCompiler(outfile,outerr,libFiles); | |
var srcCode = fs.readFileSync(src).toString(); | |
compiler.addUnit(srcCode,src); | |
compiler.emit(true, function(){ | |
return outfile; | |
}); | |
console.log(outfile.source); | |
} | |
var src = process.argv[2]; | |
//ライブラリとなるファイルを書く | |
var libFiles = [ | |
"example.d.ts" | |
]; | |
complieTs(src, libFiles); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var loadExample = exampleFunc(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment