compile mdx to html with import of jsx files #2382
Answered
by
wooorm
AngeloChecked
asked this question in
Q&A
-
project root:
main.ts import fs from 'node:fs/promises'
import {compile, run} from 'https://esm.sh/@mdx-js/mdx@2'
import {renderToStaticMarkup} from 'npm:react-dom/server'
import * as runtime from 'npm:react/jsx-runtime'
const file = await fs.readFile('test.mdx');
const test = await compile(file,{ outputFormat: "function-body" })
const { default: MDXContent } = await run(test, {...runtime})
const html = renderToStaticMarkup(MDXContent({}))
console.log(html) test.mdx import {Box} from "./Box.jsx"
export function Thing() {
return <>World!</>
}
# Hello, <Thing />
Box.jsx export function Box() {
return <>im a box!</>
} when i run the main i receive this error: error: Uncaught 1:1-1:29: Cannot use `import` or `export … from` in `evaluate` (outputting a function body) by default: please set `useDynamicImport: true` (and probably specify a `baseUrl`) Do you have any guidance on how to enable importing of |
Beta Was this translation helpful? Give feedback.
Answered by
wooorm
Oct 25, 2023
Replies: 1 comment 3 replies
-
The error you get gives the answer: pass |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update to v3 btw.
Plain JS files work.
Node supports loaders: so it can support importing MDX/JSX/etc files if you set it up. I’d assume Deno should have something like that.
useDynamicImport
, which is the default, is needed because there is no way toeval
an entire module in JavaScript. It can onlyeval
function bodies. Only entire modules can useimport
statements, function bodies can useimport()
expressions. So we compile import statements to import expressions.More on how it all works is explained in the docs. Or use the playground to inspect results.