diff --git a/package.json b/package.json index 9fc61f2..173bd1a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@feature-sliced/filesystem", - "version": "2.2.1", + "version": "2.2.2", "description": "A set of utilities for locating and working with FSD roots in the file system.", "scripts": { "build": "tsup src/index.ts --dts --format esm,cjs --clean", diff --git a/src/resolve-import.ts b/src/resolve-import.ts index 6d1e501..b277d12 100644 --- a/src/resolve-import.ts +++ b/src/resolve-import.ts @@ -36,15 +36,65 @@ import type { CompilerOptions } from "typescript"; export function resolveImport( importedPath: string, importerPath: string, - tsCompilerOptions: CompilerOptions, + tsCompilerOptions: ImperfectCompilerOptions, fileExists: (path: string) => boolean, directoryExists?: (path: string) => boolean, ): string | null { + console.log(normalizeCompilerOptions(tsCompilerOptions)); return ( - ts.resolveModuleName(importedPath, importerPath, tsCompilerOptions, { - ...ts.sys, - fileExists, - directoryExists, - }).resolvedModule?.resolvedFileName ?? null + ts.resolveModuleName( + importedPath, + importerPath, + normalizeCompilerOptions(tsCompilerOptions), + { + ...ts.sys, + fileExists, + directoryExists, + }, + ).resolvedModule?.resolvedFileName ?? null ); } + +const imperfectKeys = { + module: ts.ModuleKind, + moduleResolution: ts.ModuleResolutionKind, + moduleDetection: ts.ModuleDetectionKind, + newLine: ts.NewLineKind, + target: ts.ScriptTarget, +}; + +/** TypeScript has a few fields which have values from an internal enum, and refuses to take the literal values from the tsconfig.json. */ +function normalizeCompilerOptions( + compilerOptions: ImperfectCompilerOptions, +): CompilerOptions { + return Object.fromEntries( + Object.entries(compilerOptions).map(([key, value]) => { + if ( + Object.keys(imperfectKeys).includes(key) && + typeof value === "string" + ) { + for (const [enumKey, enumValue] of Object.entries( + imperfectKeys[key as keyof typeof imperfectKeys], + )) { + if (enumKey.toLowerCase() === value.toLowerCase()) { + return [key, enumValue]; + } + } + } + return [key, value]; + }), + ) as CompilerOptions; +} + +export interface ImperfectCompilerOptions + extends Omit { + module?: ts.ModuleKind | keyof typeof ts.ModuleKind; + moduleResolution?: + | ts.ModuleResolutionKind + | keyof typeof ts.ModuleResolutionKind; + moduleDetection?: + | ts.ModuleDetectionKind + | keyof typeof ts.ModuleDetectionKind; + newLine?: ts.NewLineKind | keyof typeof ts.NewLineKind; + target?: ts.ScriptTarget | keyof typeof ts.ScriptTarget; +} diff --git a/src/specs/resolve-import.spec.ts b/src/specs/resolve-import.spec.ts index c5d5ada..b376ac7 100644 --- a/src/specs/resolve-import.spec.ts +++ b/src/specs/resolve-import.spec.ts @@ -1,10 +1,9 @@ import { expect, test } from "vitest"; -import { ModuleResolutionKind } from "typescript"; import { resolveImport } from "../index.js"; test("Basic", () => { const tsCompilerOptions = { - moduleResolution: ModuleResolutionKind.Bundler, + moduleResolution: "Bundler" as const, baseUrl: ".", paths: { "~/*": ["./src/*"],