3

I am trying to use purgeCSS with my nextJs installation. So i am following this tutorial: https://purgecss.com/css_modules.html

However when I add that bit of code to my postcss.config.js I get the following when I try build:

> Build error occurred
Error: Your custom PostCSS configuration may not export a function. Please export a plain object instead.

postcss.config.css:

const glob = require('glob-all');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const getCSSModuleLocalIdent = require('react-dev-utils/getCSSModuleLocalIdent');

const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';

module.exports = function(webpackEnv) {

    const isEnvDevelopment = webpackEnv === 'development';
    const isEnvProduction = webpackEnv === 'production';
    const shouldUseRelativeAssetPaths = publicPath === './';

    const getStyleLoaders = (cssOptions, preProcessor) => {
        const loaders = [
            isEnvDevelopment && require.resolve('style-loader'),
            isEnvProduction && {
                loader: MiniCssExtractPlugin.loader,
                options: shouldUseRelativeAssetPaths ? { publicPath: '../../' } : {}
            },
            {
                loader: require.resolve('css-loader'),
                options: cssOptions
            },
            {
                loader: require.resolve('postcss-loader'),
                options: {
                    ident: 'postcss',
                    syntax: 'postcss-scss',
                    plugins: () => [
                        require('postcss-flexbugs-fixes'),
                        require('postcss-preset-env')({
                            autoprefixer: {
                                flexbox: 'no-2009'
                            },
                            stage: 3
                        }),
                        require('@fullhuman/postcss-purgecss')({
                            content: [ paths.appHtml, ...glob.sync(path.join(paths.appSrc, '/**/*.{js,jsx}'), { nodir: true }) ],
                        }),
                        require('postcss-normalize')
                    ].filter(Boolean),
                    sourceMap: isEnvProduction && shouldUseSourceMap
                }
            }
        ].filter(Boolean);
        if (preProcessor) {
            loaders.push({
                loader: require.resolve(preProcessor),
                options: {
                    sourceMap: isEnvProduction && shouldUseSourceMap
                }
            });
        }
        return loaders;
    };

    return {

        /* {...} */

        module: {
            rules: [

              /* {...} */
    
              {
                oneOf: [

                    /* {...} */
    
                    {
                        test: /\.module\.(scss|sass)$/,
                        use: getStyleLoaders(
                            {
                                importLoaders: 2,
                                sourceMap: isEnvProduction && shouldUseSourceMap,
                                modules: true,
                                getLocalIdent: getCSSModuleLocalIdent
                            },
                            'sass-loader'
                        )
                    }
    
                    /* {...} */

                ]
              }
    
              /* {...} */

            ]
        },

        /* {...} */
        
    };

};

Any idea how to get this working for nextjs and css modules?

strangeQuirks
  • 4,761
  • 9
  • 40
  • 67
  • Could you share you `postcss.config.js` file? Also, if you have a Next.js project you should be following the [PurgeCSS Next.js guide](https://purgecss.com/guides/next.html) instead. – juliomalves Mar 05 '21 at 22:48
  • I basically set my config as that seen in https://purgecss.com/css_modules.html That next js guide doesnt work with css modules however. It basically strips all the css away, so i think i need to follow the guide from https://purgecss.com/css_modules.html. But not sure how to implement that in my postcss.config.js – strangeQuirks Mar 06 '21 at 09:04
  • That's a webpack config file using a `postcss-loader`, it's not meant to be used as your `postcss.config.js` file. – juliomalves Mar 06 '21 at 12:07
  • Any luck getting this to work? – tonestrike Aug 22 '21 at 14:31
  • No unfortunately :( gave up – strangeQuirks Aug 23 '21 at 16:08

0 Answers0