question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Postcss-preset-env only add fallbacks when using the variables in same file

See original GitHub issue

Is this a bug report?

I’m not sure, could be me using it wrong.

Did you try recovering your dependencies?

I created a completely new create-react-app project. So everything should be up to date.

Environment

Environment Info:

System: OS: macOS 10.14 CPU: x64 Intel® Core™ i5-5257U CPU @ 2.70GHz Binaries: Node: 10.10.0 - ~/.nvm/versions/node/v10.10.0/bin/node Yarn: 1.10.1 - /usr/local/bin/yarn npm: 6.4.1 - ~/.nvm/versions/node/v10.10.0/bin/npm Browsers: Chrome: 70.0.3538.77 Firefox: 62.0.3 Safari: 12.0 npmPackages: react: ^16.6.0 => 16.6.0 react-dom: ^16.6.0 => 16.6.0 react-scripts: 2.0.5 => 2.0.5 npmGlobalPackages: create-react-app: Not Found

Steps to Reproduce

(Write your steps here:)

  1. create new react app
  2. add css variable in index.css, and then use that variable both in index.css and app.css.
  3. Apply class names on elements.
/* index.css */
:root {
	--color: red;
}

.text-a {
	font-size: 22px;
	color: var(--color);
}

/* app.css */
.text-b {
	font-size: 22px;
	color: var(--color);
}

Expected Behavior

With postcss-preset-env I expect it apply fallbacks on both classes. like this.

.text-a { 
    font-size:  22px;
	color:  red;
	color:  var(--color);
}

Actual Behavior

It only happens on the class that is written in the same file as root variables is applied. Which in this case is .text-a

Importing that file doesn’t seem to solve it either.

Reproducible Demo

https://github.com/rickbrunstedt/postcss-cra-bug-maybe

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
bluesbrozcommented, Mar 21, 2019

I had the same issue.

The problem is that imported css-files are processed separately by webpack loaders and the plugin which adds fallbacks (postcss-custom-properties inside of postcss-preset-env in postcss-loader) just doesn’t aware of other css-files in that moment.

There is option importFrom which you can provide to that plugin, and you can declare it in ‘postcss-preset-env’ (importFrom) (it will be passed down to ‘postcss-custom-properties’).

But unfortunately there is no way to pass this option to ‘postcss-preset-env’ right now: webpack.config.js.

{
  // Options for PostCSS as we reference these options twice
  // Adds vendor prefixing based on your specified browser support in
  // package.json
  loader: require.resolve('postcss-loader'),
  options: {
    // Necessary for external CSS imports to work
    // https://github.com/facebook/create-react-app/issues/2677
    ident: 'postcss',
    plugins: () => [
      require('postcss-flexbugs-fixes'),
      require('postcss-preset-env')({
        autoprefixer: {
          flexbox: 'no-2009',
        },
        stage: 3,
      }),
    ],
    sourceMap: isEnvProduction && shouldUseSourceMap,
  },
},
0reactions
iansucommented, Jun 30, 2021

These plugins only work with CSS variables defined on the root. There isn’t really a good general solution for this problem but CSS custom properties are widely supported in browsers now so this shouldn’t be an issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

css modules, postcss + webpack with IE 11 fallback
What's happening is I have to import in each module for the fallback to appear. That only works for the local css files...
Read more >
postcss-preset-env - npm
Start using postcss-preset-env in your project by running `npm i ... settings your CSS bundle will only ever contain the needed fallbacks.
Read more >
PostCSS Preset Env: Babel for CSS - DEV Community ‍ ‍
Depending on the config, postcss-preset-env includes regular values alongside CSS variables as fallback for browsers that do not support CSS ...
Read more >
Using with Preprocessors - Tailwind CSS
A guide to using Tailwind with common CSS preprocessors like Sass, Less, ... to this problem is to never mix regular CSS and...
Read more >
Add postcss-preset-env [#3170864] | Drupal.org
Drupal 10 is expected to launch 14 December, and one of the key new features in Drupal 10 is Claro administration theme (replacing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found