Skip to content

Minify Global Styles#132

Closed
niksurff wants to merge 15 commits into
vercel:masterfrom
niksurff:feature/minify-global-styles
Closed

Minify Global Styles#132
niksurff wants to merge 15 commits into
vercel:masterfrom
niksurff:feature/minify-global-styles

Conversation

@niksurff
Copy link
Copy Markdown

@niksurff niksurff commented Feb 16, 2017

An attempt to fix #120.

My first thought was to implement a minifier (or import one). But we already got our transformer!
So why not send the global styles through without assigning an id (and adding a suffix to selectors)?
On further thinking about it, it even seems more correct. As of now global styles don't get transformed at all which means no Browser prefixing etc.

Am I missing something? 🙉

Obviously tests don't pass, yet. Also proposed changes need a bit of refactoring/ commenting if accepted. 💩

Update: Sorry, a bit to many commits...

@niksurff niksurff changed the title Minify Global Styles [WIP] Minify Global Styles Feb 16, 2017
Comment thread src/babel.js Outdated

if (isGlobal) {
path.replaceWith(makeStyledJsxTag(id, css.source || css, css.modified))
const transformedCss = transform(null, (css.source || css))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nothing wrong with this but you could also move let transfromedCss (L306) to L298

Copy link
Copy Markdown
Collaborator

@giuseppeg giuseppeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good so far, you may want to pull master.
If you fix the tests I'll merge.

Update: Sorry, a bit to many commits

no worries I'll squash before merging

@giuseppeg
Copy link
Copy Markdown
Collaborator

@nikvm we rewrote the css compiler so when you have time you should rebase your changes.

@niksurff
Copy link
Copy Markdown
Author

Still on it, by the way. Sorry for the delay 🌴

@niksurff
Copy link
Copy Markdown
Author

niksurff commented Mar 1, 2017

@giuseppeg this should do the trick.

@niksurff niksurff changed the title [WIP] Minify Global Styles Minify Global Styles Mar 1, 2017
export default (() => <div data-jsx={793889750}>
<p data-jsx={793889750}>test</p>
<_JSXStyle styleId={3149549172} css={"body { background: red }"} />
<_JSXStyle styleId={3149549172} css={" body {background: red }"} />
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should suppress those leading whitespaces

Copy link
Copy Markdown
Author

@niksurff niksurff Mar 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is a stylis bug, isn't it?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might just trim() the transformed css.

Copy link
Copy Markdown
Collaborator

@giuseppeg giuseppeg Mar 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be fixable here https://github.com/zeit/styled-jsx/blob/master/lib/style-transform.js#L120-L128

either try to do blob = blob.trim() before L121 or move L127 to L120

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the space is there because of this https://github.com/thysultan/stylis.js/blob/master/stylis.js#L905

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikvm actually you might want to just add to L120

if (prefix.length === 0) {
    return blob.trim()
}

since it's using an 0 length string for global allowing the middleware to only handle non-global selectors.

Copy link
Copy Markdown
Author

@niksurff niksurff Mar 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tip @thysultan. I just pushed the fix. 🐛

Comment thread src/babel.js Outdated
null,
(css.modified || css)
)
} else if (useSourceMaps) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have source maps for global styles too I think.

Maybe you can do something like this:

if (useSourceMaps) {
   transformedCss = [
       transform(
          isGlobal ? null : String(state.jsxId),
          // ...
       ),
       // ...
   ]
} else {
      transformedCss = transform(
         isGlobal ? null : String(state.jsxId),
         // ...
      )
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't even think about that. Will fix.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

☄️ 0f7b202

@giuseppeg
Copy link
Copy Markdown
Collaborator

@nikvm awesome, the PR looks good thank you! Did you have the chance to test source maps?

@niksurff
Copy link
Copy Markdown
Author

niksurff commented Mar 4, 2017

@giuseppeg I added a global style tag to the source maps fixture here and the expected output here. But I didn't check the generated mapping comment.

@rauchg
Copy link
Copy Markdown
Member

rauchg commented Mar 5, 2017

Would love to merge this! Just need to update the tests. Thanks for all your help so far @nikvm

@fatfisz
Copy link
Copy Markdown
Contributor

fatfisz commented May 13, 2017

Hi, this would be great to have in styled-jsx, but it's been not merged for two months. Is there something I could do to help get it going?

@giuseppeg
Copy link
Copy Markdown
Collaborator

@fatfisz after #148 this branch need to be reworked. I'll try to include a fix in the next release.

@giuseppeg
Copy link
Copy Markdown
Collaborator

fixed in #194 #198

@giuseppeg giuseppeg closed this May 21, 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.

Global CSS is not minified

5 participants