feat: transform JSX in scripts (#55860)

This commit is contained in:
Oliver Eyton-Williams
2024-09-12 12:40:36 +02:00
committed by GitHub
parent 2386c57edb
commit 23888d618a

View File

@@ -177,9 +177,17 @@ async function transformSASS(documentElement) {
async function transformScript(documentElement) {
await loadBabel();
await loadPresetEnv();
await loadPresetReact();
const scriptTags = documentElement.querySelectorAll('script');
scriptTags.forEach(script => {
script.innerHTML = babelTransformCode(getBabelOptions(presetsJS))(
const isBabel = script.type === 'text/babel';
// TODO: make the use of JSX conditional on more than just the script type.
// It should only be used for React challenges since it would be confusing
// for learners to see the results of a transformation they didn't ask for.
const options = isBabel ? presetsJSX : presetsJS;
if (isBabel) script.removeAttribute('type'); // otherwise the browser will ignore the script
script.innerHTML = babelTransformCode(getBabelOptions(options))(
script.innerHTML
);
});