-
Notifications
You must be signed in to change notification settings - Fork 75
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
Trying to invoke mdx-bundler
via a custom webpack loader in a Next.js app
#177
Comments
Here is my minimal sample that reproduces the issue: And here's an example of what Nextra returns: import withLayout from "./layouts/docs";
import { withSSG } from "nextra/ssg";
/*@jsxRuntime automatic @jsxImportSource react*/
import { useMDXComponents as _provideComponents } from "@mdx-js/react";
function MDXContent(props = {}) {
const { "wrapper": MDXLayout } = { ..._provideComponents(), ...props.components };
return MDXLayout ? <MDXLayout {...props}><_createMdxContent /></MDXLayout> : _createMdxContent();
function _createMdxContent() {
const _components = {
"p": "p",
"img": "img",
"h1": "h1",
"strong": "strong",
"h2": "h2",
"h3": "h3",
"ul": "ul",
"li": "li",
..._provideComponents(),
...props.components
};
return <>
<_components.p>
<_components.img src="path/to/image.png" alt="alt text" />
</_components.p>
{"\n"}
<_components.h1>{"Introduction"}</_components.h1>
{"\n"}
<_components.p>{"Content "}<_components.strong>{"strong content"}</_components.strong>{" and more content."}</_components.p>
</>;
}
}
const _mdxContent = <MDXContent />;
export default function NextraPage(props) {
return withSSG(withLayout({
"filename": "C:/path/to/page.mdx",
"route": "/path/to/page",
"meta": {},
"pageMap": [/* Giant object with the keys: `name`, `children`, `route` */]
}, null))({
...props,
"children": _mdxContent
});
} |
No dice for: // util/webpack/loader.cjs
const path = require("path");
const { bundleMDX } = require("mdx-bundler");
const { getMDXComponent } = require("mdx-bundler/client");
const basePath = path.join(__dirname, "..", "..");
module.exports = async function(source) {
const callback = this.async();
this.addContextDependency(path.join(basePath, "pages"));
let { code, frontmatter } = await bundleMDX({
"source": source,
"esbuildOptions": function(options, frontmatter) {
options.minify = false;
return options;
},
});
const Component = getMDXComponent(code)
callback(null, Component.toString());
}; either. Yields:
And: callback(null, "export default " + Component.toString()); Yields:
Where the
|
So its got something to do with this I guess: Lines 166 to 180 in 8a4e6a6
...but why? |
I was pretty sure I tried this, but this: const path = require("path");
const { bundleMDX } = require("mdx-bundler");
const { getMDXComponent } = require("mdx-bundler/client");
const basePath = path.join(__dirname, "..", "..");
module.exports = async function(source) {
const callback = this.async();
this.addContextDependency(path.join(basePath, "pages"));
let { code, frontmatter } = await bundleMDX({
"source": source
});
const Component = getMDXComponent(code)
callback(null, "import * as import_jsx_runtime from \"react/jsx-runtime\"; export default " + Component.toString());
}; seems to have done it. I just can't tell if this is needlessly bundling |
Outstanding questions:
|
The jsx runtime is used in next.js as of the "new"(has been around since next.js 9.5) react transform and it is bundled anyways so you aren't bundling it for no reason |
Not sure I follow. Are you saying I should have to import |
What I am saying is that importing the jsx runtime is fine and you aren't adding any extra dependencies |
mdx-bundler
version: v9.0.1node
version: v18.2.0npm
version: v8.9.0Relevant code or config:
What you did:
Attempted to render a MDX file via a custom webpack loader.
What happened:
Reproduction repository:
WIP
Problem description:
I had a clever idea to copy what Nextra is doing to leverage Next.js's file-system based routing so all my MDX files would be rendered automatically, but I'm clearly not invoking
mdx-bundler
correctly. I'll work on a minimal sample to reproduce the issue and see what I can figure out.The text was updated successfully, but these errors were encountered: