Code Monkey home page Code Monkey logo

Comments (4)

anishd19 avatar anishd19 commented on May 26, 2024 1

My next js version is "next": "9.3.4". I will try once after downgrading the version. Will report back 👍 Thanks for your help.

from gatsby-mdx.

karolis-sh avatar karolis-sh commented on May 26, 2024

I tested styled components in a simple app - the mdx runtime (https://github.com/karolis-sh/gatsby-mdx/blob/master/demos/mdx-scoped-runtime-playground/src/App.js) handles it fine:

image

import React from 'react';
import MDX from 'mdx-scoped-runtime';
import { ThemeProvider } from 'styled-components';

import CoolQuote from './ui/CoolQuote';

const Layout = ({ children }) => (
  <ThemeProvider theme={{ colors: { fourthLight: 'mediumseagreen' } }}>{children}</ThemeProvider>
);

const initialMdx = `import CoolQuote from "./ui/CoolQuote";
import Layout from "./Layout";

export default Layout

<CoolQuote>
  <i>TEST</i>
</CoolQuote>
`;

export default class App extends React.Component {
  state = { mdx: initialMdx };

  render() {
    const { mdx } = this.state;
    return (
      <div style={{ width: 500, margin: '40px auto' }}>
        <textarea
          style={{ width: '100%', height: 130 }}
          value={mdx}
          onChange={(e) => this.setState({ mdx: e.target.value })}
        />
        <MDX
          allowedImports={{
            './ui/CoolQuote': { ImportDefault: CoolQuote },
            './Layout': { ImportDefault: Layout },
          }}
        >
          {mdx}
        </MDX>
      </div>
    );
  }
}

As the error states - it's hooks usage issues. I had some similar issues with gatsby and netlify-cms. You'll need to tweak nextjs webpack config.

from gatsby-mdx.

anishd19 avatar anishd19 commented on May 26, 2024

Hard luck, I changed my cms.js to match your solution, yet the same error happens.

my cms.js is as follows:

import { MdxControl, setupPreview } from 'netlify-cms-widget-mdx';
import remarkEmojiPlugin from 'remark-emoji';

import CoolQuote from '../components/cool-quote';
window.CMS.registerWidget(
  'mdx',
  MdxControl,
  setupPreview({
    allowedImports: {
      '../../components/cool-quote': { ImportDefault: CoolQuote }
    },
    mdPlugins: [remarkEmojiPlugin],
  })
);

it used to be

window.CMS.registerWidget(
  'mdx',
  MdxControl,
  setupPreview({
    scope: {
      CoolQuote: ({ children, ...props }) => (
        <CoolQuote {...props}>
          {children}
        </CoolQuote>
      )
    },
    components: {},
    allowedImports: {},
    mdPlugins: [remarkEmojiPlugin],
  })
);

May be the failure is because of my setup. I have a separate webpack build for my cms/cms.js which outputs cms.bundle.js in the public folder of my next.js app. ( referred and modified from https://github.com/Jinksi/netlify-cms-react-starter/blob/master/cms/webpack.config.js ).

// This webpack config is used to compile the JS for the CMS
const path = require('path')
const ExtractTextPlugin = require('extract-text-webpack-plugin')

const production = process.env.NODE_ENV === 'production'
const mode = production ? 'production' : 'development'
// const mode = 'production'
console.log(`Building CMS in ${mode} mode`)

module.exports = {
  entry: './cms.js',
  output: {
    filename: 'cms.bundle.js',
    path: path.resolve(__dirname, '../public/admin/'),
  },
  mode,
  stats: { warnings: false, children: false },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        options: {
          presets: ['@babel/preset-env', '@babel/preset-react'],
          plugins: [
            'babel-plugin-styled-components',
            '@babel/plugin-proposal-object-rest-spread',
            '@babel/plugin-proposal-class-properties'
          ],
        },
      },
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          use: [
            { loader: 'css-loader', options: { importLoaders: 1 } },
            'postcss-loader',
          ],
        }),
      },
    ],
  },
  plugins: [
    new ExtractTextPlugin({
      filename: 'cms.bundle.css',
    }),
  ],
}

from gatsby-mdx.

karolis-sh avatar karolis-sh commented on May 26, 2024

What's your nextjs version? I had somewhat related issue with latest nextjs versions - vercel/next.js#9022 (<=9.0.5 seems to be working fine)

from gatsby-mdx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.