Skip to content

Commit

Permalink
Add support for imperfect TSConfig compiler options
Browse files Browse the repository at this point in the history
  • Loading branch information
illright committed Jun 4, 2024
1 parent 009b685 commit 44a11fb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
62 changes: 56 additions & 6 deletions src/resolve-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompilerOptions, keyof typeof imperfectKeys> {
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;
}
3 changes: 1 addition & 2 deletions src/specs/resolve-import.spec.ts
Original file line number Diff line number Diff line change
@@ -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/*"],
Expand Down

0 comments on commit 44a11fb

Please sign in to comment.