diff --git a/src/configuration.ts b/src/configuration.ts index e961265b1..5e2b9927c 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -172,7 +172,8 @@ export function readConfig( // Some options are relative to the config file, so must be converted to absolute paths here if (options.require) { // Modules are found relative to the tsconfig file, not the `dir` option - const tsconfigRelativeResolver = createProjectLocalResolveHelper(configPath); + const tsconfigRelativeResolver = + createProjectLocalResolveHelper(configPath); options.require = options.require.map((path: string) => tsconfigRelativeResolver(path, false) ); diff --git a/src/index.ts b/src/index.ts index 45006cc97..6e0104020 100644 --- a/src/index.ts +++ b/src/index.ts @@ -596,7 +596,8 @@ export function create(rawOptions: CreateOptions = {}): Service { * be changed by the tsconfig, so we have to do this twice. */ function loadCompiler(name: string | undefined, relativeToPath: string) { - const projectLocalResolveHelper = createProjectLocalResolveHelper(relativeToPath); + const projectLocalResolveHelper = + createProjectLocalResolveHelper(relativeToPath); const compiler = projectLocalResolveHelper(name || 'typescript', true); const ts: typeof _ts = attemptRequireWithV8CompileCache(require, compiler); return { compiler, ts, projectLocalResolveHelper }; @@ -605,7 +606,12 @@ export function create(rawOptions: CreateOptions = {}): Service { // Compute minimum options to read the config file. let { compiler, ts, projectLocalResolveHelper } = loadCompiler( compilerName, - getBasePathForProjectLocalDependencyResolution(undefined, rawOptions.projectSearchDir, rawOptions.project, cwd) + getBasePathForProjectLocalDependencyResolution( + undefined, + rawOptions.projectSearchDir, + rawOptions.project, + cwd + ) ); // Read config file and merge new options between env and CLI options. @@ -627,8 +633,15 @@ export function create(rawOptions: CreateOptions = {}): Service { // Compiler is loaded relative to tsconfig.json, so tsconfig discovery may cause us to load a // different compiler than we did above, even if the name has not changed. if (configFilePath) { - ({ compiler, ts, projectLocalResolveHelper } = loadCompiler(options.compiler, - getBasePathForProjectLocalDependencyResolution(configFilePath, rawOptions.projectSearchDir, rawOptions.project, cwd))); + ({ compiler, ts, projectLocalResolveHelper } = loadCompiler( + options.compiler, + getBasePathForProjectLocalDependencyResolution( + configFilePath, + rawOptions.projectSearchDir, + rawOptions.project, + cwd + ) + )); } // Experimental REPL await is not compatible targets lower than ES2018 diff --git a/src/resolver-functions.ts b/src/resolver-functions.ts index f478646b1..f032bf0a8 100644 --- a/src/resolver-functions.ts +++ b/src/resolver-functions.ts @@ -14,8 +14,14 @@ export function createResolverFunctions(kwargs: { config: _ts.ParsedCommandLine; projectLocalResolveHelper: ProjectLocalResolveHelper; }) { - const { host, ts, config, cwd, getCanonicalFileName, projectLocalResolveHelper } = - kwargs; + const { + host, + ts, + config, + cwd, + getCanonicalFileName, + projectLocalResolveHelper, + } = kwargs; const moduleResolutionCache = ts.createModuleResolutionCache( cwd, getCanonicalFileName, @@ -137,7 +143,10 @@ export function createResolverFunctions(kwargs: { // Resolve @types/node relative to project first, then __dirname (copy logic from elsewhere / refactor into reusable function) let typesNodePackageJsonPath: string | undefined; try { - typesNodePackageJsonPath = projectLocalResolveHelper('@types/node/package.json', true); + typesNodePackageJsonPath = projectLocalResolveHelper( + '@types/node/package.json', + true + ); } catch {} // gracefully do nothing when @types/node is not installed for any reason if (typesNodePackageJsonPath) { const typeRoots = [resolve(typesNodePackageJsonPath, '../..')]; diff --git a/src/transpilers/swc.ts b/src/transpilers/swc.ts index c0f28ef12..fedc6a3af 100644 --- a/src/transpilers/swc.ts +++ b/src/transpilers/swc.ts @@ -21,7 +21,10 @@ export function create(createOptions: SwcTranspilerOptions): Transpiler { // Load swc compiler let swcInstance: typeof swcWasm; if (typeof swc === 'string') { - swcInstance = require(projectLocalResolveHelper(swc, true)) as typeof swcWasm; + swcInstance = require(projectLocalResolveHelper( + swc, + true + )) as typeof swcWasm; } else if (swc == null) { let swcResolved; try { diff --git a/src/transpilers/types.ts b/src/transpilers/types.ts index 425005c38..ab524cbdc 100644 --- a/src/transpilers/types.ts +++ b/src/transpilers/types.ts @@ -17,7 +17,10 @@ export type TranspilerFactory = ( export interface CreateTranspilerOptions { // TODO this is confusing because its only a partial Service. Rename? // Careful: must avoid stripInternal breakage by guarding with Extract<> - service: Pick>; + service: Pick< + Service, + Extract<'config' | 'options' | 'projectLocalResolveHelper', keyof Service> + >; } export interface Transpiler { // TODOs diff --git a/src/util.ts b/src/util.ts index 87217667f..ca8536d8a 100644 --- a/src/util.ts +++ b/src/util.ts @@ -122,20 +122,32 @@ export function attemptRequireWithV8CompileCache( * @internal */ export function createProjectLocalResolveHelper(localDirectory: string) { - return function projectLocalResolveHelper(specifier: string, fallbackToTsNodeRelative: boolean) { + return function projectLocalResolveHelper( + specifier: string, + fallbackToTsNodeRelative: boolean + ) { return require.resolve(specifier, { - paths: fallbackToTsNodeRelative ? [localDirectory, __dirname] : [localDirectory] + paths: fallbackToTsNodeRelative + ? [localDirectory, __dirname] + : [localDirectory], }); - } + }; } /** @internal */ -export type ProjectLocalResolveHelper = ReturnType; +export type ProjectLocalResolveHelper = ReturnType< + typeof createProjectLocalResolveHelper +>; /** * Used as a reminder of all the factors we must consider when finding project-local dependencies and when a config file * on disk may or may not exist. * @internal */ -export function getBasePathForProjectLocalDependencyResolution(configFilePath: string | undefined, projectSearchDirOption: string | undefined, projectOption: string | undefined, cwdOption: string) { +export function getBasePathForProjectLocalDependencyResolution( + configFilePath: string | undefined, + projectSearchDirOption: string | undefined, + projectOption: string | undefined, + cwdOption: string +) { return configFilePath ?? projectSearchDirOption ?? projectOption ?? cwdOption; }