Skip to content

Commit 3c59b9f

Browse files
committed
Add getDefaultLibraryFilePath to retrive the default lib file path for node package consumers
1 parent 118b7a9 commit 3c59b9f

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

src/compiler/core.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,10 @@ module ts {
633633
}
634634
}
635635

636+
export function getDefaultLibraryFilename(options: CompilerOptions): string {
637+
return options.target === ScriptTarget.ES6 ? "lib.es6.d.ts" : "lib.d.ts";
638+
}
639+
636640
export interface ObjectAllocator {
637641
getNodeConstructor(kind: SyntaxKind): new () => Node;
638642
getSymbolConstructor(): new (flags: SymbolFlags, name: string) => Symbol;

src/compiler/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ module ts {
6464

6565
return {
6666
getSourceFile,
67-
getDefaultLibFilename: options => combinePaths(getDirectoryPath(normalizePath(sys.getExecutingFilePath())), options.target === ScriptTarget.ES6 ? "lib.es6.d.ts" : "lib.d.ts"),
67+
getDefaultLibFilename: options => combinePaths(getDirectoryPath(normalizePath(sys.getExecutingFilePath())), getDefaultLibraryFilename(options)),
6868
writeFile,
6969
getCurrentDirectory: () => currentDirectory || (currentDirectory = sys.getCurrentDirectory()),
7070
useCaseSensitiveFileNames: () => sys.useCaseSensitiveFileNames,

src/services/services.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5720,6 +5720,23 @@ module ts {
57205720
return { getClassificationsForLine };
57215721
}
57225722

5723+
/// getDefaultLibraryFilePath
5724+
declare var __dirname: string;
5725+
declare var module: any;
5726+
5727+
/**
5728+
* Get the path of the default library file (lib.d.ts) as distributed with the typescript
5729+
* node package.
5730+
* The functionality is not supported if the ts module is consumed outside of a node module.
5731+
*/
5732+
export function getDefaultLibraryFilePath(options: CompilerOptions): string {
5733+
if (typeof module !== "undefined" && module.exports) {
5734+
return __dirname + directorySeparator + getDefaultLibraryFilename(options);
5735+
}
5736+
5737+
throw new Error("getDefaultLibraryFilename is only supported when consumed as a node module. ");
5738+
}
5739+
57235740
function initializeServices() {
57245741
objectAllocator = {
57255742
getNodeConstructor: kind => {

0 commit comments

Comments
 (0)