Skip to content

Commit 71d93a4

Browse files
committed
feat(compiler): Compiler now takes JSON options.
1 parent 523577c commit 71d93a4

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

source/compiler-spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
/*tslint:disable:no-unused-expression*/
77

88
import { expect } from "chai";
9-
import * as ts from "typescript";
109
import { Compiler } from "./compiler";
1110

1211
describe("Compiler", () => {
@@ -81,7 +80,7 @@ describe("Compiler", () => {
8180

8281
it("should support options", () => {
8382

84-
const compiler = new Compiler({ target: ts.ScriptTarget.ES2015 });
83+
const compiler = new Compiler({ moduleResolution: "node", target: "es2015" });
8584
const program = compiler.compile({
8685
"snippet.ts": `
8786
const person = Object.assign({}, { name: "alice" });

source/compiler.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,30 @@ export class Compiler {
1515
private _files: ts.MapLike<{ content: string, version: number }>;
1616
private _languageService: ts.LanguageService;
1717

18-
constructor(compilerOptions?: ts.CompilerOptions) {
18+
constructor(compilerOptions?: object) {
1919

20-
this._compilerOptions = /* TS 2.0 */Object.assign({ skipLibCheck: true }, compilerOptions);
20+
function normalize(path: string): string {
21+
return path.replace(/\\/g, "/");
22+
}
23+
24+
const { errors, options } = ts.convertCompilerOptionsFromJson(
25+
/* TS 2.0 */Object.assign({ skipLibCheck: true }, compilerOptions || {}),
26+
normalize(process.cwd())
27+
);
28+
const [error] = errors;
29+
if (error) {
30+
throw error;
31+
}
32+
33+
this._compilerOptions = options;
2134
this._files = {};
2235

2336
const languageServiceHost: ts.LanguageServiceHost = {
2437

2538
directoryExists: ts.sys.directoryExists,
2639
fileExists: ts.sys.fileExists,
2740
getCompilationSettings: () => this._compilerOptions,
28-
getCurrentDirectory: () => process.cwd(),
41+
getCurrentDirectory: () => normalize(process.cwd()),
2942
getDefaultLibFileName: (options: ts.CompilerOptions) => ts.getDefaultLibFilePath(options),
3043
getScriptFileNames: () => Object.keys(this._files),
3144

0 commit comments

Comments
 (0)