Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MDX - Storybook + React + Typescript

Tags:

I try to use MDX markup in building live docs for my storybook. When I run the storybook I get this error:

Module build failed (from ./node_modules/babel-loader/lib/index.js):

SyntaxError: C:/Users/User/Desktop/priority-style-react/src/stories/Button1.stories.mdx: Unexpected token (12:9)

  10 | const makeShortcode = name => function MDXDefaultShortcode(props) {
  11 |   console.warn("Component " + name + " was not imported, exported, or provided by MDXProvider as global scope")
> 12 |   return <div {...props}/>
     |          ^
  13 | };
  14 | const Preview = makeShortcode("Preview");
  15 | const layoutProps = {

my webpack config to load .mdx is:

config.module.rules.push({
    test: /\.mdx?$/,
    use: [{loader: "babel-loader"}, {loader: '@mdx-js/loader'}],
    exclude: /node_modules/,
    include: [/src/]
});

storybook config.js:

import { configure, addDecorator } from '@storybook/react';
import { withKnobs } from '@storybook/addon-knobs';

import '../style/index.scss';

const req = require.context('../src/stories', true, /.stories.(tsx|mdx)/);

addDecorator(withKnobs);
configure(req, module);

looks like loader works incorrectly. Can anyone help me to understand what I missed?

like image 318
Ihor Lavs Avatar asked Feb 05 '20 07:02

Ihor Lavs


People also ask

What is storybook MDX?

MDX is the syntax Storybook Docs uses to capture long-form Markdown documentation and stories in one file. You can also write pure documentation pages in MDX and add them to Storybook alongside your stories.

What can you do with MDX?

MDX is a superset of markdown that lets you write JSX directly in your markdown files. It is a powerful way to add dynamic interactivity, and embed components within your content, helping you to bring your pages to life.

Does storybook support React 18?

Storybook 6.5 supports React 18 and Angular 14 out of the box. When you run your React project for the first time, it will auto-detect the React version and use the new root API if it detects it on React 18. Check out #17215.


1 Answers

Okay there is something very silly in MDX files

It does not accept blank lines as we speak.

const makeShortcode = name => function MDXDefaultShortcode(props) {
console.warn("Component " + name + " was not imported, exported, or provided by MDXProvider as global scope")
return <div {...props}/>
 // remove this blank line here.
};

to

const makeShortcode = name => function MDXDefaultShortcode(props) {
console.warn("Component " + name + " was not imported, exported, or provided by MDXProvider as global scope")
return <div {...props}/>
};

And it should work as i scratched my head around this for hours.

like image 56
Zunaib Imtiaz Avatar answered Oct 05 '22 23:10

Zunaib Imtiaz