question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

tsconfig.json gets overwritten preventing real configuration

See original GitHub issue

I like CRA, especially now when I learned how to override some of its configs thanks to beautiful https://github.com/gsoft-inc/craco

And the first thing I use craco for - is fixing webpack config setting up project resolves and aliases.

For example, this craco.config.js:

const path = require('path');

module.exports = {
  webpack: {
    configure: webpackConfig => {
      webpackConfig.resolve.alias['components'] = path.resolve(__dirname, 'src', 'components');
      webpackConfig.resolve.alias['state'] = path.resolve(__dirname, 'src', 'state');
      return webpackConfig;
    }
  }
};

adds two aliases: components and state allowing us to import things using sensible paths (at long last):

import App from 'components/app';
import { loginUser } from 'state/users/actions';
// ...

and this works perfectly, but only for JavaScript, not for TypeScript. And this despite they state it should acquire path resolutions from webpack, but this never happens, probably because of craco.

To workaround this one needs to provide additional configuration for TypeScript in tsconfig.json:

{
  "compilerOptions": {
    // ...
    "baseUrl": ".",
    "paths": {
      "components/*": ["src/components/*"],
      "state/*": ["src/state/*"],
    }
  }
}

but… this is not going to work anyway, because react-scripts is overwriting tsconfig.json, removing “paths” section! As result I had to eject the project just to get this silly resolution working. I think this is a bug, because as I said overriding webpack resolve via craco works fine for JavaScript and thus must also work for TypeScript.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
hrguicommented, Mar 7, 2020

Here’s an example sandbox with using baseUrl = 'src' as an alternative to the paths described in this issue w/ JavaScript / TypeScript combined.

https://codesandbox.io/s/broken-glitter-0hkxy

0reactions
stale[bot]commented, Apr 12, 2020

This issue has been automatically closed because it has not had any recent activity. If you have a question or comment, please open a new issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

tsconfig.json gets reset after yarn start React app
There's a workaround. Create a new file, say "base-tsconfig.json" and put the baseUrl config and paths in there. Those won't be overwritten ......
Read more >
TSConfig Reference - Docs on every TSConfig option
The configuration from the base file are loaded first, then overridden by those in the inheriting config file. All relative paths found in...
Read more >
API - ESBuild
API. The API can be accessed in one of three ways: on the command line, in JavaScript, and in Go. The concepts and...
Read more >
How to configure react-script so that it doesn't override tsconfig ...
Put the configuration options react scripts likes to remove in a separate file (e.g. paths.json) and reference it from tsconfig.json via the extends...
Read more >
React with TypeScript: Best Practices - SitePoint
We'll discuss project setup including: tsconfig.json; ESLint; Prettier; VS Code extensions and settings. Project Setup. The quickest way to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found