Skip to content

Use stylis instead of PostCSS (21kB -> 9kB 🎉) - #286

Merged
mxstbr merged 21 commits into
v2from
new-parser
Jan 18, 2017
Merged

mxstbr merged 21 commits into
v2from
new-parser

Conversation

@mxstbr

@mxstbr mxstbr commented Dec 3, 2016 •

Copy link
Copy Markdown
Member

styled-components bundle size (min+gzip) with...

  • PostCSS: 21kB
  • stylis: 9kB 🎉

I hack around stylis API a bit, because we e.g. don't want to prefix keyframes with the generated classname. (something stylis does by default). Maybe @thysultan can chime in here if there's any chance we could change this, otherwise this (slightly ugly) works too!

This isn't done as we need to do dependency cleanup and fix the whitespace changes in the tests, but I wanted to kick off a discussion if we want to go down that route!

cc @geelen

Package size (min+gzip) with...

- PostCSS: 21kB
- stylis: 9kB

:tada:

I hack around stylis model a bit, because we e.g. don't want to prefix
keyframes with the generated classname. (something stylis does by
default). Maybe @thysultan can chime in here if there's any chance we
could change this, otherwise this (slightly ugly) works too!

This isn't done as we need to do dependency cleanup and fix the
whitespace changes in the tests, but I wanted to kick off a first
discussion!

/cc @geelen
@thysultan

Copy link
Copy Markdown

I've updates stylis. You can now opt out of namespacing keyframes & animations, more details on the readme

@mxstbr-bot

mxstbr-bot commented Dec 3, 2016 •

Copy link
Copy Markdown
Fails
🚫 Please include a CHANGELOG entry. You can find it at CHANGELOG.md
Warnings
⚠️ ❗ Big PR
⚠️ There are library changes, but not tests. That's OK as long as you're refactoring existing code

Generated by 🚫 dangerJS

@mxstbr

mxstbr commented Dec 3, 2016

Copy link
Copy Markdown
Member Author

Most of the test failures are whitespace related, but there's two actual regressions when using stylis:

  1. Nesting with > doesn't work:

    styled.div`
      > h1 {
        color: red;
      }
    `
/* The above should generate this CSS.. */
.somerandomhash { }
.somerandomhash > h1 {
  color: red;
}

/* ...but generates this 😢 */
.somerandomhash { }
> h1 {
  color: red;
}
  1. The autoprefixer doesn't autoprefix everything – which I'm not sure it has to, but inline-style-prefixer prefixes a lot more. I wonder where @rofrischmann gets the prefixing information from?

@robinweser

robinweser commented Dec 3, 2016 •

Copy link
Copy Markdown

I am using the caniuse-api to fetch browser versions and prefix support. I might not have all prefixes, but https://github.com/rofrischmann/inline-style-prefixer/blob/master/generator/searchMap.js shows pretty much all properties I am checking. The key represents the caniuse search name.
Additionally there are some special plugins such as different flexbox specifications or value prefixes (e.g. calc()). (see https://github.com/rofrischmann/inline-style-prefixer/tree/master/modules/static/plugins)

Also by default I am only prefixing properties for "modern" browsers (see https://github.com/rofrischmann/inline-style-prefixer#browser-support):

  • Chrome: 30+
  • Safari: 6+
  • Firefox: 25+
  • Opera: 13+
  • IE: 9+
  • Edge 12+
  • iOS: 6+
  • Android: 4+
  • IE mobile: 9+
  • Opera mini: 5+
  • Android UC: 9+
  • Android Chrome: 30+

That means it won't prefix e.g. borderRadius which is stable unprefixed in every modern browser version that is used today.
There might be a tool to generate prefix maps for your own browser support to even reduce the bundle size.
I guess it's pretty much feature-complete in comparison to Autoprefixer, but I had no time to actually add all the Autoprefixer test cases to ensure that.

@mxstbr mxstbr modified the milestone: v2.0 Dec 17, 2016
@thysultan

Copy link
Copy Markdown

@mxstbr stylis has gone through some updates so it now supports everything listed in css-we-support.md and more.

@mxstbr

mxstbr commented Dec 21, 2016

Copy link
Copy Markdown
Member Author

Awesome!!

Will update this PR soon – we'll release it as v2.0.0 (i.e. a breaking change to make sure people don't kill us) in January, together with #227!

@geelen
geelen changed the base branch from master to v2 January 11, 2017 23:54
@geelen geelen mentioned this pull request Jan 12, 2017
@geelen

geelen commented Jan 12, 2017

Copy link
Copy Markdown
Member

So this needs some work before it can proceed—converting objects isn't quite working right (see the failing tests). Can you take a look @mxstbr?

While v0.9 of stylis is out, it introduces features we really really
don't want to have. (Sass-style `@mixin`, variables with `~~var`) I've
upgraded to 0.8, which is the last version that has bugfixes we need but
no unnecessary features.

See the changelog here: https://github.com/thysultan/stylis.js/blob/master/CHANGELOG.md

/cc @thysultan @geelen
@mxstbr

mxstbr commented Jan 12, 2017

Copy link
Copy Markdown
Member Author

While v0.9 of stylis is out, it introduces features we really really don't want to have. (Sass-style @mixin, variables with ~~var) I've upgraded to 0.8, which is the last version that has bugfixes we need but no unnecessary features.

@mxstbr

mxstbr commented Jan 17, 2017

Copy link
Copy Markdown
Member Author

Let's go with 3. then? Would that be a big effort?

@thysultan

Copy link
Copy Markdown

@mxstbr no it's not, will push a patch later today.

@thysultan

Copy link
Copy Markdown

@mxstbr pushed v0.11.0 - CHANGELOG

@mxstbr

mxstbr commented Jan 18, 2017

Copy link
Copy Markdown
Member Author

Now I'm getting duplicates:

// I get (notice the double .d)
.sc-a { } .c { color: blue; } .c > h1 { font-size: 4rem; } .sc-b { } .d { color: blue; } .d > h1 { font-size: 4rem; } .d { color: red; } 
// I want
.sc-a { } .c { color: blue; } .c > h1 { font-size: 4rem; } .sc-b { } .d { color: blue; color: red; } .d > h1 { font-size: 4rem; } 

Intended?

@thysultan

Copy link
Copy Markdown

@mxstbr what is the input?

@mxstbr

mxstbr commented Jan 18, 2017

Copy link
Copy Markdown
Member Author
color: blue;
> h1 { font-size: 4rem; }
color: red;

@thysultan

thysultan commented Jan 18, 2017 •

Copy link
Copy Markdown

@mxstbr Yes that's intend, this is the method that preserves the order in which they are declared to avoid css specificity issues... if it where

color: blue;
color: red;
> h1 { font-size: 4rem; }

They would be together, anything that is not also flat css will break the current context of flat css and append that, when flat css is encountered again a new flat css context is initiated.

@geelen

geelen commented Jan 18, 2017

Copy link
Copy Markdown
Member

@thysultan you shouldn't (I don't think) have any specificity issues grouping all rules at the same level together. As long as the order within each CSS rule is preserved, you should be fine to pull color: red up into the first .d. It's how PostCSS nested and Sass would both output that block.

That said, what you're currently outputting will work fine too. It just might look a little weirder in devtools.

@thysultan

thysultan commented Jan 18, 2017 •

Copy link
Copy Markdown

@geelen edge case:

color: red;

& {
    color: blue;
}

color: orange;

orange color should win but if you group orange with red the blue color will win.

Maybe PostCSS and Sass will group them all under one selector?

@thysultan

thysultan commented Jan 18, 2017 •

Copy link
Copy Markdown

Maybe PostCSS and Sass will group them all under one selector?

Though if that's the case then multiple selectors might break it.

color: red;

&, h1 {
    color: blue;
}

color: orange;

@mxstbr

mxstbr commented Jan 18, 2017 •

Copy link
Copy Markdown
Member Author

In your first case, Sass outputs this:

// Input
color: red;
& {
    color: blue;
}
color: orange;

// Output
div {
  color: red;
  color: orange;
}
div {
  color: blue;
}

So yeah, that breaks it. That behaviour might even be more consistent than Sass?

@geelen

geelen commented Jan 18, 2017

Copy link
Copy Markdown
Member

Hahah true. Just confirmed that Sass behaves like I expect:

image

image

I'm happy with either. Your way is more correct, let's go with that.

@mxstbr

mxstbr commented Jan 18, 2017

Copy link
Copy Markdown
Member Author

Your way is more correct, let's go with that.

Awesome, will update the PR!

@mxstbr

mxstbr commented Jan 18, 2017

Copy link
Copy Markdown
Member Author

As soon as CI passes this is ready to go! 🎉

@mxstbr

mxstbr commented Jan 18, 2017

Copy link
Copy Markdown
Member Author

Failed because of CHANGELOG entry, but we'll have to do that for v2 down the line anyway. SHIPPING!

@k15a

k15a commented Jan 20, 2017 •

Copy link
Copy Markdown
Member

Just wanted to thank you @thysultan. You did amazing work! 💯

@kopax

kopax commented Feb 21, 2017

Copy link
Copy Markdown
Contributor

I have massive change when updating a project from v1.4.3 to v2.0.0-4, I suspect this is because of this.

I have no experience with stylis, and I've learned postcss when I started to use styled-components.

I have now a whole module for making website like bootstrap that require a old version of styled components. Did I miss the discussion of postcss >< stylis ? What are the big change and what about retro compatibility ?

@mxstbr

mxstbr commented Mar 1, 2017

Copy link
Copy Markdown
Member Author

How do you mean massive change @kopax, is everything broken?

@kopax

kopax commented Mar 1, 2017

Copy link
Copy Markdown
Contributor

@mxstbr, the last semicolon of rules break the css.

That can be corrected by it requires some cold blood and redbull, apparently if styled-component release a 2.0.0-5 with the latest stylis version, this would be fix.

By massive I meant that media query issues described in #453 (see http://www.webpackbin.com/4J0u-y_Ff) have a major bug which break massively our stylesheets with ignored css from theses broken components.

@mxstbr

mxstbr commented Mar 1, 2017

Copy link
Copy Markdown
Member Author

That can be corrected by it requires some cold blood and redbull, apparently if styled-component release a 2.0.0-5 with the latest stylis version, this would be fix.

Will do that now!

@thysultan thysultan mentioned this pull request May 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants