-
-
Notifications
You must be signed in to change notification settings - Fork 27k
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 {paths} are not taken into consideration #5585
Comments
AFAIK Adding tsconfig-paths-webpack-plugin should solve this issue. |
@miraage but this would have to be done by CRA, because I'd like to avoid ejecting the config. |
@rolandjitsu Agree. It should have been done by CRA, if I had been right in my assumptions regarding babel/tsc. |
@miraage actually, So it's possible that path support needs to be added in both webpack and babel. Furthermore, jest also needs support for this (in react-scripts-ts this is done via jest |
https://babeljs.io/blog/2018/08/27/7.0.0#typescript-support-babel-preset-typescript
|
Currently this is done on purpose. We have no immediate plans to respect alternate module resolve modes/paths. |
EDIT: This workaround no longer works with the latest CRA versions where the TS config is routinely reset.This can be achieved without ejecting if you're willing to use react-app-rewired in combination with customize-cra. There's a big warning on the An example, setting up two aliases:
const path = require('path')
const { addWebpackAlias } = require('customize-cra')
module.exports = function override(config, env) {
config = addWebpackAlias({
['@ui']: path.resolve(__dirname, 'src', 'ui'),
['@src']: path.resolve(__dirname, 'src')
})(config)
return config
}
The |
Yeah, I currently have the following config which works fine on react-scripts-ts@3.1.0 however not here. "paths": {
"~/*": [
"src/*"
]
}, |
@Timer while I understand the decision, I do hope that this is somewhere on the roadmap. Clearly, some of us (e.g. @lee-reinhardt) go to great lengths to figure out how to make this work. I personally believe that it would be a valuable feature to have built in support for paths in CRA 2, and the effort to achieve this should be small, assuming it's just a matter of adding the webpack tsconfig-paths-webpack-plugin plugin. @jamsch yes, react-scripts-ts does have support for paths. And they're also working on wmonk/create-react-app-typescript#409 to upgrade to CRA 2. But they're debating whether it's worth continuing given that CRA 2 also has support for TS. The only difference, at the moment, between react-scripts and react-scripts-ts in terms of TS is path support and tslint (more or less). So adding path support here would eliminate the need of using react-scripts-ts which means that you do not need to wait for the maintainers of react-scripts-ts to upgrade CRA (they're usually doing a great job at being in sync with CRA) to get the newest features and that there are less chances for bugs to occur when the sync occurs. Nonetheless, I'd be more than happy to give this a shot if pointed in the right direction. EDIT: If babel is in charge of transforming ts to js, perhaps module-resolver could be an option? |
I agree with @rolandjitsu, this is a pretty big concern. Every Typescript app I have made exploited Paths as much as possible. One of the beauties of using TS. I currently have a few projects in react-scripts-ts that I was converting over this weekend until I kept running into issues with paths. That's what led me here. I really hope this is built into CRA sooner rather than later. |
FWIW, I've managed to add paths support via craco like this: First, a craco plugin is needed:
Then, it needs to be added to craco configuration:
Hope this will help someone on this thread. However, now I have to wonder whether it's all broken now due to #5609. |
Honestly I see paths as quite a necessity for any medium-large project because what inevitably happens is importing from the project root for components that are multiple levels deep which ends up like: // src/components/ui/Button/Square.tsx
import colors from '../../../../res/colors.ts'; // instead of '~/res/colors.ts'
// src/pages/auth/signin/SignIn.tsx
import * as Button from '../../../../components/ui/Button'; // instead of '~/components/ui/Button' Any further refactoring (such as moving components around) often end up messing the import path nesting unless IDE refactoring features save you. This is a must-have for my use case so I'll probably end up ejecting if this isn't on the cards for the CRA team. |
Closing as a duplicate of #5118. TypeScript support will ship without support for Edit: we do want to support this in the future, it just didn't make it into the initial release. See this comment: #5585 (comment). |
@Timer I'm a bit disappointed to see such resistance to add TS paths support. Paths were quite a big feature in TS and a lot of ppl were very happy to have it. |
@Timer Would you care to elaborate on why this decision has been made? Your comments have been rather short and non-informative. Is it really such a technical difficulty or it was left out just annoy developers and never use Typescript with CRA? Once you start using this TypeScript feature you really don't want to go back to these long '../../../..' paths. |
We've held this position for a long time. Allowing users to adjust the module resolution order is rather confusing and can lead to subtle bugs (i.e. aliasing something that conflicts with a npm package name). It's hard to get all tooling to agree on how the resolution is performed, as we need to consider: webpack, Jest, Flow, TypeScript itself, and other tooling end-users install. This isn't a hard no, but it's likely we'll only add support for absolute imports and not arbitrary aliasing (i.e. Due to this complexity, we'll ship support without Edit: we do want to support this in the future, it just didn't make it into the initial release. See this comment: #5585 (comment). |
CRA@2.0.4 not work, It will remove |
You're right. I'm currently stuck on |
You can use |
@jamsch Why to fork when there is |
Confirm, it works. But another problem is with "isolatedModules" is being set to "true", and the extending file does not overwrite this parameter. |
That's right. I assume that you need this to fix |
+1 to add this feature to TS project. People working with TS need Absolute Path or Alias config feature. Thanks |
This doesn't work for me - the moment I run the server the tsconfig gets reset and apparently the baseUrl doesn't get propagated to webpack. It is a shame that this does not work as expected. |
Just to be clear — we do want to support absolute paths. #5585 (comment) referred to what we ship in the first version with TypeScript to intentionally limit the scope. It doesn't mean we don't want this! :-) In particular, we want to:
I'm sorry for the frustration this is causing. We're not asking you to convert your code to use relative paths — but please wait for either of these two issues to resolve. I'll close this one but you can track either #5692 or #5118, and we'll let you know when we have something! |
@gaearon thanks for your response |
There is also another plausible alternative — we could embrace |
Edit from maintainers: we want to support this — see #5585 (comment) for details!
Is this a bug report?
Yes.
Did you try recovering your dependencies?
NPM 5.6.0
Yarn 1.7.0
Which terms did you search for in User Guide?
paths, paths map, path mappings, typescript path, typescript paths
Environment
Steps to Reproduce
TypeScript
{paths}
in{compilerOptions}
are not working.yarn install
at the rootcd apps/cra
yarn start
Expected Behavior
CRA should resolve the modules listed in
{paths}
.Actual Behavior
CRA cannot resolve the modules listed in
{paths}
.Reproducible Demo
cra-workspaces#cra-2, instructions listed above.
The text was updated successfully, but these errors were encountered: