をLanguageServiceHost。私はインメモリコンパイラを設定するためにこのガイドに従っています:http://blog.scottlogic.com/2015/01/20/typescript-compiler-api.html。 私が編集しているTSファイルには、TSDファイルを持っているjQueryのようなものがいくつかインポートされています。は、私は非常に頻繁に呼び出されることになるだろうと、単一の活字体はすぐにファイルをコンパイルする必要がある活字体のコンパイラを実装しています
typescriptCompile.ts
import * as fs from 'fs';
import * as path from 'path';
import * as ts from 'typescript';
class MyLanguageServiceHost implements ts.LanguageServiceHost {
files: { [fileName: string]: { file: ts.IScriptSnapshot; ver: number } } = {};
log = _ => { };
trace = _ => { };
error = _ => { };
getCompilationSettings = ts.getDefaultCompilerOptions;
getScriptIsOpen = _ => true;
getCurrentDirectory =() => "";
getDefaultLibFileName = _ => "lib";
getScriptVersion = fileName => this.files[fileName].ver.toString();
getScriptSnapshot = fileName => this.files[fileName].file;
getScriptFileNames(): string[] {
var names: string[] = [];
for (var name in this.files) {
if (this.files.hasOwnProperty(name)) {
names.push(name);
}
}
return names;
}
addFile(fileName: string, body: string) {
var snap = ts.ScriptSnapshot.fromString(body);
snap.getChangeRange = _ => undefined;
var existing = this.files[fileName];
if (existing) {
this.files[fileName].ver++;
this.files[fileName].file = snap
} else {
this.files[fileName] = { ver: 1, file: snap };
}
}
}
var host = new MyLanguageServiceHost();
var languageService = ts.createLanguageService(host, ts.createDocumentRegistry());
const includeFiles = [
'es6-promise.d.ts',
'jquery-3.1.1.d.ts',
'twemoji-2.2.4.d.ts'
];
includeFiles.forEach(file => host.addFile('types/' + file, fs.readFileSync(path.join(__dirname, 'types', file)).toString()));
export function compile(code: string) {
host.addFile(`code.ts`, code);
var output = languageService.getEmitOutput("code.ts").outputFiles[0].text;
console.log(output);
}
コード
import * as twemoji from 'twemoji-2.2.4';
export const test = twemoji.parse('test');
をコンパイルするには、言語サービスは、スクリプトtwemoji-2.2.4.ts
ではなくtwemoji-2.2.4.ts
にアクセスしようと、エラーがスローされます。どのように定義を言語サービス/コンパイラに登録できますか?