-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove tsconfig dependency and add support for tsconfig extends
- Loading branch information
Showing
4 changed files
with
198 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
import * as Tsconfig from "tsconfig"; | ||
import * as path from "path"; | ||
import * as fs from "fs"; | ||
import * as deepmerge from "deepmerge"; | ||
import * as StripJsonComments from "strip-json-comments"; | ||
import StripBom = require("strip-bom"); | ||
|
||
export interface TsConfigLoaderResult { | ||
tsConfigPath: string | undefined; | ||
|
@@ -26,11 +30,83 @@ export function tsConfigLoader({ | |
|
||
function loadSyncDefault(cwd: string, filename?: string): TsConfigLoaderResult { | ||
// Tsconfig.loadSync uses path.resolve. This is why we can use an absolute path as filename | ||
const loadResult = Tsconfig.loadSync(cwd, filename); | ||
|
||
const configPath = resolveConfigPath(cwd, filename); | ||
|
||
if (!configPath) { | ||
return { | ||
tsConfigPath: undefined, | ||
baseUrl: undefined, | ||
paths: undefined | ||
}; | ||
} | ||
const config = loadConfig(configPath); | ||
|
||
return { | ||
tsConfigPath: loadResult.path, | ||
baseUrl: loadResult.config.compilerOptions.baseUrl, | ||
paths: loadResult.config.compilerOptions.paths | ||
tsConfigPath: configPath, | ||
baseUrl: config && config.compilerOptions && config.compilerOptions.baseUrl, | ||
paths: config && config.compilerOptions && config.compilerOptions.paths | ||
}; | ||
} | ||
|
||
export function resolveConfigPath( | ||
cwd: string, | ||
filename?: string | ||
): string | undefined { | ||
if (filename) { | ||
const absolutePath = fs.lstatSync(filename).isDirectory() | ||
? path.resolve(filename, "./tsconfig.json") | ||
: path.resolve(cwd, filename); | ||
|
||
return absolutePath; | ||
} | ||
|
||
return walkForTsConfig(cwd); | ||
} | ||
|
||
export function walkForTsConfig( | ||
directory: string, | ||
existsSync: (path: string) => boolean = fs.existsSync | ||
): string | undefined { | ||
const configPath = path.resolve(directory, "./tsconfig.json"); | ||
if (existsSync(configPath)) { | ||
return configPath; | ||
} | ||
|
||
const parentDirectory = path.resolve(directory, "../"); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Jontem
Author
Collaborator
|
||
|
||
// If we reached the top | ||
if (directory === parentDirectory) { | ||
return undefined; | ||
} | ||
|
||
return walkForTsConfig(parentDirectory, existsSync); | ||
} | ||
|
||
export function loadConfig( | ||
configFilePath: string, | ||
existsSync: (path: string) => boolean = fs.existsSync, | ||
readFileSync: (filename: string) => string = (filename: string) => | ||
fs.readFileSync(filename, "utf8") | ||
): { [key: string]: any } | undefined { | ||
if (!existsSync(configFilePath)) { | ||
return undefined; | ||
} | ||
|
||
const configString = readFileSync(configFilePath); | ||
const cleanedJson = StripBom(StripJsonComments(configString)); | ||
const config = JSON.parse(cleanedJson); | ||
|
||
if (config.extends) { | ||
const currentDir = path.dirname(configFilePath); | ||
const base = | ||
loadConfig( | ||
path.resolve(currentDir, config.extends), | ||
existsSync, | ||
readFileSync | ||
) || {}; | ||
|
||
return deepmerge(base, config); | ||
} | ||
return config; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hi @Jontem, do you remember if using
path.resolve
instead ofpath.dirname
was important here? I think they both do the same thing in this instance, yes?