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.

Using the css Prop of @emotion/react and apply babel plugin

See original GitHub issue

I am just submitting this issue so that it can be searched by others.

  1. Install these packages:
yarn add @emotion/babel-plugin @emotion/react
  1. Modify your vite.config.ts
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    react({
      jsxImportSource: "@emotion/react",
      babel: {
        plugins: ["@emotion/babel-plugin"],
      },
    }),
  ],
});
  1. Modify tsconfig.json
  "compilerOptions": {
    "types": ["vite/client", "@emotion/react/types/css-prop"]
  1. Modify .story/book/main.js
const react = require("@vitejs/plugin-react");

module.exports = {
  stories: ["../src/**/*.stories.mdx", "../src/**/*.stories.@(js|jsx|ts|tsx)"],
  addons: ["@storybook/addon-links", "@storybook/addon-essentials"],
  framework: "@storybook/react",
  core: {
    builder: "storybook-builder-vite",
  },
  async viteFinal(config) {
    config.plugins = config.plugins.filter(
      (plugin) =>
        !(Array.isArray(plugin) && plugin[0]?.name.includes("vite:react"))
    );

    config.plugins.push(
      react({
        exclude: [/\.stories\.(t|j)sx?$/, /node_modules/],
        jsxImportSource: "@emotion/react",
        babel: {
          plugins: ["@emotion/babel-plugin"],
        },
      })
    );

    console.log(config.plugins);
    return config;
  },
};

That’s it! Happy coding!!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:27
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
bdowcommented, Sep 13, 2022

@terenceodonoghue - I was having the same problem and errors trying to get MDX working along side my stories. I saw your solution but I didn’t really want to add that pragma to every file in our repo. I ended up trying out the MDX2 Opt-in feature, and the MDX stories started building without issues! Shout out to @IanVS for all the help on Discord.

Here is my entire storybook/main.js

const { mergeConfig } = require("vite");
const path = require("path");
const reactPlugin = require("@vitejs/plugin-react");

module.exports = {
  core: { builder: "@storybook/builder-vite" },
  stories: ["../src/**/*.stories.tsx"],
  addons: [
    "@storybook/addon-links",
    "@storybook/addon-essentials",
    "storybook-addon-performance/register",
    "@storybook/addon-interactions",
  ],
  // https://storybook.js.org/docs/react/configure/typescript#mainjs-configuration
  typescript: { check: false },
  async viteFinal(config) {
    const mergedConfig = mergeConfig(config, {
      esbuild: {
        // ignoring console warns of "Top-level "this" will be replaced with undefined since this file is an ECMAScript module"
        // See https://github.com/vitejs/vite/issues/8644#issuecomment-1159308803
        logOverride: { "this-is-undefined-in-esm": "silent" },
      },
      resolve: {
        alias: {
          src: path.resolve(__dirname, "../src"),
        },
      },
    });

    return {
      ...mergedConfig,
      plugins: [
        // Filter out `vite:react-jsx` per suggestion in `plugin-react`...
        // "You should stop using "vite:react-jsx" since this plugin conflicts with it."
        // Implementation suggestion from: https://github.com/storybookjs/builder-vite/issues/113#issuecomment-940190931
        ...config.plugins.filter(
          (plugin) => !(Array.isArray(plugin) && plugin.some((p) => p.name === "vite:react-jsx")),
        ),
        reactPlugin({ exclude: [/\.stories\.tsx?$/, /node_modules/], jsxImportSource: "@emotion/react" }),
      ],
      optimizeDeps: [
        ...(mergedConfig.optimizeDeps ? mergedConfig.optimizeDeps.include || [] : []),
        "@emotion/react/jsx-dev-runtime",
      ],
    };
  },
  reactOptions: { fastRefresh: true, strictMode: false },
};
3reactions
thieliumcommented, Jun 10, 2022

@bestickley I know it’s been awhile since you asked, but in answer to your question: since storybook already defines the react plugin for vite, you have to remove it first, and then re-add it with a custom configuration.

If you do not omit the plugin that storybook provides, then vite complains about conflicting plugins.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The css Prop - Emotion
The primary way to style elements with emotion is the css prop. It provides a concise and flexible API to style your components....
Read more >
What is the proper way to enable the css prop in Emotion 11 ...
Here is a working config with emotion and Next.js npm install @emotion/react @emotion/styled && npm i --save-dev @emotion/babel-plugin
Read more >
@emotion/babel-plugin | Yarn - Package Manager
The css prop doesn't work via the babel plugin. jsx can be manually imported from @emotion/core (which can be automated with eslint-plugin-emotion) or...
Read more >
How To Use Emotion for Styling in React | DigitalOcean
emotion provides a css prop that can accept nested selectors and media queries. It can support an object or a tagged template literal....
Read more >
gatsby-plugin-emotion
Provide support for using the css-in-js library Emotion including server side rendering. This plugin supports Emotion v11+. Older versions should use ...
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