create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

Cra can't support postcss-nested syntax because webpack.config.js file haven't postcss-nested module

Open melihyarikkaya opened this issue 5 years ago • 5 comments

Is your proposal related to a problem?

Cra can't support postcss-nested syntax because webpack.config.js file haven't postcss-nested module.

Sample nested css in my header.module.css file

.Header {
  background: white;
  h1 {
    color: red;
  }
}

Expected behavior when compiled

.Header {
  background: white;
}
.Header h1 {
    color: red;
}

Describe the solution you'd like

The problem solved when ejected cra and adding the 'postcss-nested' module to webpack.module.js.

Can you support postcss-nested by default?

// config/webpack.config.js
// row 89
ident: 'postcss',
          plugins: () => [
            require('postcss-flexbugs-fixes'),
            require('postcss-nested'), // <----  added this line 
            require('postcss-preset-env')({
              autoprefixer: {
                flexbox: 'no-2009'
              },
              stage: 3
            }),

Describe alternatives you've considered

postcss-nesting may be an alternative.

Additional context

(Write your answer here.)

melihyarikkaya avatar Sep 07 '20 18:09 melihyarikkaya

Just a thought: Wouldn't it be much easier to configure postcss by using a postcss.config.js file that is created when using CRA instead of the inline options in the loader? This way the developer can easily decide which additional features he might need without the need of ejecting.

e.g.

const postcssPresetEnv = require("postcss-preset-env");

const config = () => ({
  plugins: [
    postcssPresetEnv({
      stage: 3,
      features: {
        "nesting-rules": true,
        "custom-media-queries": true,
      },
      importFrom: "./src/ui/theme/theme.css",
    }),
  ],
});

module.exports = config;

btmnk avatar Dec 07 '20 13:12 btmnk

postcss.config.js is not supported in CRA. There is an open PR to update postcss-loader and that will allow the configuration to be added.

alesso-x avatar Feb 21 '21 18:02 alesso-x

postcss.config.js is not supported in CRA. There is an open PR to update postcss-loader and that will allow the configuration to be added.

This would be my preferred solution.

kalm42 avatar Mar 10 '21 16:03 kalm42

At this moment loading postcss.config.js is being disabled with config: false flag here: https://github.com/facebook/create-react-app/pull/11717 There is a PR to remove it: https://github.com/facebook/create-react-app/pull/11926

piotr-cz avatar Apr 22 '22 08:04 piotr-cz

const { addWebpackModuleRule } = require("customize-cra");

module.exports = override(
  addWebpackModuleRule({
    test: /\.css$/,
    use: [
      "style-loader",
      "css-loader",
      {
        loader: "postcss-loader",
        options: {
          postcssOptions: {
            config: true, // use postcss.config.js
          },
        },
      },
    ],
  })
);

this worked for us (incorporating a tailwind project into a cra/emotion project), adjusting the test key as necessary

neaumusic avatar Sep 23 '24 22:09 neaumusic