Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: handle ${configDir} TS template var in pathsToModuleNameMapper #4658

Open
mister-good-deal opened this issue Dec 27, 2024 · 0 comments

Comments

@mister-good-deal
Copy link

mister-good-deal commented Dec 27, 2024

🚀 Feature Proposal

In TS v5.5, there is a new template var ${configDir} in compilerOptions.paths definition see official doc

pathsToModuleNameMapper should handle it and replace this var with the correct path value.

  • I've read the code of the helper in src/config/paths-to-module-name-mapper.ts, it is already an if else soap so maybe adding a third parameter that would replace the templated string ${configDir} by the user provided string could be a basic solution. Although it could be tough to the user to provide the correct path at runtime.

  • A better solution would be finding the correct path to replace the templated string programmatically but without digging more in the code I didn't find a simple way to get the relative path of the config file path based on the root project directory.

Motivation

Keep being updated with TS standard

Example

The following project monorepo structure

monorepo/
├── tsconfig.base.json    # Base TypeScript config
├── jest.config.base.ts   # Base Jest config
└── packages/
    ├── <package_1>/
    │     ├── package.json    # Extends package-base.json
    │     ├── tsconfig.json   # Extends tsconfig.base.json
    │     └── jest.config.ts  # Extends jest.config.base.ts
    └── <package_2>/
            ├── package.json    # Extends package-base.json
            ├── tsconfig.json   # Extends tsconfig.base.json
            └── jest.config.ts  # Extends jest.config.base.ts

with a defined tsconfig.base.json

{
    "compilerOptions": {
        "rootDir": "./",
        "paths": {
            "@monorepo/*": ["./packages/*"],
            "@database/*": ["${configDir}/prisma/*"]
        },
    "include": [ "jest.config.base.ts" ]
}

would resolve @database as '^@database/(.*)$': '/path/to/monorepo/packages/package_1/prisma/$1' when loading from package_1/tsconfig.json.

I know it would be much more easier to just defined @database at tsconfig.json package level but unfortunately TS does not let extend the compilerOptions.paths property with the root one, it overwrites it...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant