Skip to content

I'm using Astro. Being able to use importFrom was a big deal. What is the workaround besides not upgrading to postcss-preset-env v8? #834

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

Closed
romainmenke opened this issue Feb 3, 2023 · 16 comments · Fixed by #841

Comments

@romainmenke
Copy link
Member

          I'm using Astro. Being able to use `importFrom` was a big deal. What is the workaround besides not upgrading to postcss-preset-env v8? 

My postcss config:

const postcssPresetEnv = require("postcss-preset-env");

module.exports = {
  plugins: [
    postcssPresetEnv({
      stage: 0,
      features: {
        "custom-media-queries": {
          importFrom: ["./src/styles/common/queries.css"],
        },
      },
      autoprefixer: true
    })
};

Originally posted by @djmtype in #822 (comment)

@romainmenke
Copy link
Member Author

@djmtype This was discussed in detail here : #192

Is it possible to use @import url('./src/styles/common/queries.css') in CSS with Astro?

@djmtype
Copy link

djmtype commented Feb 3, 2023

@romainmenke while that was always possible, and it won't throw any errors, the media queries in that file aren't being applied. Maybe because I'm using custom media queries like @custom-media --viewport-sm (width >= 55rem) too, and they need to be transformed by postcss as well?

From my understanding those custom media queries needed to be loaded by postcss beforehand which is why importFrom worked. Even if I add these rules directly to my css file, it fails to recognize the custom media queries used within Astro's <style> tags .

@romainmenke
Copy link
Member Author

There is a bug in whatever build/bundle tool you are using if they are not applied when using @import. Switching to postcss-import should be a valid workaround for that bug.


Our concern is what happens when postcss-preset-env decides not to run custom-media.
This will happen after the feature is shipped in browsers.

Removing importFrom was the only possible way forward.
It was a hack that should never have been added to the plugin.


For global styles that need to be known to component styles we are considering creating a new plugin :

#192 (reply in thread)

This would inject and expose CSS and remove it again at the end.
It would allow other features to hook into and depend on the shared CSS without bloating all files.

It allows you to configure your toolchain for what you know will come together on a single page in a browser.

This is not a polyfill and would never be part of postcss-preset-env.
It would be a dev tool to work around how certain frameworks process CSS.

@djmtype
Copy link

djmtype commented Feb 3, 2023

@romainmenke Thanks for the clarity. Much appreciated.

@mdmoreau
Copy link

mdmoreau commented Feb 4, 2023

Ran into this same issue and eventually came across https://www.npmjs.com/package/postcss-inject. It hasn't been updated in a few years, but seems to work perfectly in my limited testing.

@romainmenke
Copy link
Member Author

@mdmoreau We don't advice using that plugin. It is indeed very outdated.
If you want something today you should look at this plugin : https://www.npmjs.com/package/postcss-custom-media-generator

@mdmoreau
Copy link

mdmoreau commented Feb 4, 2023

@romainmenke yeah fair enough - was looking for something to handle custom selectors as well unfortunately.

@mantismamita
Copy link

Hello, using svelte and having the same issue. I tried using @import url("./__customMedia.css"); in a global.css file but the media-queries are simply ignored. If I understand the plan correctly the idea would be to use postcss-global-data which is not yet created? Are there any other workarounds?

@Antonio-Laguna
Copy link
Member

@mantismamita the only workaround for now is to import customMedia in every file

@mantismamita
Copy link

@mantismamita the only workaround for now is to import customMedia in every file

That's what I was afraid of 😢

Antonio-Laguna added a commit that referenced this issue Feb 6, 2023
@Antonio-Laguna
Copy link
Member

@mantismamita Global data is not that far now ^

Antonio-Laguna added a commit that referenced this issue Feb 7, 2023
* Creating new Plugin

#834

* Clarifying doc

* Linting

* Update plugins/postcss-global-data/src/index.ts

Co-authored-by: Romain Menke <11521496+romainmenke@users.noreply.github.com>

* Simpler tests

* Update plugins/postcss-global-data/docs/README.md

Co-authored-by: Romain Menke <11521496+romainmenke@users.noreply.github.com>

* remove unused examples

* adding some keywords

* linting

* regenerating docs

* examples are not mandatory

---------

Co-authored-by: Romain Menke <11521496+romainmenke@users.noreply.github.com>
@Antonio-Laguna Antonio-Laguna reopened this Feb 7, 2023
@Antonio-Laguna
Copy link
Member

@mdmoreau @mantismamita @djmtype you should now be able to use https://www.npmjs.com/package/@csstools/postcss-global-data, we'd love to know how it goes. The instruction is to just put the plugin first and pass an array of files that you wish to be providing data

@djmtype
Copy link

djmtype commented Feb 7, 2023

@Antonio-Laguna Thanks! So far so good.

@mantismamita
Copy link

@Antonio-Laguna no problems so far. I've got it working on a package using svelte and Storybook.

@Antonio-Laguna
Copy link
Member

Thanks both a lot for confirming! This is so helpful! Closing this issue as completed!

@mdmoreau
Copy link

mdmoreau commented Feb 7, 2023

@Antonio-Laguna working great - thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants