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

Fix: transpiling React.createElement into Preact h behavior #7435

Merged
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1 @@
module.exports = <div />;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -0,0 +1,12 @@
{
"private": true,
"dependencies": {
"preact": "^10.5"
},
"alias": {
"react": "preact/compat",
"react-dom/test-utils": "preact/test-utils",
"react-dom": "preact/compat",
"react/jsx-runtime": "preact/jsx-runtime"
}
}
12 changes: 12 additions & 0 deletions packages/core/integration-tests/test/transpilation.js
Expand Up @@ -183,6 +183,18 @@ describe('transpilation', function () {
assert(file.includes('_jsxRuntime.jsx("div"'));
});

it('should support the automatic JSX runtime with preact with alias', async function () {
let b = await bundle(
path.join(
__dirname,
'/integration/jsx-automatic-preact-with-alias/index.js',
),
);

let file = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
assert(file.includes('_jsxRuntime.jsx("div"'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
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"'));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I fixed in 4efec78 👍 Thanks!

});

it('should support the automatic JSX runtime with explicit tsconfig.json', async function () {
let b = await bundle(
path.join(__dirname, '/integration/jsx-automatic-tsconfig/index.js'),
Expand Down
12 changes: 8 additions & 4 deletions packages/transformers/js/src/JSTransformer.js
Expand Up @@ -200,11 +200,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()
Expand Down