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

[gatsby-plugin-postcss] breaks css minification in production #7729

Closed
ooloth opened this issue Aug 29, 2018 · 14 comments
Closed

[gatsby-plugin-postcss] breaks css minification in production #7729

ooloth opened this issue Aug 29, 2018 · 14 comments
Labels
type: bug An issue or pull request relating to a bug in Gatsby

Comments

@ooloth
Copy link
Contributor

ooloth commented Aug 29, 2018

Description

Adding gatsby-plugin-postcss to a Gatsby v2 site seems to cause CSS to no longer be minified in production.

Steps to reproduce

Install the default Gatsby v2 starter, run gatsby build && gatsby serve and note that the production CSS is minified. (So far, so good.)

Now, run npm i gatsby-plugin-postcss precss autoprefixer and add gatsby-plugin-postcss to gatsby-config.

Then, add a postcss.config.js file to the project root:

// postcss.config.js

module.exports = {
  plugins: [require(`precss`), require(`autoprefixer`)()],
}

Finally, run gatsby build && gatsby serve again.

Expected result

Production CSS should still be minified after adding gatsby-plugin-postcss.

Actual result

Production CSS is no longer minified.

Environment

System:
    OS: macOS High Sierra 10.13.6
    CPU: x64 Intel(R) Core(TM) i5-7500 CPU @ 3.40GHz
    Shell: 5.3 - /bin/zsh
  Binaries:
    Node: 10.8.0 - /usr/local/bin/node
    Yarn: 1.7.0 - /usr/local/bin/yarn
    npm: 6.4.0 - /usr/local/bin/npm
  Browsers:
    Chrome: 68.0.3440.106
    Firefox: 61.0.2
    Safari: 11.1.2
  npmPackages:
    gatsby: next => 2.0.0-rc.3
    gatsby-plugin-manifest: next => 2.0.2-rc.1
    gatsby-plugin-offline: next => 2.0.0-rc.1
    gatsby-plugin-postcss: ^1.0.0 => 1.0.0
    gatsby-plugin-react-helmet: next => 3.0.0-rc.1
  npmGlobalPackages:
    gatsby-cli: 1.1.58
@KyleAMathews
Copy link
Contributor

Could you create a small reproduction site for this? Not sure what's going on. Does mini-css-extract-plugin minify by default for us? Perhaps you want this? https://github.com/leodido/postcss-clean

@ooloth
Copy link
Contributor Author

ooloth commented Aug 30, 2018

No problem! Here's a reproduction site: https://github.com/ooloth/gatsby-postcss-test.

It's just the default Gatsby v2 starter with gatsby-plugin-postcss added (along with a couple example postcss plugins). Just run gatsby build && gatsby serve to see the unminified CSS output.

If you comment out gatsby-plugin-postcss and run gatsby build && gatsby serve, the production CSS will be minified as expected.

Yes, I believe mini-css-extract-plugin minifies CSS by default in Gatsby v2, but it seems that gatsby-plugin-postcss may be accidentally removing that functionality?

Thanks for the link to postcss-clean, but if possible I'd prefer to just use the default Gatsby CSS minification even if I've added gatsby-plugin-postcss. I'm guessing a lack of minification isn't an intended side effect of using the plugin.

@KyleAMathews
Copy link
Contributor

KyleAMathews commented Aug 30, 2018

@mdreizin any idea what's happening?

@mdreizin
Copy link
Contributor

I will take a look as soon as I reach home.

@KyleAMathews
Copy link
Contributor

Thanks!

@mdreizin
Copy link
Contributor

@ooloth @KyleAMathews I could reproduce this issue, but I need more time to fix it.

@KyleAMathews
Copy link
Contributor

@mdreizin sounds great — this isn't an urgent bug so can take a bit of time. If you do get blocked on fixing, please write up your guess at the cause so someone else could pick it up if they'd like.

@mdreizin
Copy link
Contributor

@KyleAMathews @ooloth We could achieve minification by using the recommendation from https://github.com/webpack-contrib/mini-css-extract-plugin#minimizing-for-production.

For instance, you can use the following snippet:

gatsby-node.js

exports.onCreateWebpackConfig = ({
  actions,
  stage
}) => {
  const isProduction = stage !== 'develop';

  if (isProduction) {
    actions.setWebpackConfig({
      devtool: false,
      optimization: {
        minimizer: [new OptimizeCssAssetsPlugin({})]
      }
    });
  }
};

@KyleAMathews
Copy link
Contributor

@ooloth how do we know Gatsby is minimizing CSS by default?

@KyleAMathews
Copy link
Contributor

(thanks for the research work @mdreizin)

@ooloth
Copy link
Contributor Author

ooloth commented Aug 31, 2018

@KyleAMathews I'm inferring that Gatsby is minimizing the CSS by default from that fact that when you install the default starter and build it, the CSS inlined in the head is minified.

It only stops being minified after you activate gatsby-plugin-postcss.

Hope that adds up!

@kakadiadarpan kakadiadarpan added help wanted Issue with a clear description that the community can help with. type: bug An issue or pull request relating to a bug in Gatsby and removed help wanted Issue with a clear description that the community can help with. labels Sep 5, 2018
@stefanprobst
Copy link
Contributor

I think this should be resolved with #7934 which adds OptimizeCssAssetsPlugin.

@madeleineostoja
Copy link

This seems to be fixed (using 2.0.0-rc.25), presumably by #7934

@ooloth
Copy link
Contributor Author

ooloth commented Sep 17, 2018

Agreed! It's fixed in 2.0.0-rc.25.

Thanks everyone!

@ooloth ooloth closed this as completed Sep 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

No branches or pull requests

6 participants