Fix: transpiling React.createElement into Preact h behavior#7435
Conversation
|
dc89141 to
ca600b4
Compare
| }, | ||
| }; | ||
|
|
||
| function convertAliasReactIntoPragma( |
There was a problem hiding this comment.
In order to convert preact/compat into preact pragma, I made a function to do it, but I feel this approach is a little muddy. If you have another idea to implement this, I will be happy if you comment on it.
|
I think this approach is wrong. I thought #7433 behavior is caused by this line(https://github.com/parcel-bundler/parcel/blob/v2/packages/transformers/js/src/JSTransformer.js#L160) in which reactLib is fixed to I think the cause is not this line, but these line(https://github.com/parcel-bundler/parcel/blob/v2/packages/transformers/js/src/JSTransformer.js#L201). In these line, It's 3am in my timezone, so once I back this PR to draft and sleep, and I’ll work tomorrow 🙇. |
|
I fixed 👍 |
|
Does the current version still have the problem that I think something like this should work. The other react-like libraries don't support this automatic runtime anyway --- packages/transformers/js/src/JSTransformer.js
+++ packages/transformers/js/src/JSTransformer.js
@@ -199,11 +199,15 @@ export default (new Transformer({
jsxImportSource = compilerOptions?.jsxImportSource;
automaticJSXRuntime = true;
} else if (reactLib) {
- let automaticVersion = JSX_PRAGMA[reactLib]?.automatic;
+ let effectiveReactLib =
+ (pkg?.alias && pkg.alias['react']) === 'preact/compat'
+ ? 'preact'
+ : reactLib;
+ let automaticVersion = JSX_PRAGMA[effectiveReactLib]?.automatic;
let reactLibVersion =
- pkg?.dependencies?.[reactLib] ||
- pkg?.devDependencies?.[reactLib] ||
- pkg?.peerDependencies?.[reactLib];
+ pkg?.dependencies?.[effectiveReactLib] ||
+ pkg?.devDependencies?.[effectiveReactLib] ||
+ pkg?.peerDependencies?.[effectiveReactLib];
let minReactLibVersion =
reactLibVersion != null && reactLibVersion !== '*'
? semver.minVersion(reactLibVersion)?.toString() |
|
As you say, it's not Thank you for helping! |
| let file = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8'); | ||
| assert(file.includes('_jsxRuntime.jsx("div"')); |
There was a problem hiding this comment.
I think we should also add this to make sure it's actually importing from React (using a Regex because react/jsx-runtime is a substring of preact/jsx-runtime).
| let file = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8'); | |
| assert(file.includes('_jsxRuntime.jsx("div"')); | |
| let file = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8'); | |
| assert(/\Wreact\/jsx-runtime\W/.test(file)); | |
| assert(file.includes('_jsxRuntime.jsx("div"')); |
… from react/jsx-runtime
…b.com/Shinyaigeek/parcel into fix/correct-jsx-runtime-import-source
↪️ Pull Request
Hi team 👋
Fixes: #7433
jsxImportSourcewill be determined withreactLibvariable, butreactLibvariable is always “react” string literal ifalias.react is setted. I thinkjsx-runtime(and automatic transformation) is not related.I fixed this
💻 Examples
package.json
{ "alias": { "react": "preact/compat" } }🚨 Test instructions
I made a test case to check jsx runtime transformation with preact with
alias.reactpackage.json.✔️ PR Todo