Migrating postcss-loader from 3.0.0 to 4.0.2 results in Error: [object Object] is not a PostCSS plugin

6k views Asked by At

So Today I have been trying to migrate postscss-loader from version 3.0.0 to 4.0.2

I noticed since verion 4.0.0 they added postCSS as a peer dependency so I've added postcss version 7.0.27. I did not go for 8 as I also use 'postcss-import' and this relies on version 7. This should be okay as postcss-loader can both use version 7 and 8.

However now when running I get the same error message for al of scss files:

Error: [object Object] is not a PostCSS plugin

As the message is not really descriptive, solving the issue has become a real trial and error method. With no success so far.

Here is my webpack.config.ts:

import postCSSImport from 'postcss-import';
import autoPrefixerFlexbox from 'postcss-flexbugs-fixes';
import autoPrefixer from 'autoprefixer';

const BrowserList = {
    mobile: ['android >= 4', 'ios >= 8'],
    desktop: ['firefox >= 40', 'chrome >= 49', 'edge >= 12', 'opera >= 47', 'ie >= 11', 'safari >= 9'],
    designer: ['firefox >= 40', 'chrome >= 49', 'edge >= 12', 'opera >= 47', 'ie >= 11', 'safari >= 9'],
    translation: 'last 1 chrome versions'
};

module.exports = async (env, argv) => {

    const webpackConfigs = [];
 
    webpackConfigs.push({   
        module: {
            exprContextCritical: false,
            rules: [
                { // babel javascript/typescript (no type checking), also see ./babel.config.js
                    test: /\.(js|jsx|ts|tsx)$/,
                    exclude: /node_modules\/(?!@k2g)/, 
                    use: {
                        loader: 'babel-loader',
                        options: {
                            presets: [
                                ['@babel/preset-env', {
                                    targets: {
                                        browsers: BrowserList[target]
                                    }
                                }]
                            ]
                        }
                    }
                },

                { // sass
                    test: /\.(scss|sass)$/,
                    exclude: /node_modules\/(?!sweetalert2)/, 
                    use: [
                        { loader: 'style-loader' }
                    ),
                    {
                        loader: 'css-loader',
                        options: {
                            importLoaders: 1
                        }
                    },
                    {
                        loader: 'postcss-loader',
                        options: {
                            sourceMap: false,
                            postcssOptions: {
                                plugins: [
                                    postCSSImport(), // for @import in css
                                    autoPrefixerFlexbox,
                                    autoPrefixer({
                                        overrideBrowserslist: BrowserList[target], // target = 'desktop' 
                                        grid: true
                                    })
                                ]
                            }
                        }
                    },
                    {
                        loader: 'sass-loader',
                        options: {
                            additionalData: vars.sass, // e.g. '$url-logo: "/resources/images/logo.png";'
                            sourceMap: true,
                            sassOptions: {
                                outputStyle: 'expanded',
                                sourceMapContents: true
                            }
                        }
                    }
                ] 
            },
        }
    })
}

I tried several things in the options of the postcss-loader:

  • setting execute to true
  • setting sourceMap to true
  • setting both to false

To be complete here are the versions of dependencies I use:

Node:       v12.16.1
Webpack:    4.44.1 
Yarn:       1.22.5 
postcss:    7.0.27
css-loader: 3.6.0

Do you see the problem here or know where to look? It would be a help.

Thanks in advance

1

There are 1 answers

1
Bedřich Schindler On BEST ANSWER

I was to trying to solve similar problem. We were using postcss-loader@^3.0.0 with autoprefixer@^9.7.0. After I updated postcss-loader to version 4.0.2 and autoprefixer to version 10.0.0, I received errors like Error: [object Object] is not a PostCSS plugin and true is not a PostCSS plugin.

Then I found bug report in autoprefixer repo: https://github.com/postcss/autoprefixer/issues/1359

Solution is to use autoprefixer@^9.7.0., because autoprefixer@^10.0.0 is not ready yet. This solved my problem.