Skip to content

Instantly share code, notes, and snippets.

@suisho suisho/example.d.ts
Created Nov 12, 2012

Embed
What would you like to do?
typescript compiler that custom library
failed
$ tsc sample.ts
success
$ npm install typescript
$ node my_ts_compiler.js sample.ts
declare function exampleFunc() : int;
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);
var loadExample = exampleFunc();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.