question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question about bundleMDX (files, globals, cwd)

See original GitHub issue

Hi,

thanks for building this project!

  • mdx-bundler version: ^5.2.1
  • node version: 15.14.0
  • npm version: 7.15.0

I am currently working on building this blog: https://github.com/chrislicodes/personal-blog.

The function to bundle the MDX looks like this:

export const prepareMDX = async (directoryName: string) => {
  if (process.platform === "win32") {
    process.env.ESBUILD_BINARY_PATH = path.join(
      process.cwd(),
      "node_modules",
      "esbuild",
      "esbuild.exe"
    );
  } else {
    process.env.ESBUILD_BINARY_PATH = path.join(
      process.cwd(),
      "node_modules",
      "esbuild",
      "bin",
      "esbuild"
    );
  }

  const directory = path.join(POSTS_PATH, "/", directoryName);

  const source = getSourceOfFile(directory + "/index.mdx");

  // const files = await getComponents(directoryName);

  const { code, frontmatter } = await bundleMDX(source, {
    // files,
    cwd: directory,
    xdmOptions: (options) => {
      options.remarkPlugins = [
        ...(options.remarkPlugins ?? []),
        remarkMdxImages,
      ];

      return options;
    },
    esbuildOptions: (options) => {
      options.outdir = path.join(process.cwd(), "/public/img");
      options.loader = {
        ...options.loader,
        ".png": "file",
        ".jpg": "file",
        ".gif": "file",
        ".mp3": "file",
      };
      options.publicPath = "/img/";
      options.write = true;

      return options;
    },
  });

  return { frontmatter, code };
};

In this post, I am importing a component using React Three Fiber:

https://github.com/chrislicodes/personal-blog/blob/main/src/_posts/three-js-post/index.mdx

Using it this way leads to multiple instances of THREE being imported. The multiple imports should be fixed using the “globals” parameter, or would that only apply I import the libraries in the .mdx file directly?

My questions:

  1. With passing the “cwd” parameter, the imports in a .mdx file can be resolved - in which scenarios would I have to pass/use the “files” parameter?
  2. Can I fix the multiple THREE.js instances with the “globals” parameter, or would that only be needed if I import a library directly in an MDX file?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
tol-iscommented, Oct 5, 2021

@chrislicodes Turns out you need to configure esbuild named-exports.

bundleMDX(content, 
  globals: {
    "@react-three/fiber": {
       varName: "reactThreeFiber",
       namedExports: ["Canvas",  "useFrame"],
       defaultExport: false
     }
  }
);

https://www.npmjs.com/package/@fal-works/esbuild-plugin-global-externals

0reactions
chrislicodescommented, Oct 31, 2021

Everything worked as expected thanks to the previous comment

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create feature-rich markdown pages in next.js application
Step-by-step tutorial on creating feature-rich markdown pages in next.js application using mdx-bundler.
Read more >
cannot access 'fs' before initialization - You.com | The AI ...
I already red all anwsers talking about this problem but I still can't find ... access function before initialization in file that imports...
Read more >
MDX Bundler with Next.JS - Adam Laycock
MDX Bundler's server side component is bundleMDX . This takes your mdx ... It takes the source MDX and the imported files as...
Read more >
How I built the new notjust.dev platform with NextJS
The data for the blog posts is written in markdown (.md) files. ... const { code, frontmatter } = await bundleMDX(fileContents, {. cwd: ......
Read more >
output images corrupt when using mdx-bundler + remark-mdx ...
I am using a combination of mdx-bundler to convert the MDX files into ... await bundleMDX({ source: indexFile.content, files: filesObject, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found