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

All parentheses in calc()s being ignored in CSS #9858

Closed
akselinurmio opened this issue Nov 10, 2018 · 7 comments
Closed

All parentheses in calc()s being ignored in CSS #9858

akselinurmio opened this issue Nov 10, 2018 · 7 comments
Labels
status: confirmed Issue with steps to reproduce the bug that’s been verified by at least one reviewer. type: bug An issue or pull request relating to a bug in Gatsby

Comments

@akselinurmio
Copy link
Contributor

akselinurmio commented Nov 10, 2018

Description

In CSS nested calc() functions must be treated as simple parentheses.MDN In Gatsby, however all the parentheses are currently being dropped in the build process (gatsby build).

The issue persists even when the --no-uglify flag is used on gatsby build. Issue doesn't occur on development (gatsby develop).

Steps to reproduce

I wrote a small Gatsby project here that reproduces the issue. I have deployed the repo here.

Here's an outline of the reproducing process:

  1. Add calc() or calculation in parentheses () in some calc() function in one of your CSS files.
  2. Run gatsby build.
  3. Inspect the production CSS and see that your parentheses or nested calc() are gone (and your beautiful layout too).

Expected result

Parentheses in calc() functions alter the computation order of calculations.

Actual result

Parentheses are dropped in Gatsby's production build process.

Environment

  System:
    OS: macOS 10.14.1
    CPU: (4) x64 Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 10.13.0 - ~/.nvm/versions/node/v10.13.0/bin/node
    npm: 6.4.1 - ~/.nvm/versions/node/v10.13.0/bin/npm
  Browsers:
    Chrome: 70.0.3538.77
    Firefox: 63.0.1
    Safari: 12.0.1
  npmPackages:
    gatsby: ^2.0.44 => 2.0.44 
@akselinurmio akselinurmio changed the title All parentheses and nested calc() being ignored in CSS All parentheses in calc()s being ignored in CSS Nov 10, 2018
@leimonio
Copy link
Contributor

leimonio commented Nov 11, 2018

It seems there is an issue with the minification using optimize-css-assets-webpack-plugin.

EDIT:
cssnano is responsible for handling calc optimizations, by reducing its usage for better browser compatibility.

@gatsbot
Copy link

gatsbot bot commented Jan 30, 2019

Old issues will be closed after 30 days of inactivity. This issue has been quiet for 20 days and is being marked as stale. Reply here or add the label "not stale" to keep this issue open!

@gatsbot gatsbot bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Jan 30, 2019
@akselinurmio
Copy link
Contributor Author

No, this is not stale.

@m-allanson m-allanson added not stale and removed stale? Issue that may be closed soon due to the original author not responding any more. labels Jan 31, 2019
@marlonmarcello
Copy link

marlonmarcello commented Apr 3, 2020

I just stumbled upon this same issue.
Did you find a work around @akselinurmio ?

@gtsop
Copy link

gtsop commented Jul 28, 2020

@marlonmarcello

After a longer than usual debugging session, I found this man's legendary post, enjoy:

https://www.eduard.work/adjust-css-minification-in-gatsby/

I'm reposting his solution here in the interest of future-proofing this resource:

exports.onCreateWebpackConfig = ({
  actions,
  stage,
  plugins,
  getConfig
}) => {
  // override config only during 
  // production JS & CSS build
  if (stage === 'build-javascript') {
    // get current webpack config
    const config = getConfig()
    // our new cssnano options
    // are still based on default preset
    const options = {
      cssProcessorPluginOptions: {
        preset: ['default',
          {
            discardComments: {
              removeAll: true
            },
            calc: false,
            reduceTransforms: false,
            minifySelectors: false
          }]
      }
    }
    // find CSS minimizer
    const minifyCssIndex = config.optimization.minimizer.findIndex(
      minimizer => minimizer.constructor.name ===
        'OptimizeCssAssetsWebpackPlugin'
    )
    // if found, overwrite existing CSS minimizer with the new one
    if (minifyCssIndex > -1) {
      config.optimization.minimizer[minifyCssIndex] =
        plugins.minifyCss(options)
    }
    // replace webpack config with the modified object
    actions.replaceWebpackConfig(config)
  }
}

Maybe we should see to actually make this modification on gatsby's webpack source:

cssProcessorPluginOptions: {

(related issues on the interwebs: postcss/postcss-calc#91, cssnano/cssnano#628

@marlonmarcello
Copy link

marlonmarcello commented Jul 29, 2020 via email

@LekoArts
Copy link
Contributor

LekoArts commented May 7, 2021

Closing this as stale since in the meantime Gatsby v3 and updated related packages were released. Please try with the latest versions and if you still see this problem open a new bug report (it must include a minimal reproduction).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: confirmed Issue with steps to reproduce the bug that’s been verified by at least one reviewer. type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

No branches or pull requests

7 participants