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.

Build failed when adding custom remark/rehype plugin

See original GitHub issue
  • mdx-bundler version: 6.0.1
  • node version: 14.17.0
  • npm version: 6.14.13
  • esbuild version: 0.12.25
  • platform: windows 11

Relevant code or config

async function getFileRenderBySlug(type: string, slug: string) {
  const file_path = path.join(BLOG_FOLDER_PATH, type, `${slug}.mdx`);

  if (checkIfPathExists(file_path) === false) {
    throw new Error(`${file_path} file not found`);
  }

  const mdxSource = fs.readFileSync(file_path, "utf8");

  if (process.platform === "win32") {
    process.env.ESBUILD_BINARY_PATH = path.join(
      ROOT,
      "node_modules",
      "esbuild",
      "esbuild.exe"
    );
  } else {
    process.env.ESBUILD_BINARY_PATH = path.join(
      ROOT,
      "node_modules",
      "esbuild",
      "bin",
      "esbuild"
    );
  }

  const result = await bundleMDX(mdxSource, {
    cwd: path.join(ROOT, COMPONENTS),
    xdmOptions(options) {
      options.remarkPlugins = [
        ...(options.remarkPlugins ?? []),
        // if I remove this plugins it builds without any issue
        remarkGfm,
        remarkToc,
        remarkMath,
      ];
      options.rehypePlugins = [
        ...(options.rehypePlugins ?? []),
        // if I remove this plugins it builds without any issue
        rehypePrism,
        rehypeKatex,
      ];

      return options;
    },
  });

  const { code, frontmatter } = result;

  return {
    toRender: code,
    data: {
      readingTime: readingTime(code),
      ...(frontmatter as MarkdownFrontMatter),
    },
  };
}

Error Output:

Server Error
Error: Build failed with 1 error:
_mdx_bundler_entry_point-1f92ed01-7bb4-4697-9847-570c4c57cf02.mdx:0:0: error: [plugin: esbuild-xdm] 

This error happened while generating the page. Any console logs will be displayed in the terminal window.

Call Stack
failureErrorWithLog
file:///E:/projects/next-ts-blog-starter/node_modules/esbuild/lib/main.js (1478:15)
<unknown>
file:///E:/projects/next-ts-blog-starter/node_modules/esbuild/lib/main.js (1136:28)
runOnEndCallbacks
file:///E:/projects/next-ts-blog-starter/node_modules/esbuild/lib/main.js (926:63)
buildResponseToResult
file:///E:/projects/next-ts-blog-starter/node_modules/esbuild/lib/main.js (1134:7)
<unknown>
file:///E:/projects/next-ts-blog-starter/node_modules/esbuild/lib/main.js (1243:14)
<unknown>
file:///E:/projects/next-ts-blog-starter/node_modules/esbuild/lib/main.js (614:9)
handleIncomingPacket
file:///E:/projects/next-ts-blog-starter/node_modules/esbuild/lib/main.js (711:9)
Socket.readFromStdout
file:///E:/projects/next-ts-blog-starter/node_modules/esbuild/lib/main.js (581:7)
Socket.emit
events.js (376:20)
Socket.emit
domain.js (470:12)

When I am adding my custom remark/rehype plugins, it fails to build. But if I remove all my custom remark/rehype plugins it builds without any issue. Don’t know where I am doing wrong. Currently moved to next-mdx-remote 😅

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:20

github_iconTop GitHub Comments

2reactions
chasemccoycommented, Sep 18, 2021

@wooorm 🤦‍♂️ I figured it out. I forgot to change to named exports for my unified dependencies:

I had this:

import visit from 'unist-util-visit'
import toc from 'mdast-util-toc'
import toHast from 'mdast-util-to-hast'
import toHtml from 'hast-util-to-html'

I needed this:

import {visit} from 'unist-util-visit'
import {toc} from 'mdast-util-toc'
import {toHast} from 'mdast-util-to-hast'
import {toHtml} from 'hast-util-to-html'

Such a small detail that I completely overlooked it. Thank you again for your help, your patience, and for all the awesome work you do!

1reaction
chasemccoycommented, Sep 19, 2021

Yeah, this error getting swallowed up somewhere made it really hard to debug this. But anyway, I think we can close this issue now!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Transforming Markdown with Remark & Rehype - ryanfiller.com
Writing custom plugins to give extra powers to Markdown syntax using the Unified ecosystem.
Read more >
Can't require remark and rehype plugins for gatsby-plugin-mdx
However upon running gatsby develop I encouter the following error: Error: [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\User\ ...
Read more >
rehype-remark - npm
This is a rehype plugin that transforms hast into mdast to support remark. When should I use this? This project is useful when...
Read more >
gatsby-transformer-remark
This plugin adds additional fields to the MarkdownRemark GraphQL type including html , excerpt , headings , etc. Other Gatsby plugins can also...
Read more >
Why I moved to Next.js | Saying goodbye to Gatsby
The plugins for remark and rehype can be used directly, I'm not waiting for a plugin author to add support. Everything else outside...
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