Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

102
Views
Is there a way to reliably run a Babel transform against transpiled JSX?

Say we had the following JSX component:

export function Footer() {
    return (
        <footer class="foo bar baz">
            <span>foo bar baz</span>
        </footer>
    );
}

Using Babel, it is of course very easy to detect & run a transform against the contents of class:

function plugin({ types: t }) {
    return {
        name: 'my-plugin',
        visitor: {
            JSXAttribute(path) {
                if (/class(?:Name)?/.test(path.node.name.name)) {
                    ...
                }
            }
        }
    }
}

However, this presents a bit of an issue with certain build tools that don't (easily) let you get in between JSX transpilation & transforming. What I was wondering was if there was a way to run a transform against transpiled JSX in a way that isn't (reasonably) likely to ever cause false matches? For example, the function above could transpile to the following:

import { jsx as _jsx } from "preact/jsx-runtime";
export function Footer() {
    return _jsx("footer", {
        class: "foo bar baz",
        children: _jsx("span", {
            children: "foo bar baz"
        })
    });
}

A naive variation of the original plugin might look like the following:

function plugin({ types: t }) {
    return {
        name: 'my-plugin',
        visitor: {
            JSXAttribute(path) {
                if (/class(?:Name)?/.test(path.node.name.name)) {
                    ...
                }
            },
            ObjectProperty(path) {
                if (/class(?:Name)?/.test(path.node.key.name)) {
                    ...
                }
            }
        }
    }
}

While this "works" for the situation, it would also transform something like notJSXTransform('foo', { class: 'bar' });, which would be undesirable. Can't necessarily look for a certain Identifier on a CallExpression either, as there's no guarantees of the imported JSX function name.

I wasn't able to find any spec on what might be a safe way to approach this, if there is a way at all. I think it would be interesting and beneficial to be able to handle these situations better (where a user isn't in full control over the transforms ran & when), but I don't know if losing the JSX AST is too big of a blow to possibly overcome.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!