You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add a way to specify an alternate tsconfig for production builds #6023
No. This is a feature request. Our team is now using CRA with TS and it is great! One thing we can't seem to find is any information on is having different TS compiler settings when doing a production build. Simply adding a tsconfig.prod.json appears to have no effect. I can't seem to find a documented way to add this and it would be super useful!
Sorry, this isn't possible and we have no plans to add support for this.
It would be better to accomplish this via linting, e.g. TSLint.
Reacted by Mike Wyatt, XiaoYan Li, Slav Ivanov, Raphaël Morineau, Matt Mazzola, Yannick Stachelscheid, Riccardo, Vasiliy Telyatnikov, byxor, Alexandre Paré and 55 more
Reacted by Sebastian Obentheuer, Tom Carrio, krasevych, Samuel Gaona, Leo Doldan, allindark, Jess and Miles B Huff
Reacted by felixhayashi, Samuel Gaona and Leo Doldan
@Timer I agree that unused variables is linter's job. But currently TSLint considers this rule deprecated, since it's quite hard for them to maintain it and tsc already has this feature (palantir/tslint#4100).
I don't think it's "impossible" to support this (create-react-app-typescript did that somehow), but I understand if it goes against zero-config policy. Still it would be nice to find some appropriate solution.
Ultimately, it comes down to being a foot-gun IMO. Users may accidentally configure their tsconfig.json differently between development and production (besides "linting" rules) and be really confused when their bundle is broken in production.
If this is necessary, I'd suggest modifying tsconfig.json on-the-fly before your CI tests run.
Reacted by Miles B Huff, Clarity and Joakim Uddholm
Since I only need that unused variable checking, I ended up just running tsc -p tsconfig.prod.json before an actual build with "skipLibCheck": true for better performance.
Reacted by Corey Light, Leo Doldan and Richard Torres
Since I only need that unused variable checking, I ended up just running tsc -p tsconfig.prod.json before an actual build with "skipLibCheck": true for better performance.
Would you @alexkuz mind elaborating a bit more how you did this?
Reacted by Christophe Geers, Colin, stefanfuchs, John Darryl Pelingo, Mehmet, abosshard, Clinton Blackburn, Kevin Xiang Li, Vitalii Zurian, Onur Önder and 26 more
Reacted by felixhayashi and Anton Versal
Reacted by Samuel Kirton, Leo Doldan and Justin Burr
Reacted by Richard Torres, Robert Molina and Farshad Mousalou
Reacted by Samuel Kirton, Leo Doldan and Farshad Mousalou
Reacted by Julian Frumar, dongyuwei, byxor, Clinton Blackburn, Uladzimir Kasacheuski, Leo Doldan, Didier Villevalois, Matt, Will, adamkowalczykB2M and 8 more
We need to use "jsx": "react" in our production builds, but react-scripts start is overwriting the line to "jsx": "preserve". 😦
This behaviour is breaking our production builds and could be avoided by allowing me to specify a different tsconfig.json when doing quick local development.
Update: I've worked around this by creating three files:
tsconfig.base.json
tsconfig.development.json // inherits from base config
tsconfig.production.json // inherits from base config
I added some extra steps to the build to copy the correct config to tsconfig.json before building. Now I can use multiple typescript configurations for different builds.
For example, here is what I added to package.json to copy the files:
Reacted by Clinton Blackburn, felixhayashi, Kevin Xiang Li, Uladzimir Kasacheuski, Leo Doldan, Ziad Beyens, Lucio Bordonaro, Stuart C. Robinson, hungluong-asnet, Taylor McLean and 7 more
I can "work around" nearly everything, because JS got' eval() but this is not the point. :) @byxor We used nearly the same approach; however such a standard feature should not be worked around, but supported natively.
Reacted by felixhayashi, Uladzimir Kasacheuski, Leo Doldan, Max, Didier Villevalois, HKFiliatix, Matt, Tibin Thomas, Stuart C. Robinson, Albert Schilling and 2 more
Support for multiple tsconfigs is a fundamental requirement for any build tool that claims to support TypeScript. Not having it is the real footgun.
The reason is that, in order for intellisense in editors like VSCode to work, your root tsconfig.json must include every source file and dependency in your app. I.e. all your app's source files, all the .test.ts files, the .stories.tsx files, etc. And the types/libs must reference things like Storybook, node, Jest, etc. as well.
However, when building your app, you obviously don't want to include everything. In fact, it is very dangerous to do so. If one of your app's components accidentally imported something from Storybook or node, you wouldn't get an error and build/type-check time. Therefore, you need to create a separate tsconfig.app.json that extends from the root tsconfig.json but which narrows the list of included files and allowed dependencies. You create a similar tsconfig file for your tests, Storybook, Cypress, etc.
This is how other tools like Angular CLI work, and it is how it is supposed to work. It's the whole reason TypeScript supports multiple tsconfig files and extending from each other.
Reacted by Przemysław Żydek, adamkowalczykB2M, Weston Sewell, Gabriele, Josh McCullough, Joe Maffei, Albert Schilling, Nicholas Paldino, Oskar Larsson Högfeldt, Dan Philibin and 23 more
FWIW adding tsc -p tsconfig.production.json before react-scripts build does work, but also adds time to the build process. Best we can do for now, IMO.
Having "*.stories.tsx" excluded/skipped in the production build would be useful. Otherwise storybook and its required dependencies need to be installed with npm install --only=prod. The other way it reduces build time is because there's a lot less to compile.
FWIW adding tsc -p tsconfig.production.json before react-scripts build does work, but also adds time to the build process. Best we can do for now, IMO.
This work-around doesn't work for the use case of excluding files from the production build.
Having "*.stories.tsx" excluded/skipped in the production build would be useful. Otherwise storybook and its required dependencies need to be installed with npm install --only=prod. The other way it reduces build time is because there's a lot less to compile.
FWIW adding tsc -p tsconfig.production.json before react-scripts build does work, but also adds time to the build process. Best we can do for now, IMO.
This work-around doesn't work for the use case of excluding files from the production build.
Hi, I believe a feature to use different tsconfigs is crucial, why aren't you planning to add it?
Reacted by Greg Wróbel, Miles B Huff, tyankatsu and Eric Miller
Reacted by Patrick Kerschbaum, Bendegúz Hajnal, Árni Jónsson, Jenky W. Nolasco, safazi, hasan-aa, Jean-Franck, Roni Litman, yf-hk, iron-cherep and 1 more
Support for multiple tsconfigs is a fundamental requirement for any build tool that claims to support TypeScript. Not having it is the real footgun.
The reason is that, in order for intellisense in editors like VSCode to work, your root tsconfig.json must include every source file and dependency in your app. I.e. all your app's source files, all the .test.ts files, the .stories.tsx files, etc. And the types/libs must reference things like Storybook, node, Jest, etc. as well.
However, when building your app, you obviously don't want to include everything. In fact, it is very dangerous to do so. If one of your app's components accidentally imported something from Storybook or node, you wouldn't get an error and build/type-check time. Therefore, you need to create a separate tsconfig.app.json that extends from the root tsconfig.json but which narrows the list of included files and allowed dependencies. You create a similar tsconfig file for your tests, Storybook, Cypress, etc.
Hello from Cypress! 👋🏻
Support for multiple tsconfigs would be a huge benefit for users.
Cypress 10 supports Component Testing and recommends adding *.cy.tsx files sibling to your Jest *.spec.tsx (also sibling to your source code). To avoid resolving global it, describe, and expect as Jest types from VSCode Intellisense... the only workaround we've found is using reference types at the top of each file. 😢
/// <reference types="cypress" />
Depending on the version of VSCode, you need to reload (Cmd + R) for the new reference types to take effect. This is a poor DX.
Lastly, files executed in the node context, like cypress.config.ts or plugins would benefit from a different set of types, where the DOM isn't actually available.
In projects that have: mixed toolchains, mixed environments (node vs browser), mixed module systems (esm, cjs), it's critical to support diversity in compilation options.
Reacted by Kostas Karvounis, Thomas G Henry, Viktor Fritzon, Greg Wróbel, Miles B Huff and Marcio Santos
Activity
Timer commentedon Dec 11, 2018
Sorry, this isn't possible and we have no plans to add support for this.
It would be better to accomplish this via linting, e.g. TSLint.
alexkuz commentedon Dec 24, 2018
@Timer I agree that unused variables is linter's job. But currently TSLint considers this rule deprecated, since it's quite hard for them to maintain it and tsc already has this feature (palantir/tslint#4100).
I don't think it's "impossible" to support this (
create-react-app-typescriptdid that somehow), but I understand if it goes against zero-config policy. Still it would be nice to find some appropriate solution.Timer commentedon Dec 24, 2018
Ultimately, it comes down to being a foot-gun IMO. Users may accidentally configure their
tsconfig.jsondifferently between development and production (besides "linting" rules) and be really confused when their bundle is broken in production.If this is necessary, I'd suggest modifying
tsconfig.jsonon-the-fly before your CI tests run.alexkuz commentedon Dec 24, 2018
Since I only need that unused variable checking, I ended up just running
tsc -p tsconfig.prod.jsonbefore an actual build with"skipLibCheck": truefor better performance.tlehtimaki commentedon Feb 19, 2019
Would you @alexkuz mind elaborating a bit more how you did this?
Obiwarn commentedon Feb 22, 2019
@alexkuz
Add a tsconfig.production.json:
and change your build script in package.json:
ChristianUlbrich commentedon May 9, 2019
@Timer Why would this not be possible? To me it looks like it would simply involve changing:
in packages/react-scripts/config/paths.js to something "env-based" using the Advanced Configuration "way".
byxor commentedon Sep 19, 2019
We need to use
"jsx": "react"in our production builds, butreact-scripts startis overwriting the line to"jsx": "preserve". 😦This behaviour is breaking our production builds and could be avoided by allowing me to specify a different
tsconfig.jsonwhen doing quick local development.byxor commentedon Sep 20, 2019
Update: I've worked around this by creating three files:
I added some extra steps to the build to copy the correct config to
tsconfig.jsonbefore building. Now I can use multiple typescript configurations for different builds.For example, here is what I added to
package.jsonto copy the files:ChristianUlbrich commentedon Nov 13, 2019
I can "work around" nearly everything, because JS got'
eval()but this is not the point. :) @byxor We used nearly the same approach; however such a standard feature should not be worked around, but supported natively.jonrimmer commentedon Jun 3, 2020
Support for multiple tsconfigs is a fundamental requirement for any build tool that claims to support TypeScript. Not having it is the real footgun.
The reason is that, in order for intellisense in editors like VSCode to work, your root
tsconfig.jsonmust include every source file and dependency in your app. I.e. all your app's source files, all the.test.tsfiles, the.stories.tsxfiles, etc. And the types/libs must reference things like Storybook, node, Jest, etc. as well.However, when building your app, you obviously don't want to include everything. In fact, it is very dangerous to do so. If one of your app's components accidentally imported something from Storybook or node, you wouldn't get an error and build/type-check time. Therefore, you need to create a separate
tsconfig.app.jsonthat extends from the roottsconfig.jsonbut which narrows the list of included files and allowed dependencies. You create a similar tsconfig file for your tests, Storybook, Cypress, etc.This is how other tools like Angular CLI work, and it is how it is supposed to work. It's the whole reason TypeScript supports multiple tsconfig files and extending from each other.
JoshMcCullough commentedon Aug 20, 2020
FWIW adding
tsc -p tsconfig.production.jsonbeforereact-scripts builddoes work, but also adds time to the build process. Best we can do for now, IMO.illume commentedon Mar 22, 2021
Having "
*.stories.tsx" excluded/skipped in the production build would be useful. Otherwise storybook and its required dependencies need to be installed withnpm install --only=prod. The other way it reduces build time is because there's a lot less to compile.This work-around doesn't work for the use case of excluding files from the production build.
jurajpiar commentedon Mar 29, 2021
Agree.
This one does though btw. #6023 (comment)
cakeinpanic commentedon Feb 28, 2022
Hi, I believe a feature to use different tsconfigs is crucial, why aren't you planning to add it?
JessicaSachs commentedon May 9, 2022
Hello from Cypress! 👋🏻
Support for multiple
tsconfigswould be a huge benefit for users.Cypress 10 supports Component Testing and recommends adding
*.cy.tsxfiles sibling to your Jest*.spec.tsx(also sibling to your source code). To avoid resolving globalit,describe, andexpectas Jest types from VSCode Intellisense... the only workaround we've found is using reference types at the top of each file. 😢/// <reference types="cypress" />Depending on the version of VSCode, you need to reload (Cmd + R) for the new reference types to take effect. This is a poor DX.
Lastly, files executed in the node context, like
cypress.config.tsorpluginswould benefit from a different set of types, where the DOM isn't actually available.In projects that have: mixed toolchains, mixed environments (node vs browser), mixed module systems (esm, cjs), it's critical to support diversity in compilation options.
samboylett commentedon Aug 10, 2022
You can achieve this with react-app-rewired:
Crismon96 commentedon Apr 24, 2023
Guys! There is a PR for it. It would make me so happy to see such a crucial change merged CarlRibbegaardh but it seems to have gotten stale.