What's the default behavior for default & namespace imports/exports? #2401
-
I am trying to recreate how imports/exports work utilizing import * as NamespaceImport from './source.mdx';
import DefaultImport from './source.mdx'; When you do a default import does it only work when you have a default export in the source mdx file? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hey @hahnbeelee! 👋
MDX exports translate into ESM exports, the behavior matches the standard https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
You can generate unlimited examples of the expected behavior with https://mdxjs.com/playground/ One that you may find particularly illustrative. import { test } from 'node:test';
# page
export { test } compiles to import {jsx as _jsx} from "react/jsx-runtime";
import {test} from 'node:test';
export {test};
function _createMdxContent(props) {
const _components = {
h1: "h1",
...props.components
};
return _jsx(_components.h1, {
children: "page"
});
}
export default function MDXContent(props = {}) {
const {wrapper: MDXLayout} = props.components || ({});
return MDXLayout ? _jsx(MDXLayout, {
...props,
children: _jsx(_createMdxContent, {
...props
})
}) : _createMdxContent(props);
} the default export is the page/component contents. import Page from './example.mdx' // just the page
import Page, { test } from './example.mdx' // the page and the named export
import * as space from './example.mdx' // an object with `default` (the page) and `test` properties. |
Beta Was this translation helpful? Give feedback.
Hey @hahnbeelee! 👋
MDX exports translate into ESM exports, the behavior matches the standard https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
You can generate unlimited examples of the expected behavior with https://mdxjs.com/playground/
One that you may find particularly illustrative.
compiles to