Storybook: How to use babel-emotion-plugin with Storybook + Typescript?

Created on 16 Jan 2019  路  17Comments  路  Source: storybookjs/storybook

I am currently using Storybook (v.4.0.12) with TypeScript and have successfully followed the instructions to set up my TypeScript webpack.config.js. This works fine.

However, I cannot seem to find _any_ way of successfully adding a babel plugin to the config. My latest attempt is below, which does not work.

module.exports = (baseConfig, env, config) => {
  config.module.rules.push({
    test: /\.(ts|tsx)$/,
    use: [
      {
        loader: require.resolve('awesome-typescript-loader'),
        options: {
          babelOptions: {
            babelrc: false,
            presets: [],
            plugins: ['emotion'],
          },
        },
      },
    ],
  });
  config.resolve.extensions.push('.ts', '.tsx');

  return config;
};

If I boot Storybook with this config, I continue to see the message Component selectors can only be used in conjunction with babel-plugin-emotion., which means that the Emotion babel plugin is not being picked up by Storybook.

I am not using CRA, just plain ol' React and Typescript.

Any suggestions?

Thanks.

babel / webpack compatibility with other tools has workaround inactive

Most helpful comment

Any updates on this? I'm getting the same problem and not sure how to solve

All 17 comments

config.module.rules.push({
test: /.(js|jsx)$/,
exclude:<>,
loader: require.resolve("babel-loader")
});

test: /.(ts|tsx)$/,
include :path to src
require.resolve("ts-loader")

test: /.scss$/,
require.resolve("style-loader"),
require.resolve("css-loader"),
require.resolve("sass-loader")

What? Are you suggesting to add rules for TS and JS?

module.exports = (baseConfig, env, config) => {
  config.module.rules.push({
    test: /\.(ts|tsx)$/,
    exclude: /node_modules/,
    use: [
      {
        loader: require.resolve('babel-loader'),
        options: {
          plugins: [require.resolve('babel-plugin-emotion')],
        },
      },
      {
        loader: require.resolve('ts-loader'),
      },
    ],
  });
  config.resolve.extensions.push('.ts', '.tsx');

  return config;
};

Still not working.

  defaultConfig.module.rules.push({
    test: /\.(ts|tsx)$/,
    include: [path.resolve(__dirname, "../src"), path.resolve(__dirname, "../stories")],
    loader: require.resolve("ts-loader")
  });

  defaultConfig.module.rules.push({
    test: /\.(js|jsx)$/,
    exclude: /node_modules/,
    loader: require.resolve("babel-loader")
  });

This does not work either. ^^

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

Hey there, it's me again! I am going close this issue to help our maintainers focus on the current development roadmap instead. If the issue mentioned is still a concern, please open a new ticket and mention this old one. Cheers and thanks for using Storybook!

Any updates on this? I'm getting the same problem and not sure how to solve

Also having this problem if anyone has a solution. @meebix ?

Having this issue too, can we re-open please?

Did anyone find a solution @meebix? @ximbong91023 @kennycrosby

PSA: Upvoting the original issue description is the most effective way to get it solved. Upvoting comments below is also great, but won't help us prioritize the feature/fix

@MarkLyck crossposting it here from SO so other people might find it as well.

I don't know if it applies to everyone, but in my case I got it working with storybook 5.3.x by using in .storybook/main.js this:

module.exports = {
  stories: ['../packages/**/*.stories.tsx'],
  webpackFinal: async (config) => {
    config.module.rules.push({
      test: /\.(ts|tsx)$/,
      loader: require.resolve('babel-loader'),
      options: {
        plugins: ['emotion'],
        presets: [['react-app', { flow: false, typescript: true }]],
      },
    });
    config.resolve.extensions.push('.ts', '.tsx');
    return config;
  },
};

This doesn't use TypeScript tho, so might or might not be relevant in this situation, or might just be used while a solution using TypeScript is found.

Hope it helps.

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

Hey there, it's me again! I am going close this issue to help our maintainers focus on the current development roadmap instead. If the issue mentioned is still a concern, please open a new ticket and mention this old one. Cheers and thanks for using Storybook!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

enagy27 picture enagy27  路  69Comments

ilias-t picture ilias-t  路  73Comments

Gongreg picture Gongreg  路  58Comments

tycho01 picture tycho01  路  76Comments

Olian04 picture Olian04  路  78Comments