-
Notifications
You must be signed in to change notification settings - Fork 262
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
SASS transformation #5
Comments
Note: whoever writes this transformation must be aware that we auto-prefix already. I know that many SASS compilers do that as well, so we'd have to avoid it |
I'm looking into adding this to stylis, ping me if you have implementation ideas. |
Having a documentation about how to add own css transformation rules would be really useful.
Btw, this is also important part missing in docs. Autoprefixing should be at least configurable to define supported browsers. |
@rauchg @thysultan @operatino why not simply do it like csjs? They allow compile time babel transforms: Links: Real world example using CSSNext, Autoprefixer and a minifier/compressor: That way any postCSS transform can run over the css, you'd open this up to an entire, existing eco system instead of porting a single thing. You could safely remove the autoprefixer as well as it's no longer needed. PostCSS prefixers are fully configurable so there goes another issue.
|
So i already got a working version of this for stylis that i'll push with the next release and it's only like ~40 lines so i'm pretty impressed with the results, the way it works is when it finds a block it skips it but adds it as a block to the end of the string buffer namespaced to it's parent, when the parser finally comes across it again the same thing will happen for any nested blocks within it, recursively until there are no more nested blocks. so this... h1, div {
color: red;
h2, &:before {
color: red;
}
color: blue;
header {
font-size: 12px;
}
} becomes this... h1,
div { color: red; color: blue; }
h1 h2,
h1:before,
div h2,
div:before { color: red; }
h1 header,
div header{ font-size: 12px; } |
^^ v0.8.0 now supports this. |
+1 to cssnext, we would avoid lot's of bugs ( like #68 ) with full-featured css transpiler. Dependency size is not an issue since it's compile time step |
...also it's not mutually-exclusive to Personally I'm looking forward to using postcss-import and postcss-cssnext.<style jsx>{`
@import './variables.css';
.button {
& .icon { background: var(--color-danger); }
}
`}</style> In fact, you can use this approach above to take the CSS out of your .jsx files, if mixing them together isn't for you :)/* components/button/index.js */
<div>
<a href='' class='button'>Click me</a>
<style jsx>{`@import './style.css';`}</style>
</div> /* components/button/style.css */
.button { background: var(--color-danger); } |
Btw, I tried looking at getting postcss implemented in styled-jsx, but postcss's API is async while (afaik) Babel plugins are sync :\ |
Good point. Unlike styled-components we don't compile at run-time so we could as well use PostCSS and take advantage of its ecosystem and the AST. cc @rauchg
Using PostCSS sounds like a good idea but I wouldn't go as far as picking an opinionated preset of plugins (like cssnext) that sometimes can't fully polyfill css features (e.g. custom properties or color functions). IMHO we would need a custom plugin to add scoping, autoprefixer and
correct |
One of the reasons I don't like postcss is that it increases the compilation time and memory allocation very significantly :( |
That said, I think there's a lot of merit to supporting features like
|
@drcmda I wrote that plugin for styled-jsx! Actually I never read your post and while I was writing the babel plugin I found the csjs one 😅 (should have read the thread more carefully) https://www.npmjs.com/package/styled-jsx-postcss It works on styled-jsx transformed code so autoprefixing is still done by styled-jsx. Hope you find it useful. cc @rstacruz @operatino @sidorares |
@rstacruz Like scss? I'm adding this in the next version of stylis and other scss like features see the changelog, including the possibility for @imports via middleware, though i'm still not yet sure if middleware should be a single function that you can optionally compose from many functions as well or an array of middlewares that are composed internally. |
@giuseppeg , this is amazing. Exactly what i was looking for, but i am having difficulties with implementing it with nextjs. Fails at babel compilation with below error. Any pointers or examples i can look at that implements into nextjs, since styled-components is used by default.
Below is my .babelrc
|
@raavanan can you open an issue on the styled-jsx-postcss repo? I need to update the dependency to |
@rauchg people who want advanced features/preprocessing can now use styled-jsx-postcss. I could add a section to the README if you want. Can we close this issue or do you want to keep it open for reference? |
Just for reference, a |
I couldn't agree more, I think these are pretty mandatory actually, especially & |
Those can all be achieved with postcss plugins and https://www.npmjs.com/package/styled-jsx-postcss |
@giuseppeg this is awesome. Thanks a lot! |
What about stylus? /cc @giuseppeg |
I would really like to see example how to use scss syntax with styled-jsx-postcss. PostCSS with ton of plugins and modules makes me confused how to even integrate it with webpack and react. :) |
Probably outside the scope of this module, but we could document how to make this work:
The text was updated successfully, but these errors were encountered: