cssinjs / jss

JSS is an authoring tool for CSS which uses JavaScript as a host language.
https://cssinjs.org
MIT License
7.08k stars 400 forks source link

PostCSS plugins adapter #427

Closed DenisIzmaylov closed 7 years ago

DenisIzmaylov commented 7 years ago

Requirements:

Problems:

Solutions:

It would be great to have an adapter which make it possible to re-use PostCSS plugins in JSS and to get the existing ecosystem available without any changes.

Some examples:

  1. Autoprefixer: https://github.com/postcss/autoprefixer/blob/master/lib/autoprefixer.coffee
  2. RTL, which is discussing in #410

The problem is that guys from PostCSS made a big vendor lock-in by using require("postcss") instead of dependency injection pattern.

iamstarkov commented 7 years ago

@DenisIzmaylov wanna explore options and contribute?

iamstarkov commented 7 years ago

do you need rtl? and jss has plugin for autoprefixing

kof commented 7 years ago

PostCSS produces the AST, currently JSS doesn't have one.

kof commented 7 years ago

I will wait with this until there is a specific case. From my experience 95% of things people need are already in place.

kof commented 7 years ago

I will close this for now, because the output is not clear and overhead + work amount to make this happen is big. This would require a whole new concept.

kof commented 7 years ago

Also you don't need all postcss plugins in jss, those 2 have partially very different goals. Only some plugins are really useful.

DenisIzmaylov commented 7 years ago

Thanks for attention.

There is a lot of sugar and "nice to have" plugins which could be ported to JSS.

Almost all of those plugins could be used "AS IS".

Bonus point:

I think we have to define rule:

Or something like that.

kof commented 7 years ago

We can always continue the discussion on a closed issue, closing means only that there are currently no actions planned. If I wouldn't a discussion I would lock the conversation.

kof commented 7 years ago

For e.g. we have jss-vendor-prefixer for prefixing values and we have javascript to use any color library we want, I am using this one, postcss-write-svg and postcss-inline-svg don't make any sense, we can use string template or even import svg as a string.

kof commented 7 years ago

Big differences between jss and postcss are:

  1. JSS is designed to work at runtime in the first place, on the server second.
  2. JSS tries a more functional API from the user standpoint, its not a new language. We try to use javascript features to produce CSS. PostCSS tries to enhance the CSS as a language, like babel. Lots of postcss plugins don't make any sense in JSS, especially syntactic sugar plugins.
DenisIzmaylov commented 7 years ago

Thank you for sharing your point. But in the same time it seems we have to think and to care about new users which could migrate from PostCSS, styled-components and so on.

In other words we should help to reduce any costs for this migration. It means any migration process should be smooth, step-by-step, etc. Otherwise it will be very expensive for the project budget and reduce JSS users. But by increasing the users is important goals. It we'll improve the quality. A lot of solutions are dead just by the one reason - they did not have a right promotion.

So it would be great if:

Sorry, if I understood something incorrect.

kof commented 7 years ago

Thank you for sharing your point. But in the same time it seems we have to think and to care about new users which could migrate from PostCSS, styled-components and so on.

It will mean: rewrite and learn how to do those things in cssinjs. It doesn't mean they can run a magical script and migrate everything easily.

We will not limit anybody.

Constraints are the reason why one lib is a good fit for a particular task. If you have any specific things you want to add, feel free to create a separate issue for a very specific idea.

All activities in GitHub Issues will be more kind-friendly, predictable, expected and polite.

Does this mean my wording is not polite?

Thanks for your feedback, its mostly correct, but very philosophical. The only thing I can tell is that supporting all PostCSS plugins is a very hard task, which will require a complete rewrite. Also I act almost always upon specific feature request. Otherwise it ends up being useless, because we have no one to validate the quality of what we would do.

kof commented 7 years ago

Btw. I someone REALLY wants postcss in jss at runtime, I could write a plugin, which will use the original postcss + allow to use any plugin. But the performance cost would be big + much bigger library size. I would do it or help doing it once at least 1 person has a valid use case to do so and asks.

iamstarkov commented 7 years ago

@DenisIzmaylov here we go https://github.com/lttb/babel-plugin-prejss

iamstarkov commented 7 years ago

dropmic

kof commented 7 years ago

Its only for preprocessing, but yet it gives you everything from postcss.

kof commented 7 years ago

Means one can't regenerate the styles created on the server differently, for e.g. if postcss rtl plugin has been used, you can't switch back to ltr at runtime, like it would be with a native jss plugin, but yet I guess it might be useful.

DenisIzmaylov commented 7 years ago

@iamstarkov commented 9 hours ago: DenisIzmaylov here we go https://github.com/lttb/babel-plugin-prejss

Yep! I've asked Artur to publish it yesterday. :)

On previous week we've started to make jss-from-css adapter which has been originally based on styled-components:

We used stylis.js like styled-components for preparing plain CSS from SCSS. For transforming plain CSS to JSS we used reworkcss. It's isomorphic and now you can use it as:

npm install jss-from-css --save-dev

There was one problem - performance issues on client-side because we have to parse CSS.

To solve it we create the task about babel plugin:

It was good news from Artur yesterday.

Now, when we solved performance issue on client-side transformation, we focused on PostCSS version to keep it integrated on both sides with prejss:

It means this PostCSS adapter and prejss babel plugin could be used together to keep the application code isomorphic:

kof commented 7 years ago

On client-side PostCSS styles are converting to JSS by babel-plugin-prejss

This happens also on the server during the build, no?

DenisIzmaylov commented 7 years ago

With using babel plugin - yes - it's happening on server side during build stage. In this case on client-side we'll get a compiled callback function in final bundle which returns a plain JSS object.

On the server side we have this compilation on module execution stage (when a module is requiring).

This solution should be total replacement for styled-components. Or we missed something which still is good in SC and do not present in JSS?

kof commented 7 years ago

Further discussions reg. styled-components api here: #341