Skip to content

CRA4+TypeScript failure on jsx:preserve #826

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

Closed
davakos opened this issue Nov 5, 2020 · 3 comments
Closed

CRA4+TypeScript failure on jsx:preserve #826

davakos opened this issue Nov 5, 2020 · 3 comments
Labels

Comments

@davakos
Copy link

davakos commented Nov 5, 2020

Describe the bug
Informational: This is not a LinguiJS bug, but does impact the ability to run Lingui when using Create React App 4 with TypeScript.
It appears that CRA4 currently fails if you set "jsx": "preserve" in your tsconfig.json file, which a Lingui requirement for preserving JSX and tagged template literals:
https://lingui.js.org/guides/typescript.html

In create-react-app project refer to the following issues:

To Reproduce
npx create-react-app cra4 --template redux-typescript
Then modify tsconfig.json and change:

{
  "compilerOptions": {
    "jsx": "preserve",
    "target": "es2016"
  }
}

run dev server with: npm start

Results:

> [email protected] start cra4
> react-scripts start

./node_modules/react-scripts/scripts/utils/verifyTypeScriptSetup.js:239
      appTsConfig.compilerOptions[option] = value;
                                          ^

TypeError: Cannot assign to read only property 'jsx' of object '#<Object>'
    at verifyTypeScriptSetup (./node_modules/react-scripts/scripts/utils/verifyTypeScriptSetup.js:239:43)
    at Object.<anonymous> (./node_modules/react-scripts/scripts/start.js:31:1)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Expected behavior
Setting jsx:preserve fails to start a run server.
Setting jsx:react (the default) starts a run server but Lingui is unable to extract message catalogs.

Additional context

  • jsLingui version 3.0.3
  • Babel version 7.12.3
  • Using Create-React-App v4, react-scripts 4.0.0
  • TypeScript version 4.0.5
@semoal
Copy link
Contributor

semoal commented Nov 5, 2020

Thanks for pointing it out, I'm sure that will help future users, probably the issue will be fixed on his side asap

@davakos
Copy link
Author

davakos commented Dec 28, 2020

Update: This appears to be fixed in create-react-app/react-scripts 4.0.1.

I have not tested exhaustively, but this worked in my testing:

  • Changed an application string, correctly shows "Missing Translation" in my running app per configuration of setupI18n option missing
  • Running npm run extract properly extracts new strings into a message catalog file
  • Running npm run compile properly compiles new strings into a message file
  • Adding a new language to the app properly displays the translated strings in the app

Environment:

  • Node v14 (14.15.3)
  • React 17.0.1
  • TypeScript 4.1.3
  • LinguiJS 3.3.0

I tested this both in an existing app that I upgraded to React 17 + TypeScript 4 and also in a test app I just bootstrapped using latest 4.0.1 version of create-react-app and following LinguiJS documentation here:
https://lingui.js.org/tutorials/setup-cra.html

One thing I'd like to note from the docs on using LinguiJS with TypeScript documentation:
https://lingui.js.org/guides/typescript.html

I am still setting compilerOptions => "target": "es2016" in tsconfig.json, but when I set "jsx": "preserve" this setting is overwritten with "react-jsx" whenever you call react-scripts (such as by running npm start). You will also briefly see the following console message as the dev server starts:

The following changes are being made to your tsconfig.json file:
  - compilerOptions.jsx must be react-jsx (to support the new JSX transform in React 17)

There does not appear to be any way to prevent or override this behavior, but at the same time, I appear to be able to still utilize all LinguiJS functionality without setting "jsx": "preserve". I have not tested this exhaustively, but perhaps if LinguiJS maintainers have an opportunity to ensure this does not cause any breakages or unintended side-effects, the LinguiJS/TypeScript documentation noted above could be updated to indicate that this option is no longer required if using TS4+React 17+CRA 4.0.1.

Best regards,

@stale
Copy link

stale bot commented Feb 26, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Feb 26, 2021
@stale stale bot closed this as completed Mar 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants