-
-
Notifications
You must be signed in to change notification settings - Fork 608
Support for exports from postcss plugins (other than ICSS ones) #788
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
Comments
@ZeeCoder
|
Hey @evilebottnawi ! I don't expect Simplified use-case: My Exact use-case: If something is unclear, feel free to ask. 👍 |
@ZeeCoder Can you provide PoC or minimum code ( |
All I would need, is a way to make it possible for my PostCSS plugin to declare an export with a js object that could be then imported in the JS file after it also went through the css-loader. ":export" doesn't quite work for me because of two reasons:
My current solution with ":export"https://github.com/ZeeCoder/container-query/pull/99/files#diff-e745f42867c62365cbc66287035d1195R167 import { meta } from './App.pcss';
// This is why this solution is hacky. I need to convert the string to the object
const parsedMeta = JSON.parse(meta.slice(1, -1))
// do stuff with parsedMeta What I would like to be able to doThe postcss plugin would "declare" what it would like css-loader to export: const plugin = 'my-plugin'
export default postcss.plugin(plugin, (options = {}) => (css, result) => {
// Code ...
const meta = // ...
result.messages.push({
type: 'export'
plugin,
name: "meta",
value: meta
})
}) import { meta } from './App.pcss';
// do stuff with meta (meta is already an object here! 🎉) This solution is not actually something I came up with, but it was part of the @next branch of css-loader for a while, but it seems like it never made it into v1: Hope that clears things up @evilebottnawi ! |
It is part of future CSS support inside
In this case it is impossible to avoid using |
But my exact point is that it would not be impossible to avoid JSON.stringify, if css-loader had support for custom exports through postcss messages. I think I gave you all the examples and code I can, not sure if you've read them? If you don't plan to support this use case, I can close the thread. I can just use |
I just want to collect as much information as possible to make the next version convenient for everyone, so I ask many questions. Based on you information looks we need support You need only export? No need import? |
I personally don't need import, just the ability to export stuff to the end user. By using postcss plugin messages in theory multiple independent PostCSS plugins could have custom exports if they needed to. import {result1, result2, result3} from './styles.css';
// here, results can be js objects I would probably limit this api to only export plain objects and simple values like numbers / strings etc, since as you said otherwise it's a fairly low level thing you give access to which might mess things up in the wrong hands. |
@ZeeCoder hm, it is should be possible right now, we exported data in |
I'm not sure I understand @evilebottnawi , so with an example, how do I use const plugin = 'my-plugin';
export default postcss.plugin(plugin, (options = {}) => (css, result) => {
// Code ...
// generating meta somehow
const meta = // ...
// Is this it? 🤔
export.locals = meta;
}) And then is this how I import the meta? import meta from './styles.pcss'; ☝ Is this how it works? Does this still work with css modules enabled though? import styles, { meta } from './styles.pcss'; |
@ZeeCoder friendly ping, we prepare |
Thanks for the heads-up! For the time being I'm using the "hack" I've described, exporting variables using the ICSS ":export": ZeeCoder/container-query#99 |
@ZeeCoder the main problem in what you want to avoid using |
yeah, since it has nothing to do with the css, really. With :export, the css file acts as an intermediary between my plugin and the JavaScript module that does the import. Ideally I wouldn't have to push this data through the css, but rather generate a js module that already has a custom export. the other issue with :export is that the json I'm exporting like this becomes a string, including the quotes! this whole process could be avoided if named exports were supported, so that I wouldn't even need to JSON.stringify / JSON.parse |
@ZeeCoder any ideas how we can implement this? We can support |
I'll set up a reminder to think about this. I'm not too involved in how loaders work tbh, so I'd have to do some study first before I can advise on what to do. |
@ZeeCoder feel free to feedback |
@ZeeCoder what do you think if we solve this on |
Apparently no one is interested in this, it can also be solved by |
Continuing the discussion here: #645 (comment)
I see that currently adding
works, and when the file goes through the css loader, it can be imported in the JS as a custom export.
However as I've outlined I'd like to export a JSON object, and doing so using CSS is not ideal:
When I import this in JS, data is returned as string, with the quotes around it. (Without them it wouldn't pass as valid CSS)
Ideally, I'd want to be able to import a JS object, as outputted by a postcss plugin.
In the "next" branch, there was a time where css-loader would look for postcss plugin messages with the "export" type, but it didn't same to make it in v1. (As outlined here: #645 (comment))
I'm wondering if there's any plan to add support for this in the future?
The text was updated successfully, but these errors were encountered: