-
Notifications
You must be signed in to change notification settings - Fork 246
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
TypeScript path aliasses #865
Comments
Can you please laborite on what the use-case for path aliases is, so we can determine whether this is a feature that we should support? |
The use case is pretty code. The only solution for this is to write the relative path out in full: import { NameGen } from '../../../../infra/lib/NameGen';
import { MatchingStack } from '../../../../infra/MatchingStack'; In the code I generate for AWS lambda, I use path aliases to get rid of require('@lib/bootstrap');
import FreelancerActivatedEventBody from '@event-handlers/FreelancerActivated/FreelancerActivatedEventBody';
import { SnsEventHandler } from '@lib/SnsEventHandler'; It makes the imports a lot more humanreadable and cleans up the code. I hope clean code is a good enough reason to implement is, as clean code is a high priority for readability and maintainability IMO |
We should stop including |
Is this related to my issue? Or related to the JSII compiler? |
This issue is still present in cdk as well (it uses JSII). In my case I would love to use ts paths in order to provide locators for my lambdas code. |
I'm contemplating whether we should allow user-controlled What do you guys think about this idea? |
It seems simple enough to parse a user provided tsconfig.json if present and merge it with the required/default tsconfig for jsii. We should just warn when a user is including some config that is incompatible with JSII and we are overriding it. Or crash with non-zero. |
This also effects lerna (yarn workspace) based projects, as workspace packages can not be resolved to
|
@JeremyJonas - I'm not sure I understand how your project is set up. The Would you be able to explain your project structure in more tail, or better yet, share a pointer to an example repository that uses it? |
@RomainMuller I’ll try to send example code next week when get back to this. But basically I have a /packages/... with typescript packages. Those packages use the above tsconfig to reference each other. Using the paths they reference the /src code rather than /dist declaration files for type, which works great. These are all included in lerna/yarn workspaces. I then have a /infra dir at root that is also included as lerna/yarn workspace. It has similar tsconfig file that includes paths but to the ../packages relative to that dir. But since cdk tsconfig doesn’t support alias paths it doesn’t work. But it does resolve to node_modules symlink of those /packages. But because node_modules resolution is based on “main” which is /dist/index.d.ts from complies typescript it requires first building all the packages before /infra cdk package can use them. Hope that makes sense. Appreciate you guidance and support! Will provide more details if needed next week. |
Have you tried out the |
@RomainMuller Do you have a example of that working? Docs are pretty sparse on that, only thing I found was aws/aws-cdk#1466. Just tried to setup tsconfig references and same issue, need to build first. If that is like tsconfig "references", does it have the same limitation of having to build referenced packages before they can be references? https://www.typescriptlang.org/docs/handbook/project-references.html#caveats-for-project-references If that is the case, basically same issue I have. Currently with my lerna/yarn project everything is in root node_modules, so after building it just works. But with aliases no need to build, can reference src directly :) |
This feature would definitely be a quality of life improvement, to avoid doing: import {x} from '../../../../folder/file' And to be consistent with frontend imports, I would assume most people don't you relative imports, when working in typescript. |
I ran into this issue today, Its a shame I have to use relative imports in my microservices. The way my project is structured is as follows. Tooling
Structure
Problem
Current Workaround
|
Is this issue still a thing? |
I just ran into it I think, so seemingly, yes. :-/ |
Not an issue in our project anymore. |
@moltar Well how did you solve it then? I am ASTONISHED cdk doesn't support path aliases, which essentially breaks my entire backend workflow. @RomainMuller please add this feature to cdk. |
Here's a demo, hope that helps: https://github.com/moltar/cdk-ts-path-mapping |
Kind of surprising that this is still an issue 🤔 As @Pidz-b said
Agreed 100%. And
Having to install tsconfig-paths and then adding Relative paths are bad practice. |
Allow user control of the `baseUrl` and `paths` compiler options in `tsconfig.json` via the `jsii.tsc` stanza in `package.json`. Fixes #865
Allow user control of the `baseUrl` and `paths` compiler options in `tsconfig.json` via the `jsii.tsc` stanza in `package.json`. Fixes #865 --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
|
Hello @RomainMuller, thanks for merging this feature as per #3662, this means the next cdk release will include the latest changes in jsii (namely this feature) ? |
🚀 Feature Request
Affected Languages
TypeScript
orJavascript
Python
Java
C#
,F#
, ...)General Information
What is the problem?
When running
cdk deploy
orcdk synth
, a path alias defined in in tsconfig.json is not resolved to the correct path.I have all my CDK code in a folder named
<project root>/infra/
In one of my files I import a custom file that abstracts our naming:
The tsconfig.json paths look like:
When running CDK, the following error is thrown:
Cannot find module '@infra/lib/NameGen'
Apparently JSII does not resolve the path aliasing of the tsconfig correctly?
The text was updated successfully, but these errors were encountered: