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

postcss-custom-media: incorrect rendering with nested rules #1519

Closed
2 of 3 tasks
massimo-cassandro opened this issue Nov 19, 2024 · 14 comments
Closed
2 of 3 tasks

postcss-custom-media: incorrect rendering with nested rules #1519

massimo-cassandro opened this issue Nov 19, 2024 · 14 comments

Comments

@massimo-cassandro
Copy link

Bug description

In custom media queries inside nested rules, the parent selectors are ignored

Source CSS

.some-class {

  .nested {
    flex-direction: column;

    @media (--my-custom-media) {
      flex-direction: row;
    }
  }
}

Expected CSS

.some-class {
  .nested {
    flex-direction: column;
  }
}
@media (min-width: 480px) { /* --my-custom-media */
  .some-class {
    .nested {
      flex-direction: row;
    }
  }
}

Actual CSS

.some-class {
  .nested {
    flex-direction: column;
  }
}
@media (min-width: 480px) { /* --my-custom-media */
  .nested {
    flex-direction: row;
  }
}

Playgound example

No response

Does it happen with npx @csstools/csstools-cli <plugin-name> minimal-example.css?

N/A

Debug output

No response

Extra config

No response

What plugin are you experiencing this issue on?

PostCSS Custom Media Queries

Plugin version

11.0.5

What OS are you experiencing this on?

macOS

Node Version

22.3.0

Validations

  • Follow our Code of Conduct
  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate.

Would you like to open a PR for this bug?

  • I'm willing to open a PR
@massimo-cassandro
Copy link
Author

thank you
it doesn't preserve nested rules, but it works

@romainmenke
Copy link
Member

@massimo-cassandro What was the issue then?
What tool was creating the incorrect output?

@massimo-cassandro
Copy link
Author

Hi I'm using webpack and postcss.
There is no real problem, you lose the nesting of the css rules, but it is not a serious issue

@romainmenke
Copy link
Member

So your original output was also correct and the Actual CSS example posted above isn't what was actually produced by your setup?


I just want to know if there is a tool that is producing incorrect output. Because if there is, then I should fix that.

I can only fix what I understand :)

@massimo-cassandro
Copy link
Author

I understand what you mean and I really appreciate your help.

I did some more tests, and now, after rebuilding my initial configuration (but with postcss-preset-env). and I can't get the result of your demo anymore. Surely, I'm using some wrong config.

I'm using PostCSS with webpack, this is my configuration before adding postcss-preset-env.

/* eslint-env node */
const isDevelopment = process.env.NODE_ENV === 'development';

const postcssConfig = {
 plugins: [

 require('@csstools/postcss-global-data')({
 files: [
 './node_modules/open-props/media.min.css',
 './frontend/css/custom-properties-figma.css',
 './frontend/css/custom-properties.css',
 ]
 }),

 require('postcss-jit-props')({
 ...require('open-props'),
 custom_selector: ':where(html)'
 }),

 require('autoprefixer'),

 require('postcss-custom-media')({
 preserve: isDevelopment
 }),

 ]
};

if (process.env.NODE_ENV === 'production') {
 postcssConfig.plugins.push(
 require('cssnano')({
 preset: 'default',
 })
 );
}
module.exports = postcssConfig;

I don't use postcss-nesting. My autoprefixer reads its config from a .browserslistrc file.

Honestly, I think it's all getting a bit too clunky, and I'm thinking about using Sass for media queries. I'm not too keen on it, I'd like to use custom media, but I don't want to spend hours configuring postCSS plugins...

Thanks again for your help

@romainmenke
Copy link
Member

Thank you for providing this extra info.

I tried this config as best as possible.
I don't have access to your exact stylesheets.

However I don't get any broken output.

For this input:

.some-class {

	.nested {
		flex-direction: column;

		@media (--xxs-only) {
			flex-direction: row;
		}
	}
}

I get:

:where(html) {}

.some-class {

	.nested {
		flex-direction: column;

		@media (min-width: 0px) and (max-width: 239px) {
			flex-direction: row;
		}

		@media (--xxs-only) {
			flex-direction: row;
		}
	}
}

Is it possible that PostCSS is working correctly but that Webpack (or part of your Webpack config) is causing issues?

I don't ever use Webpack to process CSS because Webpack is JavaScript bundler and not a CSS bundler.

@massimo-cassandro
Copy link
Author

I use webpack because I need to process both js and css. I recently switched from rollup to webpack and found that it is the most convenient solution for my work.
Of course, it is possible that my webpack configuration is somehow conflicting with postcss, can you give me the postcss configuration you used to do another test in my environment?

Thank You

@romainmenke
Copy link
Member

Sure!

We have a recipe that is fairly similar to your config.
I've modified it to your config, but removed the references to files that are yours.

#1520

The config is here: https://github.com/csstools/postcss-plugins/blob/repro-1519--ambitious-woodpecker-c6f183a4ef/postcss-recipes/open-props/postcss.config.js

You can run that yourself by:

  • cloning the repo
  • checking out the branch repro-1519--ambitious-woodpecker-c6f183a4ef
  • cd postcss-recipes/open-props
  • npm ci
  • npm run style

@massimo-cassandro
Copy link
Author

Tried cloning the repo: all works as expected
Tried in my project: same problem (actually, your postcss configuration is pretty much the same I did).
At this point the only notable difference, as you noticed, is that I use webpack.
I'll try to do some more testing.
In the meantime, thanks for the help

@romainmenke
Copy link
Member

One thing you could try is to use a dedicated CSS bundler like:

postcss-import has more config options, but @csstools/postcss-bundler has fewer issues.

Then Webpack won't be tasked with processing multiple files, only your entrypoint.
This might be sufficient to avoid the issue.


Might be worth it to create a minimal repro in a GitHub project.
I think your config is quite common and that this should work.
Would be nice to get this resolved :)

@massimo-cassandro
Copy link
Author

Thanks. As soon as I can, I'll try it and let you know

@massimo-cassandro
Copy link
Author

found the problem.
In my project I use fonts from @fontsource and use their sass mixins to load them.
I had a solitary scss file, just for fonts and of course sass-loader in my webpack-config.
After removing sass all works fine.
I tried changing the loader order first but got a lot of errors, so I decided to completely avoid sass.

I honestly didn't imagine it could create such a conflict. I have other projects where I use sass with webpack and postcss (without postcss-custom-media), and I never had any problems.
Anyway, now it works.

Thanks again for your help

@romainmenke
Copy link
Member

That is indeed surprising.
I guess that Sass somehow corrupts these even before postcss runs.

Thank you for investigating further 🙇

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

2 participants