Repository navigation
Nested Component imports via MDX aren't JSX transformed #309
Description
Activity
- changed the title
[-]Component Import Issue[/-][+]Component imports causing ContentLayer to crash[/+]on Sep 28, 2022 - changed the title
[-]Component imports causing ContentLayer to crash[/-][+]Component imports causing Contentlayer to crash[/+]on Sep 28, 2022 Thanks for opening this issue and providing a minimal and straightforward repro! Looking into it! 🕵️
- addedblocked-upstreamThis issue is currently blocked by another problem/limitation in an upstream packageThis issue is currently blocked by another problem/limitation in an upstream packagehelp wantedExtra attention is neededExtra attention is needed
on Oct 4, 2022 After further investigation it seems like this might actually be a problem in esbuild which is a transitive dependency of Contentlayer as its a dependency of mdx-bundler which Contentlayer currently relies on. (Note I'm not 100% sure this is actually a bug in esbuild - that's just my current hypothesis.)
Unfortunately I don't have enough capacity right now to create a reproduction of the underlying upstream problems. My suspicion is that other tools like mdx-bundler also suffer from this same problem. I'd greatly appreciate if someone wants to help out with investigating this further but I don't think this is a problem within Contentlayer directly.
As a temporary workaround you could use a different "MDX strategy" by avoiding direct
import MyComponent from '...'in your MDX content files but rather "inject"MyComponentby passing it into thecomponentsprop like here:const MyButton: React.FC = () => <button>Click me</button> const Page: React.FC<{ post }> = ({ post }) => { const MDXContent = useMDXComponent(post.body.code) return ( <div> {/* Some code ... */} <MDXContent components={{ MyButton }} /> </div> ) }
Note: Implementing #298 might make this problem either more obvious or obsolete.
Reacted by Christoph Lindstädt, Tom Dohnal, Raqib Hayder, Wellington Mendoza and juaReacted by MertReacted by Raqib Hayder and MertReacted by Mert- changed the title
[-]Component imports causing Contentlayer to crash[/-][+]Nested Component imports via MDX aren't JSX transformed[/+]on Oct 4, 2022 @andy-stewart-design did you later resolve this?
Update: it's indeed a bug/limitation in esbuild:
Reacted by Wellington Mendoza- added a commit that references this issue
on Apr 16, 2023 Hey @schickling is this problem fixed?
Reacted by Yangshun Tay- added a commit that references this issue
on Jan 4, 2025

I'm running into problems when trying to import components inside a
.mdxfile, and more specifically when the imported component also imports other components. What I want to be able to doImporting the component by starting with the base path works as long as it is a standalone component and does not import another component. If that component does include an import statement, I get the following error:
The weird thing is, I'm having this issue on a project that I just spun up this morning, but not on a project that was created several months ago, even though they are using the same version of Next and ContentLayer. This could mean that it's user error, but I've looked in detail at the setup/configs for both projects and cannot find the issue.
Repro
Attempted Solutions:
contentTypeasmdx. error - SyntaxError: Unexpected token '<' #180makeSourceas described here, but that did not resolve the issue. Can't import components that use external packages #302