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 2 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"
}
}
15 changes: 14 additions & 1 deletion packages/core/integration-tests/test/transpilation.js
Expand Up @@ -120,7 +120,7 @@ describe('transpilation', function () {
await bundle(path.join(__dirname, '/integration/jsx-react-alias/index.js'));

let file = await outputFS.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(file.includes('React.createElement("div"'));
Shinyaigeek marked this conversation as resolved.
Show resolved Hide resolved
assert(file.includes('h("div"'));
});

it('should support compiling JSX in JS files with Preact dependency', async function () {
Expand Down Expand Up @@ -183,6 +183,19 @@ 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('preact/jsx-runtime'));
assert(file.includes('_jsxRuntime.jsx("div"'));
});

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
33 changes: 32 additions & 1 deletion packages/transformers/js/src/JSTransformer.js
Expand Up @@ -126,6 +126,37 @@ const SCRIPT_ERRORS = {
},
};

function convertAliasReactIntoPragma(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

alias: string | {|[string]: string|},
): 'react' | 'preact' | 'hyperapp' | 'nervjs' {
if (typeof alias === 'string') {
switch (alias) {
case 'react': {
return 'react';
}
case 'preact/compat':
case 'preact': {
return 'preact';
}
case 'hyperapp': {
return 'hyperapp';
}
case 'nervjs': {
return 'nervjs';
}
default: {
return 'react';
}
}
} else {
for (const key in alias) {
return convertAliasReactIntoPragma(alias[key]);
}
}

return 'react';
}

type TSConfig = {
compilerOptions?: {
// https://www.typescriptlang.org/tsconfig#jsx
Expand Down Expand Up @@ -157,7 +188,7 @@ export default (new Transformer({
let reactLib;
if (pkg?.alias && pkg.alias['react']) {
// e.g.: `{ alias: { "react": "preact/compat" } }`
reactLib = 'react';
reactLib = convertAliasReactIntoPragma(pkg.alias['react']);
} else {
// Find a dependency that we can map to a JSX pragma
reactLib = Object.keys(JSX_PRAGMA).find(
Expand Down