-
-
Notifications
You must be signed in to change notification settings - Fork 534
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement "extends" support for "ts-node" options in tsconfigs (#1356)
* Implement "extends" support for "ts-node" options in tsconfigs * remove once util; is not used * WIP test * fix * Finish test * oops forgot this
- Loading branch information
Showing
9 changed files
with
264 additions
and
53 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
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
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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { isAbsolute, resolve } from 'path'; | ||
import { cachedLookup, normalizeSlashes } from './util'; | ||
import type * as _ts from 'typescript'; | ||
import type { TSCommon, TSInternal } from './ts-compiler-types'; | ||
|
||
export const createTsInternals = cachedLookup(createTsInternalsUncached); | ||
/** | ||
* Given a reference to the TS compiler, return some TS internal functions that we | ||
* could not or did not want to grab off the `ts` object. | ||
* These have been copy-pasted from TS's source and tweaked as necessary. | ||
*/ | ||
function createTsInternalsUncached(_ts: TSCommon) { | ||
const ts = _ts as TSCommon & TSInternal; | ||
/** | ||
* Copied from: | ||
* https://github.com/microsoft/TypeScript/blob/v4.3.2/src/compiler/commandLineParser.ts#L2821-L2846 | ||
*/ | ||
function getExtendsConfigPath( | ||
extendedConfig: string, | ||
host: _ts.ParseConfigHost, | ||
basePath: string, | ||
errors: _ts.Push<_ts.Diagnostic>, | ||
createDiagnostic: ( | ||
message: _ts.DiagnosticMessage, | ||
arg1?: string | ||
) => _ts.Diagnostic | ||
) { | ||
extendedConfig = normalizeSlashes(extendedConfig); | ||
if ( | ||
isRootedDiskPath(extendedConfig) || | ||
startsWith(extendedConfig, './') || | ||
startsWith(extendedConfig, '../') | ||
) { | ||
let extendedConfigPath = getNormalizedAbsolutePath( | ||
extendedConfig, | ||
basePath | ||
); | ||
if ( | ||
!host.fileExists(extendedConfigPath) && | ||
!endsWith(extendedConfigPath, ts.Extension.Json) | ||
) { | ||
extendedConfigPath = `${extendedConfigPath}.json`; | ||
if (!host.fileExists(extendedConfigPath)) { | ||
errors.push( | ||
createDiagnostic(ts.Diagnostics.File_0_not_found, extendedConfig) | ||
); | ||
return undefined; | ||
} | ||
} | ||
return extendedConfigPath; | ||
} | ||
// If the path isn't a rooted or relative path, resolve like a module | ||
const resolved = ts.nodeModuleNameResolver( | ||
extendedConfig, | ||
combinePaths(basePath, 'tsconfig.json'), | ||
{ moduleResolution: ts.ModuleResolutionKind.NodeJs }, | ||
host, | ||
/*cache*/ undefined, | ||
/*projectRefs*/ undefined, | ||
/*lookupConfig*/ true | ||
); | ||
if (resolved.resolvedModule) { | ||
return resolved.resolvedModule.resolvedFileName; | ||
} | ||
errors.push( | ||
createDiagnostic(ts.Diagnostics.File_0_not_found, extendedConfig) | ||
); | ||
return undefined; | ||
} | ||
|
||
function startsWith(str: string, prefix: string): boolean { | ||
return str.lastIndexOf(prefix, 0) === 0; | ||
} | ||
function endsWith(str: string, suffix: string): boolean { | ||
const expectedPos = str.length - suffix.length; | ||
return expectedPos >= 0 && str.indexOf(suffix, expectedPos) === expectedPos; | ||
} | ||
|
||
// These functions have alternative implementation to avoid copying too much from TS | ||
function isRootedDiskPath(path: string) { | ||
return isAbsolute(path); | ||
} | ||
function combinePaths( | ||
path: string, | ||
...paths: (string | undefined)[] | ||
): string { | ||
return normalizeSlashes( | ||
resolve(path, ...(paths.filter((path) => path) as string[])) | ||
); | ||
} | ||
function getNormalizedAbsolutePath( | ||
fileName: string, | ||
currentDirectory: string | undefined | ||
) { | ||
return normalizeSlashes( | ||
currentDirectory != null | ||
? resolve(currentDirectory!, fileName) | ||
: resolve(fileName) | ||
); | ||
} | ||
|
||
return { getExtendsConfigPath }; | ||
} |
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
Empty file.
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"ts-node": { | ||
"require": ["./require-hook"], | ||
"scopeDir": "./scopedir" | ||
} | ||
} |
Oops, something went wrong.