Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incremental compiler support #895

Merged
merged 12 commits into from
Oct 12, 2019
Prev Previous commit
Next Next commit
Implement compiler host object for older TS
  • Loading branch information
blakeembrey committed Oct 12, 2019
commit 5ff74b764bae1c9f557cd029afd62df1260ad095
32 changes: 17 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,10 @@ export function register (opts: Options = {}): Register {
}

const sys = {
args: ts.sys.args,
newLine: ts.sys.newLine,
useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames,
writeFile: ts.sys.writeFile,
write: ts.sys.write,
...ts.sys,
readFile: (fileName: string) => {
if (memoryCache.fileContents.has(fileName)) {
return memoryCache.fileContents.get(fileName)
}

const cacheContents = memoryCache.fileContents.get(fileName)
if (cacheContents !== undefined) return cacheContents
return cachedReadFile(fileName)
},
readDirectory: cachedLookup(debugFn('readDirectory', ts.sys.readDirectory)),
Expand All @@ -306,16 +300,24 @@ export function register (opts: Options = {}): Register {
directoryExists: cachedLookup(debugFn('directoryExists', ts.sys.directoryExists)),
resolvePath: cachedLookup(debugFn('resolvePath', ts.sys.resolvePath)),
realpath: ts.sys.realpath ? cachedLookup(debugFn('realpath', ts.sys.realpath)) : undefined,
createDirectory: ts.sys.createDirectory,
getExecutingFilePath: ts.sys.getExecutingFilePath,
getCurrentDirectory: () => cwd,
exit: ts.sys.exit,
createHash: ts.sys.createHash
getCurrentDirectory: () => cwd
}

const host: _ts.CompilerHost = ts.createIncrementalCompilerHost
? ts.createIncrementalCompilerHost(config.options, sys)
: (ts as any).createCompilerHostWorker(config.options, undefined, sys)
: {
...sys,
getSourceFile: (fileName, languageVersion) => {
const contents = sys.readFile(fileName)
if (contents === undefined) return
return ts.createSourceFile(fileName, contents, languageVersion)
},
getDefaultLibLocation: () => normalizeSlashes(dirname(compiler)),
getDefaultLibFileName: () => normalizeSlashes(join(dirname(compiler), ts.getDefaultLibFileName(config.options))),
getCanonicalFileName: sys.useCaseSensitiveFileNames ? x => x : x => x.toLowerCase(),
getNewLine: () => sys.newLine,
useCaseSensitiveFileNames: () => sys.useCaseSensitiveFileNames
}

// Fallback for older TypeScript releases without incremental API.
let builderProgram = ts.createIncrementalProgram
Expand Down