Merge packaged theme components with original components #11808
-
|
Hi community 👋 I recently wrote my first docusaurus plugin (npm, github). For now, the essence is that it exports a remark plugin that adds MDX components to some markdown AST trees, and it defines those components in a theme. Unfortunately it turned out that my theme completely overrides the original components so that e.g. admonitions no longer render correctly. // src/theme/Components.tsx
import GlossaryIndex from "@theme/GlossaryIndex";
import GlossaryTooltip from "@theme/GlossaryTooltip";
export default {
GlossaryIndex,
GlossaryTooltip,
};I realize that the recommended pattern is the following: import MDXComponents from "@theme-original/MDXComponents";
import GlossaryIndex from "@theme/GlossaryIndex";
import GlossaryTooltip from "@theme/GlossaryTooltip";
export default {
...MDXComponents,
GlossaryIndex,
GlossaryTooltip,
};However, if I then build my package and use it in an example project (same repo, npm workspace links to my plugin), I get
I was suggested to not build my package as ES module but as commonJS instead. But removing the I did manage to make the plugin work by adding an import statement for my component in every markdown AST, see here. But I feel like I had better follow the Disclaimer: I am very new to npm and typescript and I work on my first docusaurus plugin. I am easily confused by ES modules versus commonJS, and making any sense of |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
Hi The Creating a theme that overrides the default from our classic theme is not a good idea: it creates a coupling point between your theme and the classic theme, and even makes it sensitive to theme ordering issue ( We document it here: https://docusaurus.io/docs/markdown-features/react#mdx-component-scope However, I don't think it's a good idea in general for a theme to extend another. Keep in mind that if users also create their own I don't know why you encountered bundling errors, but I'd suggest that you should try to make your theme self-encapsulated, and adding explicit exports for the 2 components is likely a more portable solution. This way it works whether users have a In general, I'm not satisfied with the |
Beta Was this translation helpful? Give feedback.
If the components are in the MDX map, they are automatically available everywhere: MDX docs use them from the React context.
Otherwise, they need to be imported, either manually or automatically.
Here's an example Remark plugin that automatically adds imports to all MDX docs containing
npm2yarncode blocks: https://github.com/facebook/docusaurus/blob/main/packages/docusaurus-remark-plugin-npm2yarn/src/index.tsNote: in the future we'd like to introduce an API so that plugins can register remark plugins automatically. For now, you'd have to document in your lib that users need to add the remark plugin manually if they want those components to become ambiantly available