Skip to content

Commit

Permalink
Use lib.es6.d.ts when targeting ES6 (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivogabe committed May 1, 2015
1 parent 5f79f13 commit facd691
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
8 changes: 7 additions & 1 deletion lib/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ export class ProjectCompiler implements ICompiler {
return;
}

this.host = new host.Host(this.project.typescript, this.project.currentDirectory, this.project.input, !this.project.noExternalResolve);
this.host = new host.Host(
this.project.typescript,
this.project.currentDirectory,
this.project.input,
!this.project.noExternalResolve,
this.project.options.target >= ts.ScriptTarget.ES6 ? 'lib.es6.d.ts' : 'lib.d.ts'
);

let rootFilenames: string[] = this.project.input.getFileNames(true);

Expand Down
11 changes: 7 additions & 4 deletions lib/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import path = require('path');

export class Host implements ts.CompilerHost {
static libDefault: utils.Map<ts.SourceFile> = {};
static getLibDefault(typescript: typeof ts) {
static getLibDefault(typescript: typeof ts, libFileName: string) {
let fileName: string;
for (const i in require.cache) {
if (!Object.prototype.hasOwnProperty.call(require.cache, i)) continue;
Expand All @@ -23,28 +23,31 @@ export class Host implements ts.CompilerHost {
if (fileName === undefined) {
return undefined; // Not found
}
fileName = path.join(path.dirname(fileName), libFileName);
if (this.libDefault[fileName]) {
return this.libDefault[fileName]; // Already loaded
}

const content = fs.readFileSync(path.resolve(path.dirname(fileName) + '/lib.d.ts')).toString('utf8');
const content = fs.readFileSync(fileName).toString('utf8');
return this.libDefault[fileName] = tsApi.createSourceFile(typescript, '__lib.d.ts', content, typescript.ScriptTarget.ES3); // Will also work for ES5 & 6
}

typescript: typeof ts;

currentDirectory: string;
private externalResolve: boolean;
private libFileName: string;
input: input.FileCache;
output: utils.Map<string>;

constructor(typescript: typeof ts, currentDirectory: string, input: input.FileCache, externalResolve: boolean) {
constructor(typescript: typeof ts, currentDirectory: string, input: input.FileCache, externalResolve: boolean, libFileName: string) {
this.typescript = typescript;

this.currentDirectory = currentDirectory;
this.input = input;

this.externalResolve = externalResolve;
this.libFileName = libFileName;

this.reset();
}
Expand Down Expand Up @@ -79,7 +82,7 @@ export class Host implements ts.CompilerHost {

getSourceFile = (fileName: string, languageVersion: ts.ScriptTarget, onError?: (message: string) => void): ts.SourceFile => {
if (fileName === '__lib.d.ts') {
return Host.getLibDefault(this.typescript);
return Host.getLibDefault(this.typescript, this.libFileName);
}

let sourceFile = this.input.getFile(fileName);
Expand Down

0 comments on commit facd691

Please sign in to comment.