Skip to content
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

How to use it with next-compose-plugins and next-sass? #9

Open
VBagmut opened this issue Nov 10, 2018 · 25 comments
Open

How to use it with next-compose-plugins and next-sass? #9

VBagmut opened this issue Nov 10, 2018 · 25 comments

Comments

@VBagmut
Copy link

VBagmut commented Nov 10, 2018

No description provided.

@lucleray
Copy link
Owner

lucleray commented Nov 11, 2018

You should be able to use it like this :

// next.config.js
const withPlugins = require('next-compose-plugins');
const purgecss = require('next-purgecss');
const sass = require('@zeit/next-sass');

const nextConfig = {
  // your next.js configuration
};

const plugins = [
  [sass, {
    // purgecss config
  }],
  [purgecss, {
    // sass config
  }]
];

module.exports = withPlugins(plugins, nextConfig);

Let me know if it works for you 🙂

@nagman
Copy link

nagman commented Feb 19, 2019

Does it work with SASS Modules ?
I'm using this config with cssModules on true, and all my css in components and pages is removed.
Only my global css remains intact.

@lucleray
Copy link
Owner

Can you share a reproduction?

The order of the plugins might be the issue.

@nagman
Copy link

nagman commented Feb 20, 2019

Sure, here's my next.config.js:

const withPlugins = require('next-compose-plugins');
const css = require('@zeit/next-css');
const sass = require('@zeit/next-sass');
const purgecss = require('next-purgecss');

const sassConfig = {
	cssModules: true,
	cssLoaderOptions: {
		localIdentName: '[local]__[hash:base64:5]',
	},
	sassLoaderOptions: {
		includePaths: ['theme'],
		outputStyle: 'compressed',
	},
};

const nextConfig = {};

module.exports = withPlugins(
	[css, [sass, sassConfig], purgecss],
	nextConfig,
);

@lucleray
Copy link
Owner

Thanks!

I'm not really familiar with next-compose-plugins, but could you try to put purgecss first in the list ?

Like this:

module.exports = withPlugins(
	[purgecss, css, [sass, sassConfig]],
	nextConfig,
);

@nagman
Copy link

nagman commented Feb 20, 2019

If I do this, node throws me this error:

TypeError: webpack is not a function
    at Object.webpack (/home/adelios/dev/wth/node_modules/next-purgecss/index.js:29:12)
    at Object.webpack (/home/adelios/dev/wth/node_modules/@zeit/next-css/index.js:30:27)
    at webpack (/home/adelios/dev/wth/node_modules/@zeit/next-sass/index.js:47:27)
    at Object.webpack (/home/adelios/dev/wth/node_modules/next-size/index.js:21:12)
    at Object.getBaseWebpackConfig [as default] (/home/adelios/dev/wth/node_modules/next/dist/build/webpack-config.js:290:32)
    at HotReloader.getWebpackConfig (/home/adelios/dev/wth/node_modules/next/dist/server/hot-reloader.js:165:37)
error Command failed with exit code 1.

@lucleray
Copy link
Owner

@nagman Awesome! I have published a new version of the package (3.0.1), can you try it and tell me if it works?

@nagman
Copy link

nagman commented Feb 20, 2019

Thanks @lucleray!
Now I don't get any error, but my CSS modules are still thrown away.

@lucleray
Copy link
Owner

Oh, that's unexpected. It may be the underlying packages :

https://github.com/FullHuman/purgecss
https://github.com/FullHuman/purgecss-webpack-plugin

I don't really have control over those. I found an open issue that seems to be related to yours here : FullHuman/purgecss#163

@fractefactos
Copy link

fractefactos commented Apr 7, 2019

I have the same problem but this make can i work without any error, thanks @nagman!

@nagman Awesome! I have published a new version of the package (3.0.1), can you try it and tell me if it works?

@nagman
Copy link

nagman commented Apr 8, 2019

@dr-martin I don't understand what you mean by

this make can i work without any error

Does it work? Or does it fail without any error?

If it works for you, I'd like to see your next.config.js and your package.json.

@skywickenden
Copy link

@dr-martin & @nagman Did Either of you manage to solve this. I have the same problem.

@nagman
Copy link

nagman commented Jun 12, 2019

@skywickenden Nope. I've given up with this.

@serhiislobodian
Copy link

Had same error, worked for me only without any of sass config options (and next-css can be omitted). Like:

const withPlugins = require('next-compose-plugins');
const sass = require('@zeit/next-sass');
const purgecss = require('next-purgecss');

const nextConfig = {};

module.exports = withPlugins(
	[sass, purgecss],
	nextConfig,
);

@bhkim526
Copy link

I am having the same issue. Anyone solved it?

@rinkusam12
Copy link

Anyone still facing the problem of using purgecss with css module, use @fullhuman/postcss-purgecss with the postcss config, see their site for usage documentation. The problem with this plugin is it uses purgecss webpack loader and it runs before the hashed CSS class is generated. Hence it removes all the CSS. With the postcss purge it runs on the final CSS that gets generated.

@bhkim526
Copy link

@rinkusam12 Thank you for your tips but @fullhuman/postcss-purgecss still won't work since Next.js generated HTML dynamically so can't set the content. Please let me know if you have a better way to set the content. Thank you.

const purgecss = require('@fullhuman/postcss-purgecss')
postcss([
purgecss({
content: ['./src/**/*.html']
})
])

@rinkusam12
Copy link

rinkusam12 commented Oct 28, 2019

@bhkim526 Its not working because you are targetting the wrong file. replace content: ['./src/**/*.html'] with " content: ['components/**/*.js', 'pages/**/*.js']

@th-devangnaghera
Copy link

th-devangnaghera commented Dec 20, 2019

Is anybody able to fix the issue? I am stuck as well. I tried all the mentioned solutions but none of them working as expected.
Her's my sass config
[ sass, { cssModules: true, cssLoaderOptions: { localIdentName: isProd ? '[hash:base64:8]' : '[name]_[local]_[hash:base64:5]', }, }, ],

@dpkpaa
Copy link

dpkpaa commented Jun 26, 2020

i have to use purgecss with compose-plugins only i tried all of the config that is mention above but it's not working my css chunk is showing 100% of code is unused

@tilman
Copy link

tilman commented Jul 16, 2020

I had the same problem and ended up in a solution without using any next plugins at all, by only using the postcss config. Just let the next.config.js file untouched and create a new file postcss.config.js with following content:

module.exports = {
    plugins: [
        "postcss-flexbugs-fixes",
        [
            '@fullhuman/postcss-purgecss',
            {
                content: [
                    './pages/**/*.{js,jsx,ts,tsx}',
                    './pages/**/**/*.{js,jsx,ts,tsx}',
                    './pages/**/**/**/*.{js,jsx,ts,tsx}',
                    './pages/**/**/**/**/*.{js,jsx,ts,tsx}',
                    './components/**/*.{js,jsx,ts,tsx}',
                    './components/**/**/*.{js,jsx,ts,tsx}',
                    './components/**/**/**/*.{js,jsx,ts,tsx}',
                    './components/**/**/**/**/*.{js,jsx,ts,tsx}',
                ]
            }
        ],
        [
            "postcss-preset-env",
            {
                "autoprefixer": {
                    "flexbox": "no-2009"
                },
                "stage": 3,
                "features": {
                    "custom-properties": false
                }
            }
        ],
    ]
};

@dpkpaa
Copy link

dpkpaa commented Jul 20, 2020

react slick slider and phone-input2 styles are removing which is used in my components

@kayix
Copy link

kayix commented Aug 22, 2020

I had the same problem and ended up in a solution without using any next plugins at all, by only using the postcss config. Just let the next.config.js file untouched and create a new file postcss.config.js with following content:

module.exports = {
    plugins: [
        "postcss-flexbugs-fixes",
        [
            '@fullhuman/postcss-purgecss',
            {
                content: [
                    './pages/**/*.{js,jsx,ts,tsx}',
                    './pages/**/**/*.{js,jsx,ts,tsx}',
                    './pages/**/**/**/*.{js,jsx,ts,tsx}',
                    './pages/**/**/**/**/*.{js,jsx,ts,tsx}',
                    './components/**/*.{js,jsx,ts,tsx}',
                    './components/**/**/*.{js,jsx,ts,tsx}',
                    './components/**/**/**/*.{js,jsx,ts,tsx}',
                    './components/**/**/**/**/*.{js,jsx,ts,tsx}',
                ]
            }
        ],
        [
            "postcss-preset-env",
            {
                "autoprefixer": {
                    "flexbox": "no-2009"
                },
                "stage": 3,
                "features": {
                    "custom-properties": false
                }
            }
        ],
    ]
};

its not working mate :(

@armsteadj1
Copy link

armsteadj1 commented Oct 14, 2020

I did similar to @tilman .

I copied what nextjs says to start with into postcss.config.js then added @fullhuman/postcss-purgecss. Finally I removed the next-purgecss module entirely from my nextjs.config.

I included my additional node_modules and whitelist example because it is different than how next-purgecss does it, as it is a newer version of the purgecss plugin.

Unfortunately, this solution doesn't use the next-purgecss module, although it gave me all of my css module css. I'm not sure the best path to including this solution in the existing plugin. Possibly upgrading to the newest version of @fullhuman/postcss-purgecss would fix it. I'll try that if i get a chance.

const glob = require("glob-all");

module.exports = {
  plugins: [
    "postcss-flexbugs-fixes",
    [
      "postcss-preset-env",
      {
        autoprefixer: {
          flexbox: "no-2009",
        },
        stage: 3,
        features: {
          "custom-properties": false,
        },
      },
    ],
    [
      "@fullhuman/postcss-purgecss",
      {
        content: glob.sync(
          [
            "./pages/**/*.js",
            "./components/**/*.js",
            "./blogs/**/*.js",
            "./node_modules/react-bootstrap/**/*",
            "./node_modules/react-bootstrap-range-slider/**/*",
          ],
          {
            nodir: true,
          }
        ),
        safelist: {
          greedy: [
            /^float/,
            /^btn.*/,
            /.*(sm|md|lg|xl)+.*/,
            /text.*/,
            /font.*/,
            /container.*/,
            /.*active.*/,
            /.*nav-tabs.*/,
          ],
        },
      },
    ],
  ],
};

@Vsion
Copy link

Vsion commented Apr 2, 2022

Has anyone solved the problem (with cssModules)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests