Skip to content

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

@mister-good-deal

Description

@mister-good-deal

🚀 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...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions