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.

Allow @next/mdx plugin to accept webpack loaders

See original GitHub issue

Feature request

Is your feature request related to a problem? Please describe.

The problem is quite similar to what has been described in the #8857 issue. I want to use the front-matter in my markdown files to export any meta data. Currently we can export a variable from mdx files e.g.

export const meta = {}

In this post we will ....

and it will work. But that is more of a tech heavy solution. Writing that meta data in frontmatter would be much helpful for non-techy people to write blogs. That is the first part.

Next, what I want is to add some custom properties like short info, time to read, formatted published date to each markdown based on it’s content i.e. assume that we have following markdown file

---
title: 'My Blog'
date: '2020-12-12'
---

In this post we will ....

Expected output (input for @mdx-js/loader after processing with my custom logic) after formatting date, calculating time to read based on content, adding a description like so

export const meta = {
  title: 'My Blog',
  displayDate: 'December 12, 2020',
  description: 'In this pos',
  timeToRead: '2 min read'
}


In this post we will ....

And the blog writer doesn’t need to care about it.

Describe the solution you’d like

This can be easily accomplished if we can just provide a plugin option to take loaders for webpack i.e.

module.exports = (pluginOptions = {}) => (nextConfig = {}) => {
  const extension = pluginOptions.extension || /\.mdx$/
+ const loaders = plugionOptions.loaders || []
  return Object.assign({}, nextConfig, {
    webpack(config, options) {
      config.module.rules.push({
        test: extension,
        use: [
          options.defaultLoaders.babel,
          {
            loader: '@mdx-js/loader',
            options: pluginOptions.options,
          },
+         ...loaders
        ],
      })

and when using the @next/mdx plugin, on will just have to pass the loaders (optional)

module.exports = require("@next/mdx")({
+  loaders: [],
})( .... )

Describe alternatives you’ve considered

Currently I am using the above code snippet loaded via a custom nextjs plugin. see here

module.exports = require("@next/mdx")()( .... )
module.exports = require("./my-plugin")()( .... )

Additional context

I can create a PR if this feature may be useful to the community.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
sudkumarcommented, Jan 30, 2020

I could not found any library that would fit my need out of box, so here is the custom plugin that I wrote. It may be useful to anybody else stumbling over this issue.

1reaction
timneutkenscommented, Jan 30, 2020

you can create a custom implementation, but you could also write a MDX plugin that converts frontmatter to export const meta

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Webpack Config - Next.js
Extend the default webpack config added by Next.js. ... a loader that depends on babel-loader // This source was taken from the @next/mdx...
Read more >
Getting started - MDX
Install and configure the webpack loader @mdx-js/loader . Configure your JSX runtime depending on which one you use (React, Preact, Vue, etc.).
Read more >
Next.js + MDX Enhanced Not Allowing Typescript Template Files
I'm not sure why but I had to update the webpack config to include 'ts' ... const mdx = require('next-mdx-enhanced')({ defaultLayout: true, ...
Read more >
How to Build Your Own Blog with Next.js and MDX
@next/mdx, which is the official tool built by the Next.js team ... webpack: (config, { buildId, dev, isServer, defaultLoaders, ...
Read more >
Using MDX with NextJS (and when to go Gatsby) - Ryosuke
As of writing, Next has a plugin for MDX that allow for authoring ... in the project and parses them with Webpack (using...
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