LeDDGroup / typescript-transform-paths

Transforms module resolution paths using TypeScript path mapping and/or custom paths
MIT License
465 stars 22 forks source link

[Feature] Add an option to exclude by keys #175

Closed Sylphide closed 1 year ago

Sylphide commented 1 year ago

Hi,

Would it be possible to add an option to exclude resolution by key pattern in addition to the one with resolved path pattern? I am using an NX monorepo and I would like to transform only the paths of the current library but not the external ones.

For example I have this in my tsconfig.json

"paths": {
      "@myorg/database": ["libs/database/src/index.ts"],
      "@myorg/dtos": ["libs/dtos/src/index.ts"],
      "@/database/*": ["libs/database/src/*"],
      "@/dtos/*": ["libs/dtos/src/*"],
}

In my database lib I would use @/database/* to reference my lib root folder. Those paths I'd like to be transformed. On the contrary, I would reference dtos exported objects with @myorg/dtos and I don't want them to be transformed.

Since all libs resides at the same level, dtos would resolve to something like ../../../../dtos. The only exclude patterns working would have to include dtos only. I think it is not accurate enough and would prevent me from having another dtos folder in my database lib.

Hence my request to have an option that would allow to exclude by tsconfig keys like in my case something like "excludeKeys": ["@myorg/*"]

Thanks

danielpza commented 1 year ago

Have you tried https://github.com/LeDDGroup/typescript-transform-paths#exclusion-patterns?

Sylphide commented 1 year ago

Yes I tried, but I haven't found a pattern matching only external libs resolutions since the resolve at the same level as the current lib.

danielpza commented 1 year ago

Can you post what glob patterns you have tried?

Sylphide commented 1 year ago

The only patterns working were "**/dtos/**" and "**/dtos".

nonara commented 1 year ago

Thank you for the suggestion, @Sylphide.

For most cases, exclusion patterns should cover it. From what I understand, however, you want to be able to have a path alias that points to the same file as another path alias, but you'd like to only transform for a specific alias prefix. If that is correct, that would not be covered by exclusion patterns.

At this point, I don't think that's going to be a broad enough use case to warrant adding another config property, but if anyone wants to weigh in in the future, we may reconsider down the line.

In the mean time, I would suggest simply prefixing paths (where you can't use the exclusion pattern) with @no-transform tags to ensure they are preserved.