-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
jsx: true
output does not handle custom elements correctly
#2100
Comments
This is related to a new issue in Astro's MDX integration: withastro/astro#4258 |
Could you get away with ditching the JSX language? Compiled away JSX can express this. Otherwise, I'm not sure what MDX could do about it. Turning dashes into camel seems like something other people would disagree with. |
@wooorm See my PR above! Unfortunately, Astro relies on our own JSX transformation process, so we cannot abandon this JSX output. I also discovered a unit test in the repo that asserts MDX outputs invalid JSX for custom elements. Regardless, I think a fix is warranted. I also agree that camel case isn't the best approach. I've opted for replacing invalid identifier characters with underscores |
One more alt: what if we turn invalid IDS into valid, different IDS, that still get them from components? With a:
|
@wooorm That could work too! So create variables below the |
Yeah, create throwaway variables. I like that much better than changing what we get from components, and hence that users have to change what they pass too? |
This commit particularly solves elements names that include dashes: custom elements, such as `custom-element-name`. These can be written in JSX like so: ```jsx <custom-element-name /> ``` …which is fine. But to support passing them in, we were previously rewriting such code to a member id, to take a key from an object, like so: ```jsx <_components.custom-element-name> ``` …which crashed. This commit solves that by taking the component from the object in a temporary variable, and using that valid variable name as a single component name. ```js const _component0 = _components['custom-element-name'] <_component0 /> ``` Closes GH-2100. Closes GH-2101. Co-authored-by: Titus Wormer <[email protected]> Reviewed-by: Titus Wormer <[email protected]>
This fixes a bug, uncovered in #2112 and in #2123, which is rather unlikely to occur. It only occurs when: 1. Custom elements (such as `<a-b></a-b>`) are injected (not authored) into a tree 2. Either: 1. A layout component is defined within an MDX document 2. A provider is used, and any component is defined within MDX documents In those cases, an accidental `const ;` was injected into the final serialized document. Which caused anything trying to run the code to crash. The problem was introduced in 9904838, the commit message of which sheds some light on why custom elements are peculiar and need extra handling. We track which component contains which other components. If some component uses `<A />`, then some code to handle `A` is injected in that component. If a different component uses `<B />`, some code for `B` is injected inside it. But the components don’t need to know about what’s used in other components. This mechanism had a mistake for custom elements: they were tracked globally. This commit fixes that, by tracking them scoped to the component that includes them. Related-to: GH-2100. Related-to: GH-2101. Closes GH-2112. Closes GH-2123. Co-authored-by: Caleb Eby <[email protected]> Co-authored-by: bholmesdev <[email protected]>
Initial checklist
Affected packages and versions
^2.1.2"
Link to runnable example
https://stackblitz.com/github/bholmesdev/mdx-shiki-compilation?file=test.mjs
Steps to reproduce
rehypeRaw
plugin applied + some other plugin that inserts a custom element (Shiki twoslash in this case).jsx: true
in the compiler config<_components.custom-element-name>
in the output, but dashes (-
) are not valid JSX!Expected behavior
Ideally, the
_components
statement would serialize dashes to camelCase or some other JSX-compliant string like so:Actual behavior
Instead,
_components
uses the element name untouched:Runtime
Node v16
Package manager
pnpm
OS
macOS
Build and bundle tools
Other (please specify in steps to reproduce)
The text was updated successfully, but these errors were encountered: