-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
babel-preset-css-prop does not work with Storybook #1306
Comments
The problem is that React presets is applied whatever we do. A temporary workaround: // .storybook/webpack.config.js
module.exports = ({ config }) => {
// Remove react preset to make emotion work
config.module.rules[0].use[0].options.presets.splice(1, 1)
return config
} The best would be to create a stronger function to find the "react" preset and remove it. I don't have time right now 😅 |
Hey @neoziro, thanks a lot for explaining what's going on! FWIW I think your one-liner is perfectly well suited for the job, but just for the sake of over-engineering, here is a more complex approach 😂: // .storybook/webpack.config.js
module.exports = ({ config }) => {
const rules = config.module.rules;
// Find the rule handling .jsx
const javaScriptRuleIndex = rules.findIndex(rule => rule.test.test('.jsx'));
const javaScriptRules = rules[javaScriptRuleIndex];
const javaScriptRulePresets = javaScriptRules.use[0].options.presets;
// Find the react preset
const reactPresetIndex = javaScriptRulePresets.findIndex(preset => {
if (typeof preset !== 'string') return false;
return preset.match('preset-react');
});
// Remove the react preset
javaScriptRulePresets.splice(reactPresetIndex, reactPresetIndex);
return config;
}; |
Played around a bit with these solutions and realized that Storybook only supports loading It only loads from If you drop a 🤦♂️ |
🤦♂️ indeed, but good to know! That would be an issue with Storybook then. They seem to have several conflicting open issues around Thanks for looking into this! |
In my project I was able to get Storybook to recognize css-prop (without using the jsx pragma) by adding this config to Line 17 - Line 19: config.module.rules[0].use[0].options.presets = [
require.resolve('@babel/preset-react'),
require.resolve('@babel/preset-env'),
// Emotion preset must run BEFORE reacts preset to properly convert css-prop.
// Babel preset-ordering runs reversed (from last to first). Emotion has to be after React preset.
require.resolve('@emotion/babel-preset-css-prop'),
]; Also, I am including the Line 41 - Line 43: config.module.rules.push({
test: /\.(ts|tsx)$/,
loader: require.resolve('babel-loader'),
options: {
presets: [
['react-app', { flow: false, typescript: true }],
// Emotion preset must run BEFORE reacts preset to properly convert css-prop.
// Babel preset-ordering runs reversed (from last to first). Emotion has to be after React preset.
require.resolve('@emotion/babel-preset-css-prop'),
],
// other plugins here...
},
}); I created a template to share my project config that uses Gatsby + TypeScript + Emotion + Storybook + React Intl + SVGR + Jest: Here's a link to my |
Thanks @duncanleung , the adding the second code config to my webpackFinal config in Storybook main.js worked for me! |
.storybook/main.js module.exports = {
...,
babel: async (options) => ({
...options,
presets: [...options.presets, '@emotion/babel-preset-css-prop'],
}),
} https://storybook.js.org/docs/react/configure/babel#gatsby-focus-wrapper |
Thanks @iamyoki your snippet worked for me! (SB 6.4.19, emotion 11.0.9, react 17.0.2, typescript 4.6.3) |
@iamyoki this safe my life thanks |
emotion
version: 10.0.9react
version: 16.8.3storybook
version 5.0.6Relevant code:
What you did:
Bootstrap a new Storybook instance:
What happened:
Output is
<button css="[object Object]">Hello</button>
Problem description:
The
css
prop is not transformed. It does, however, work when using/** @jsx jsx */
pragma.Per https://storybook.js.org/docs/configurations/custom-babel-config/ Storybook uses the app's babel config so the note about custom configs should not apply.
The text was updated successfully, but these errors were encountered: