Skip to content
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

Unable to set swc option for automatic react runtime #1800

Closed
ShravanSunder opened this issue Jun 17, 2022 · 14 comments · Fixed by #1802
Closed

Unable to set swc option for automatic react runtime #1800

ShravanSunder opened this issue Jun 17, 2022 · 14 comments · Fixed by #1802
Milestone

Comments

@ShravanSunder
Copy link

ShravanSunder commented Jun 17, 2022

Search Terms

swc options, swc runtime, react automatic

Expected Behavior

There is no way to set swc to use automatic run time in react:
Its not part of the transpiler configs thats passed into swc

Actual Behavior

Steps to reproduce the problem

Minimal reproduction

Specifications

  • ts-node version: latest
  • node version: 16
  • TypeScript version: 4.7
  • tsconfig.json, if you're using one:
{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true
  },
  "files": ["hardhat.config.ts"],
  "ts-node": {
    "swc": true,

    "compilerOptions": {
      "module": "CommonJS"
    },
    "moduleTypes": {
      "**/*": "cjs"
    }
  }
}

  • package.json:
{}
  • Operating system and version: mac, latest
  • If Windows, are you using WSL or WSL2?:
@cspotcode
Copy link
Collaborator

Is there an equivalence between this swc option and a tsconfig option? In other words, do people using tsc (not swc) use a certain config option to make TSC compile in this way?

@cspotcode
Copy link
Collaborator

It looks like we should implement a translation between tsconfig's jsx option and the equivalents for swc.

https://www.typescriptlang.org/tsconfig#jsx

Does that sound correct to you?

@ShravanSunder
Copy link
Author

@cspotcode yes! the swc option directly relates tsconfig options and is a blocker to using ts-node to run any tests related to react.

In my example, hardhat uses ts-node to compile typescript tests. I would like to replace the transpiler to use swc for performance reasons but cannot since i am unable to transmit this tsconfig option to the swc transpiler.

@cspotcode
Copy link
Collaborator

What's the mapping between tsconfig options and swc options? Like, when we see certain tsconfig options, which swc options should we generate? If we have this mapping then we can implement the translation.

@ShravanSunder
Copy link
Author

ShravanSunder commented Jun 18, 2022

@cspotcode

jsx

In the jsx option in tsconfig: https://www.typescriptlang.org/tsconfig#jsx
the values:

  • react-jsx
  • react-jsxdev

should be mapped to: https://swc.rs/docs/configuration/compilation#jsctransformreactruntime

  • transform.react.runtime : automatic

The default for transform.react.runtime isclassic. It is classic for all the other options of jsx in tsconfig

jsx import source

in the jsxImportSource in tsconfig: https://www.typescriptlang.org/tsconfig#jsxImportSource

@cspotcode
Copy link
Collaborator

Thanks, if someone has in their tsconfig.json:

"jsx": "react-jsxdev"
// jsxImportSource is not specified

...should we be passing a certain value for swc's transform.react.importSource?

@ShravanSunder
Copy link
Author

ShravanSunder commented Jun 18, 2022

@cspotcode jsxImportSource default value is react. I don't think its required to be specified.

@cspotcode
Copy link
Collaborator

react-jsx and react-jsxdev cause different output from TSC, right? How do we get swc to do the same?

@ShravanSunder
Copy link
Author

ShravanSunder commented Jun 20, 2022

@cspotcode i read through the swc docs and issues.
It seems like when runtime is set to automatic it will automatically set the pragma to _jsx which is what the expected behaviour is.

if runtime is classic it uses the pragma that's passed in via the transpiler config.

I'm not familiar with rust, but seems like thats what the code does.

The test for this is that if runtime is set to automatic the swc transpiled code should automatically generate with _jsx or _jsxDEV instead of React.createElement: https://www.typescriptlang.org/docs/handbook/jsx.html

@cspotcode
Copy link
Collaborator

Ok, my concern is this:

If a user sets one of their projects to use "jsx": "react-jsx" and they set another project to use "jsx": "react-jsxdev", then presumably they want tsc's output to be different in those two projects, because "react-jsx" is different from "react-jsxdev". But based on what you have described, we are sending exactly the same options to SWC for both. Shouldn't we be sending different options to SWC for these two different tsconfigs?

What is the difference between tsc's react-jsx and react-jsxdev? And how do we send different options to swc to tell it to emit differently, too?

@ShravanSunder
Copy link
Author

ShravanSunder commented Jun 20, 2022

@cspotcode I understand. I looked it up more.

Looks like if it is react-jsxdev swc devleopment should be set to true.

looks like that is how it differentiates jsx from jsx dev

@ShravanSunder
Copy link
Author

@cspotcode did you need me to do any other clarification or reserach?

@cspotcode
Copy link
Collaborator

cspotcode commented Jun 21, 2022 via email

@cspotcode cspotcode modified the milestones: 10.8.2, 10.8.3 Jun 26, 2022
@ShravanSunder
Copy link
Author

@cspotcode thank you 🙏🏽

crapStone pushed a commit to Calciumdibromid/CaBr2 that referenced this issue Jul 18, 2022
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [ts-node](https://typestrong.org/ts-node) ([source](https://github.com/TypeStrong/ts-node)) | devDependencies | minor | [`10.8.2` -> `10.9.1`](https://renovatebot.com/diffs/npm/ts-node/10.8.2/10.9.1) |

---

### Release Notes

<details>
<summary>TypeStrong/ts-node</summary>

### [`v10.9.1`](https://github.com/TypeStrong/ts-node/releases/tag/v10.9.1)

[Compare Source](TypeStrong/ts-node@v10.9.0...v10.9.1)

**Fixed**

-   Workaround nodejs bug introduced in 18.6.0 ([#&#8203;1838](TypeStrong/ts-node#1838)) [@&#8203;cspotcode](https://github.com/cspotcode)
    -   Only affects projects on node >=18.6.0 using `--esm`
    -   Older versions of node and projects without `--esm` are unaffected

https://github.com/TypeStrong/ts-node/milestone/18?closed=1

### [`v10.9.0`](https://github.com/TypeStrong/ts-node/releases/tag/v10.9.0)

[Compare Source](TypeStrong/ts-node@v10.8.2...v10.9.0)

**Added**

-   `--project` accepts path to a directory containing a `tsconfig.json` ([#&#8203;1829](TypeStrong/ts-node#1829), [#&#8203;1830](TypeStrong/ts-node#1830)) [@&#8203;cspotcode](https://github.com/cspotcode)
    -   previously it required an explicit filename
-   Added helpful error message when swc version is too old to support our configuration ([#&#8203;1802](TypeStrong/ts-node#1802)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Added `experimentalTsImportSpecifiers` option which allows using voluntary `.ts` file extensions in import specifiers (undocumented except for [API docs](https://typestrong.org/ts-node/api/interfaces/CreateOptions.html#experimentalTsImportSpecifiers)) ([#&#8203;1815](TypeStrong/ts-node#1815)) [@&#8203;cspotcode](https://github.com/cspotcode)

**Fixed**

-   Fixed bug where `child_process.fork()` would erroneously execute the parent's entrypoint script, not the intended child script ([#&#8203;1812](TypeStrong/ts-node#1812), [#&#8203;1814](TypeStrong/ts-node#1814)) [@&#8203;devversion](https://github.com/devversion)
-   Fixed support for jsx modes `"react-jsx"` and `"react-jsxdev"` in swc transpiler ([#&#8203;1800](TypeStrong/ts-node#1800), [#&#8203;1802](TypeStrong/ts-node#1802)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Fixed support for import assertions in swc transpiler ([#&#8203;1817](TypeStrong/ts-node#1817), [#&#8203;1802](TypeStrong/ts-node#1802)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Fixed bug where calling `repl.evalCode()` with code not ending in a newline would not update the typechecker accordingly ([#&#8203;1764](TypeStrong/ts-node#1764), [#&#8203;1824](TypeStrong/ts-node#1824)) [@&#8203;cspotcode](https://github.com/cspotcode)

https://github.com/TypeStrong/ts-node/milestone/16?closed=1

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, click this checkbox.

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzMi4xMTEuMSIsInVwZGF0ZWRJblZlciI6IjMyLjExMi4wIn0=-->

Co-authored-by: cabr2-bot <cabr2.help@gmail.com>
Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1461
Reviewed-by: Epsilon_02 <epsilon_02@noreply.codeberg.org>
Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants