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.

Error in postcss/css loader with create-react-app @storybook/react v5.2.6

See original GitHub issue

Describe the bug When using a standard/new project created with create-react-app 3.2.0 and Storybook 5.2.6, Storybook fails to build any React component with which imports a CSS file with the following:

ERROR in ./src/HeaderLogo.css (./node_modules/css-loader/dist/cjs.js??ref--7-oneOf-3-1!./node_modules/postcss-loader/src??postcss!./node_modules/@storybook/core/node_modules/style-loader!./node_modules/@storybook/core/node_modules/css-loader/dist/cjs.js??ref--9-1!./node_modules/postcss-loader/src??postcss!./src/HeaderLogo.css)
Module build failed (from ./node_modules/postcss-loader/src/index.js):
SyntaxError

(2:1) Unknown word

To Reproduce Steps to reproduce the behavior:

  1. Create a new application using create-react-app (latest was 3.2.0 when last created) and typescript configuration (I’m assuming typescript does not cause this problem)
  2. Add Storybook npx -p @storybook/cli sb init
  3. Add a very simple story that uses a component that does an import of a CSS file: import './MyComponent.css'
  4. Build will fail

Expected behavior Build will not fail

Build Output info @storybook/react v5.2.6 info info => Loading static files from: /Users/mattmassey/work/storydemo/public . info => Loading presets info => Loading presets info => Loading custom addons config. info => Loading Webpack configuration from node_modules/react-scripts info => Removing existing JavaScript and TypeScript rules. info => Modifying Create React App rules. info => Using default Webpack setup.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
mlmasseycommented, Nov 30, 2019

The reason for this is because there is a problem with the Storybook integration with the CRA build scripts. Its inserting two CSS loader rules which are conflicting with each other. The solution is to remove one of the rules, since they are basically the same in the webpack config. A temporary work-around is to add a custom webpack.config.js in the .storybook folder that has the following:

let foundCSSRule = false;

function removeDuplicateCSSRule(rule) {
  if (!rule || !rule.test) {
    return;
  }
  if (rule.test.toString() !== '/\\.css$/') {
    return;
  }
  if (!foundCSSRule) {
    foundCSSRule = true;
    return;
  }
  return false;
}

function updateRules(oldRules) {
  const newRules = [];
  if (!oldRules || !oldRules.length) {
    return newRules;
  }
  for (let i = 0; i < oldRules.length; i++) {
    const rule = oldRules[i];
    if (rule.oneOf) {
      rule.oneOf = updateRules(rule.oneOf);
      newRules.push(rule);
    } else {
      const newRule = removeDuplicateCSSRule(oldRules[i]);
      if (newRule) {
        // If a new rule was returned, push it into the new rules list
        newRules.push(newRule);
      }
      if (newRule !== false) {
        // If any function returned false, it means remove it from the list of rules
        // Otherwise, we just use the old rule that was there previously
        newRules.push(oldRules[i]);
      }
    }
  }
  return newRules;
}

module.exports = async ({ config, mode }) => {
  console.log('*** Custom webpack running ****');
  config.module.rules = updateRules(config.module.rules);
  // Uncomment this line if you would like to view the final webpack config so you can alter it.  Useful for debugging
  // console.dir(config, { depth: null });
  return config;
};

1reaction
mlmasseycommented, Dec 2, 2019

@shilman Confirmed that 1.3.2 fixes this issue. Thanks for the quick work

Read more comments on GitHub >

github_iconTop Results From Across the Web

Errors thrown when integrating Storybook with Webpack 5 ...
I am getting 2 errors and haven't been able to resolve them: 1) complaining that I am missing an appropriate loader to handle...
Read more >
Storybook Addon PostCSS
Storybook addon used to run the PostCSS preprocessor against your stories.
Read more >
Setting up Storybook with Material UI and Styled Components
Recently I started experimenting with Material UI, a project that provides a set of React components that follow Google's Material Design ...
Read more >
storybook/preset-create-react-app - npm
One-line Create React App configuration for Storybook. This preset is designed to use alongside @storybook/react .
Read more >
Add PostCSS to Create-React-App - DEV Community ‍ ‍
In the end, after a bit of trial and error hacking together different solutions, I got it working. Here's how! You can find...
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