Skip to content
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

Fix tagged templates minifier #1836

Merged
merged 7 commits into from
Apr 16, 2020
Merged

Conversation

Andarist
Copy link
Member

@Andarist Andarist commented Apr 5, 2020

Just how HTML should not be parsed with RegExp, CSS should not be either 🤪

By using a proper CSS parser we can write context-based minifying rules to avoid some pitfalls, the code also becomes much more readable.

I'm using a forked Stylis here to achieve the goal because I don't want to expand the input string to proper~ CSS nodes, but rather maintain hierarchy so I can serialize it back to a string which is just a minified input and nothing more than that. It's just a single change in Stylis source code, but it can't be achieved without forking. cc @thysultan - it might be interesting to you to see how I've bent and used Stylis here. Would you maybe also like some license/author header put into the fork file? If yes - how it should look like?

Keep in mind that this probably should land after Stylis v4 gets published to npm. It would be great to avoid checking in its source code.

@Andarist Andarist requested a review from emmatown April 5, 2020 21:40
@changeset-bot
Copy link

changeset-bot bot commented Apr 5, 2020

🦋 Changeset is good to go

Latest commit: 1ed6211

We got this.

This PR includes changesets to release 4 packages
Name Type
@emotion/babel-plugin Patch
@emotion/server Patch
@emotion/react Patch
@emotion/jest Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 5, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 1ed6211:

Sandbox Source
Emotion Configuration

@codecov
Copy link

codecov bot commented Apr 5, 2020

Codecov Report

Merging #1836 into stylis-v4 will increase coverage by 0.02%.
The diff coverage is 100.00%.

Impacted Files Coverage Δ
packages/babel-plugin/src/utils/minify.js 100.00% <100.00%> (ø)
...ugin/src/utils/transform-expression-with-styles.js 95.74% <100.00%> (-0.09%) ⬇️

@Andarist Andarist changed the base branch from next to stylis-v4 April 15, 2020 11:00
@Andarist Andarist marked this pull request as ready for review April 15, 2020 11:00
@Andarist
Copy link
Member Author

@mitchellhamilton this is ready for review now - I'm targeting stylis-v4 branch with this now as ideally, we are going to wait for stylis v4 to be published (this should happen soon-ish) before merging both of those PRs into next. Both can be reviewed beforehand though.

@@ -710,7 +710,7 @@ import { css } from '@emotion/react';
function media(...args) {
return (
/*#__PURE__*/
css(\\"@media (min-width:100px){\\",
css(\\"@media (min-width: 100px){\\",
Copy link
Member

Choose a reason for hiding this comment

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

Why is a space added here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because stylis is crazy-ass parser 🤣and it doesn't try to minify the input too hard, just some whitespace collapsing, nothing more.

It also tries to minimize information that it needs to track along the way, especially given the fact that it often doesn't yet know what it is currently parsing - is it a rule? is it a declaration? don't know - until it reaches either { or ;

So what happens here is basically that whitespace needs to be preserved in css functions like calc (probably not all of it, but at least collapsed to a single space - although it doesn't try to collapse it here). So it just consumes anything between ( and ) as is - or rather between opening and closing tokens. The same delimit function is used for single quotes, double quotes, parenthesis, square brackets and obviously for strings we need to preserve all whitespace whatsoever.

I will probably revisit this later because we can minify this on our side, but didn't want to overcomplicate this PR.

Copy link
Member

@emmatown emmatown left a comment

Choose a reason for hiding this comment

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

LGTM - just a non-blocking curiosity

@Andarist Andarist merged commit c6a79be into stylis-v4 Apr 16, 2020
@Andarist Andarist deleted the fix/tagged-templates-minifier branch April 16, 2020 20:44
@Andarist Andarist mentioned this pull request Apr 16, 2020
Andarist added a commit that referenced this pull request Jun 22, 2020
* Stylis v4 tryout

* Move @import rules in test to be first

* Improved compat plugin

* Add tests for orphanated pseudos

* orphanated -> orphaned

* Upgrade stylis and improve compat plugin

* Improve compat plugin - avoid double compilation

* Shorten compat plugin a little bit

* Add guard for global top-level rules in the compat plugin

* Fix tagged templates minifier (#1836)

* Add TS test to check for excessive instantiation regressions

* Fix tagged templates minifier

* Use non-forked stylis for minification

* Pass missing argument to toInputTree and fix isAutoInsertedRule for parent-less rules

* Temporarily add @emotion/stylis as dep of @emotion/babel-plugin

* Update snapshots

* Make compat plugin be always included

* move removeLabel into omnipresentPlugins

* Stop special-casing @import insertion

* fix getServerStylisCache

* Remove outdated docs around stylisPlugins and prefix options

* Add changesets

* Add note about prefixer being just a plugin to stylisPlugins docs

* Actually use officially published Stylis v4

* Improve error message about incorrect @import insertion

* Fix flow errors

* Reword error messages and changeset content

Co-authored-by: Mitchell Hamilton <mitchell@hamil.town>

* update snapshots

* Remove mention of `@import` not being usable in `css` calls as it wasnt true for @emotion/react+<Global/>

* Add mention to the changeset about where one can find a prefixer

* Update .changeset/warm-ties-drop.md

Co-authored-by: Mitchell Hamilton <mitchell@hamil.town>
louisgv pushed a commit to louisgv/emotion that referenced this pull request Sep 6, 2020
* Stylis v4 tryout

* Move @import rules in test to be first

* Improved compat plugin

* Add tests for orphanated pseudos

* orphanated -> orphaned

* Upgrade stylis and improve compat plugin

* Improve compat plugin - avoid double compilation

* Shorten compat plugin a little bit

* Add guard for global top-level rules in the compat plugin

* Fix tagged templates minifier (emotion-js#1836)

* Add TS test to check for excessive instantiation regressions

* Fix tagged templates minifier

* Use non-forked stylis for minification

* Pass missing argument to toInputTree and fix isAutoInsertedRule for parent-less rules

* Temporarily add @emotion/stylis as dep of @emotion/babel-plugin

* Update snapshots

* Make compat plugin be always included

* move removeLabel into omnipresentPlugins

* Stop special-casing @import insertion

* fix getServerStylisCache

* Remove outdated docs around stylisPlugins and prefix options

* Add changesets

* Add note about prefixer being just a plugin to stylisPlugins docs

* Actually use officially published Stylis v4

* Improve error message about incorrect @import insertion

* Fix flow errors

* Reword error messages and changeset content

Co-authored-by: Mitchell Hamilton <mitchell@hamil.town>

* update snapshots

* Remove mention of `@import` not being usable in `css` calls as it wasnt true for @emotion/react+<Global/>

* Add mention to the changeset about where one can find a prefixer

* Update .changeset/warm-ties-drop.md

Co-authored-by: Mitchell Hamilton <mitchell@hamil.town>
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.

2 participants