Skip to content

Commit

Permalink
Fix: transpiling React.createElement into Preact h behavior (parcel-b…
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinyaigeek authored and bhovhannes committed Dec 23, 2021
1 parent 412a5a9 commit 1eaac05
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 4 deletions.
@@ -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"
}
}
Empty file.
13 changes: 13 additions & 0 deletions packages/core/integration-tests/test/transpilation.js
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(/\Wreact\/jsx-runtime\W/.test(file));
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
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

0 comments on commit 1eaac05

Please sign in to comment.