css-loader: `@import` with media query produces incorrect output when the imported file is a entry point

Feature Proposal

Support @import list-of-media-queries syntax. Currently the plugin ignores and strips the media query from the output, making runtime-conditional import impossible without additional plugins.

Feature Use Case

@import "./dark.css" (prefers-color-scheme: dark);

Currently the plugin outputs the content of dark.css file without any media query condition, so the CSS is applied unconditionally at runtime:

/* content of dark.css */

A better output would be:

@media (prefers-color-scheme: dark) {
  /* content of dark.css */
}

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 27 (14 by maintainers)

Commits related to this issue

Most upvoted comments

Confirmed, thanks for the quick fix! 🎉

No hurry, we have a workaround via postcss until then. But I’ll be very happy to rip it out again after this fix is in 😄.

Yes, I was right, the problem is in the mini-css-extract-plugin, a fix is simple:

identifier() {
  return `css|${this._identifier}|${this._identifierIndex}|${this.media}|${this.supports}|${this.layer}`;
}

https://github.com/webpack-contrib/mini-css-extract-plugin/blob/master/src/index.js#L168

I am very tired today because I moved to a new country and will send a PR and will do release tomorrow, but if you want to send a PR yourself, feel free you have the fix and the test case, in this case I will do just release 😄

I see…give me time to think how I can fix it and there is a problem (I think in MiniCssExtractPlugin.loader, but need check)

Just fyi, the minimal repro so far does not reproduce it, so it’s definitely something else in that config interfering.

https://github.com/silverwind/media-url

It correctly outputs

@media (prefers-color-scheme: dark) {
body { background: black }

}

Will check further what else in the full config could be a cause.