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

[ts-interface-generator] Support TS ${configDir} in path mappings for source exports #484

Open
stabpd opened this issue Jan 13, 2025 · 0 comments
Labels
ts-interface-generator Related to the ts-interface-generator sub-package

Comments

@stabpd
Copy link

stabpd commented Jan 13, 2025

Describe the bug
Source exports are not or incorrectly resolved when using TS config file template variable ${configDir} in path mappings.

// tsconfig.json
{
    "compilerOptions": {
        // [...]
        "paths": {
            "mylib/*": ["${configDir}/src/mylib/*"] // <- PROBLEM: This is resolved to an absolute path by TS.
                                                    // Joining basePath with pathMattingTarget does not work in that case.
                                                    // ("./src/mylib/*" works though)
        }
    },
    "include": ["${configDir}/src/**/*"] // <- no issue here
}

That then results in an error when trying to resolve imports for interfaces.

For the type 'mylib.MyEnum' an import is created with module name 'mylib/MyEnum', using its default export. Is this correct?
Usually this indicates some kind of issue. Maybe this import will also show up as error in your code editor.

Expected behavior
I'd expect the same behaviour for the same path mapping targets, no matter if specified relatively or with ${configDir}.

Additional context
ts-interface-generator expects relative path mapping targets and applies path.join in packages/ts-interface-generator/src/addSourceExports.ts#L39.

Using path.resolve instead seems to fix the issue for me although I do not know

  1. if this introduces other issues and
  2. if other changes would be needed to fully support ${configDir} in all places / features.
- const fullTargetPath = path.join(basePath, allPathMappings[i].target);
+ const fullTargetPath = path.resolve(basePath, allPathMappings[i].target);

Debugged values with current implementation and ${configDir}:

variable value
basePath /my/project/root
allPathMappings[i].target /my/project/root/src/mylib/
fullTargetPath /my/project/root/my/project/root/src/mylib/ ⚠️
globalExportName undefined.MyEnum ⚠️
@akudev akudev added the ts-interface-generator Related to the ts-interface-generator sub-package label Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ts-interface-generator Related to the ts-interface-generator sub-package
Projects
None yet
Development

No branches or pull requests

2 participants