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 not being run on css modules during build

See original GitHub issue

Bug report

Describe the bug

When I develop locally the css vars in my css modules (eg: background: var(--white);) get converted by postcss-preset-env to the actual values stored in a vars.css file which look like this:

:root {
  --white: #F4F4F4;
  /* ...etc */
}

This also gets applied to a global stylesheet.

However, when I run next build, it seems like postcss-preset-env doesn’t recognise these variables and generates css that includes background: var(--white); with no fallback. The CSS in my global stylesheet does get converted.

I have a PostCSS config that looks like this:

module.exports = {
  plugins: {
    'postcss-flexbugs-fixes': {},
    'postcss-import': {},
    'postcss-mixins': {},
    'postcss-calc': {},
    'postcss-nested': {},
    'postcss-color-mod-function': {
      importFrom: [
        './assets/styles/vars.css',
      ],
    },
    'postcss-preset-env': {
      stage: 1,
    },
  },
}

and I’m not using next-css.

Expected behavior

postcss-preset-env should convert the CSS variables in my CSS modules when running next build.

System information

  • OS: MacOS
  • Version of Next.js: 9.2.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
bravokiloechocommented, Apr 2, 2020

I fixed it by modifying the config of postcss-present-env to include an importFrom setting that loaded the definition of the CSS vars:

'postcss-preset-env': {
      stage: 1,
      preserve: false,
      importFrom: [
        './assets/styles/vars.css',
      ],
},
0reactions
balazsorban44commented, Jan 30, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

postcss-preset-env not being run on css modules during build
However, when I run next build , it seems like postcss-preset-env doesn't recognise these variables and generates css that includes background: ...
Read more >
Loading PostCSS "postcss-preset-env" plugin failed: Cannot ...
As it appears this error will temporarily get fixed by installing the latest version of Node.js . So to fix this follow these...
Read more >
postcss-preset-env - npm
Start using postcss-preset-env in your project by running `npm i ... PostCSS to build your CSS, you can simply add PostCSS Preset Env...
Read more >
postcss/postcss - Gitter
I am having issues getting postcss-preset-env to do anything at all. Executing it with the PostCSS-CLI, which runs other plugins successfully... My postcss....
Read more >
css-loader | webpack - JS.ORG
And run webpack via your preferred method. If, for one reason or another, you need to extract CSS as a file (i.e. do...
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