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

Restructured types to greatly reduce number of types created during c… #1501

Merged

Conversation

JakeGinnivan
Copy link
Contributor

@JakeGinnivan JakeGinnivan commented Sep 12, 2019

…ompilation

BREAKING CHANGE: There are a few breaking changes, see below

  • withTheme now infers types properly and may require removing the manually specified generic parameter
  • The Theme generic parameter has been removed from a number of types exported from @emotion/styled and @emotion/styled-base.
  • Introduced new CreateThemedStyled type which is exported from emotion-theming, use type to create your own themed styled export. See updated docs
  • WithTheme should be imported from emotion-theming
  • CreateStyledComponentExtrinsic, CreateStyledComponentIntrinsic and CreateStyledComponentBase all have been replaced with CreateStyledComponent

Fixes #1496

What:
This PR simplifies a number of types to try and speed up compilation of TypeScript projects when using emotion

Why:
Our project over doubled compilation time when we upgraded from emotion 9 to emotion 10. This brings compilation time back down to emotion 9 times.

How:

I reduced complexity in the types by removing mapped typeds.

Checklist:

  • Documentation
  • Tests
  • Code complete
  • Changeset

Want to get initial feedback, I have consumed all these types in our large project and have gone through a few iterations of fixing issues already.

@changeset-bot
Copy link

changeset-bot bot commented Sep 12, 2019

🦋 Changeset is good to go

Latest commit: c336eba

We got this.

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

@Andarist
Copy link
Member

@JakeGinnivan I must say that I like changes so far, seems cleaner in many places. This still needs some work and testing but looks really great at this point in time. I would like to applaud this effort and preparing such a nice PR 👏

@JakeGinnivan
Copy link
Contributor Author

Thanks @Andarist, I will keep working through all the feedback. I think things can be improved in a few places and I might try to write some tests around the types as well.

Likely will be a few days, have a busy weekend this weekend so haven't been able to do much on it.

I was also copying types between 3 projects so need to ensure all actually are in sync to test properly.

@JakeGinnivan
Copy link
Contributor Author

I'm also trying to write down as I go. https://medium.com/@jakeginnivan/improving-emotion-10s-typescript-types-622e2d172f6f

This is slowing me down, sorry about that but it's helping me make sure the changes are more deliberate.

@Andarist
Copy link
Member

@JakeGinnivan Don't worry - it's not like we want to rush this. You are doing a tremendous effort here only because you are motivated to do so on your own - what I'm trying to say, I don't expect anything from you, it's OSS. However, I'm of course grateful for all your efforts 👍

The writeup itself is a great idea and can't wait to dive into it - I'm going to be busy in the following days, but I promise to look at it and on your latest commits afterwards.

@Andarist
Copy link
Member

Andarist commented Nov 4, 2019

@hsz not really, this is somewhat a breaking change and as we are preparing v11 right now it’s just better to include this there. We are going to release a prerelease version with those types and without any breaking changes (other than that). So you should be able to switch to beta v11 line easily if you want to use those typings.

@JakeGinnivan JakeGinnivan force-pushed the feature/improve-type-performance branch from eee5398 to c336eba Compare November 4, 2019 12:16
@codecov
Copy link

codecov bot commented Nov 4, 2019

Codecov Report

Merging #1501 into next will increase coverage by 0.08%.
The diff coverage is n/a.

Impacted Files Coverage Δ
packages/babel-plugin-emotion/src/emotion-macro.js 90.47% <0%> (+1.28%) ⬆️

@elektronik2k5
Copy link

Sorry for the (somewhat) off topic: is there a list of goals or an umbrella issue for v11 anywhere? A plan or timeline? All I was able to find was the v11 milestone tagged issues.

@Andarist
Copy link
Member

Andarist commented Nov 4, 2019

@elektronik2k5 there is no such umbrella ticket at the moment, but it will be probably created soon

@JakeGinnivan thanks for working on this so hard, I'm going to merge this into v11 right now and comment here in some time, once this gets published to npm (under next tag)

@Andarist Andarist merged commit a72e6dc into emotion-js:next Nov 4, 2019
@github-actions github-actions bot mentioned this pull request Nov 4, 2019
@emmatown emmatown mentioned this pull request Nov 4, 2019
@Andarist
Copy link
Member

Andarist commented Nov 5, 2019

This has just been published as 11.0.0-next.1

@fabien0102
Copy link
Contributor

Hi!

I'm not totally sure if it was addressed somewhere before, but when I update to the @next version with typescript in strict mode, since props.theme become optional, nothing compile, in a very nasty way 😕 (a lotttttttt of errors, really ^^)

I've isolated the usecase in this codesandbox -> https://codesandbox.io/s/charming-nobel-7nbh9
It's basically the documentation implementation with typescript in strict mode:

image

This seams to be related to this PR, so I would like to know if it's an issue (and I will open a real issue) or if I'm missing something obvious 😅

Thanks for this great library! Really looking forward the v11 😍

@Andarist
Copy link
Member

As you have used CreateStyled I believe this shouldn't happen right now. @JakeGinnivan could you take a look at this one?

@JakeGinnivan JakeGinnivan deleted the feature/improve-type-performance branch November 14, 2019 11:08
@JakeGinnivan
Copy link
Contributor Author

Yeah, this is a bug for sure. Will fix and add some tests.

@JakeGinnivan
Copy link
Contributor Author

Thanks for the repro @fabien0102 , we don't use that particular overload in our codebase so I missed it.

PR up.
#1632

BYK added a commit to getsentry/sentry that referenced this pull request Feb 10, 2020
This patch essentially backports emotion-js/emotion#1501 to dramatticaly
reduce the number of types generated during compilation. This reduces
the number of types from ~700k to 200k which translates to more than 50%
reduction in type check time and memory consumption.

Using the command `tsc --noEmit --extendedDiagnostics`

| Metric | Before | After |
|:------|--------:|------:|
| Files | 1189 |    1189 |
| Lines | 484378 |  484571 |
| Nodes | 892394 |  893030 |
| Identifiers | 296394 |  296638 |
| Symbols | 769880 |  719992 |
| Types | **705230** | **202553** |
| Memory used | **1024710K** | **747802K** |
| Assignability cache size | 576214 |  382608 |
| Identity cache size | 7697 | 6167 |
| Subtype cache size | 18326 | 18307 |
| I/O Read time | 0.36s | 0.30s |
| Parse time | 2.13s | 1.76s |
| Program time | 4.28s | 3.48s |
| Bind time | 1.25s | 0.94s |
| Check time | **40.85s** | **16.66s** |
| Total time | **46.38s** | **21.09s** |
BYK added a commit to getsentry/sentry that referenced this pull request Feb 11, 2020
* build(tsc): Refactor emotion types so we use less memory

This patch essentially backports emotion-js/emotion#1501 to dramatticaly
reduce the number of types generated during compilation. This reduces
the number of types from ~700k to 200k which translates to more than 50%
reduction in type check time and memory consumption.

Using the command `tsc --noEmit --extendedDiagnostics`

| Metric | Before | After |
|:------|--------:|------:|
| Files | 1189 |    1189 |
| Lines | 484378 |  484571 |
| Nodes | 892394 |  893030 |
| Identifiers | 296394 |  296638 |
| Symbols | 769880 |  719992 |
| Types | **705230** | **202553** |
| Memory used | **1024710K** | **747802K** |
| Assignability cache size | 576214 |  382608 |
| Identity cache size | 7697 | 6167 |
| Subtype cache size | 18326 | 18307 |
| I/O Read time | 0.36s | 0.30s |
| Parse time | 2.13s | 1.76s |
| Program time | 4.28s | 3.48s |
| Bind time | 1.25s | 0.94s |
| Check time | **40.85s** | **16.66s** |
| Total time | **46.38s** | **21.09s** |
@Kadrian
Copy link

Kadrian commented Mar 26, 2020

How to use CreateStyled with a theme now? It doesn't work like export styled as CreateStyled<Theme> anymore, because CreateStyled isn't generic anymore.

@Andarist
Copy link
Member

You don't have to use CreateStyled to provide Theme type, you can just augment the Theme interface as described here: https://deploy-preview-1600--emotion.netlify.com/docs/typescript#define-a-theme

@Kadrian
Copy link

Kadrian commented Mar 26, 2020

Aha! That's pretty cool. But that only works for me when wrapping html tags directy.

When I'm wrapping an existing component, the theme is any again, see: https://codesandbox.io/s/friendly-agnesi-ufbpb

Bildschirmfoto 2020-03-26 um 18 23 43

Should I open a new ticket? Great work with the library by the way!

@Andarist
Copy link
Member

Yes, you can open a new ticket. Please include a repro case in it.

louisgv pushed a commit to louisgv/emotion that referenced this pull request Sep 6, 2020
…ompilation (emotion-js#1501)

* Restructured types to greatly reduce number of types created during compilation

BREAKING CHANGE: There are a few breaking changes, see below

* withTheme now infers types properly and may require removing the manually specified generic parameter
* The Theme generic parameter has been removed from a number of types exported from `@emotion/styled` and `@emotion/styled-base`.
* Introduced new CreateThemedStyled type which is exported from emotion-theming, use type to create your own themed `styled` export. See updated docs
* WithTheme should be imported from emotion-theming
* CreateStyledComponentExtrinsic, CreateStyledComponentIntrinsic and CreateStyledComponentBase all have been replaced with CreateStyledComponent

* Fixing tests

* Fixed a bunch of tests and improved TypeScript docs

* Updated a bunch of the TypeScript docs

* Removed WithTheme type, not sure it's usage and there is no tests

* Few small cleanups around styled-base

* Fixed tests in a few more packages

* Fixed serialise tests after changes in emotion-js#1236

* Removed failing redundant test in sheet typescript tests.

It is failing with the same compilation error as the previous line, but formatting is causing the test failure

* Removed line with expected error, I am not sure the reason it should be failing.

* Need to bump the typescript version for styled-base for the union type test

* TypeScript tests passing

* Upgrade build image version to get newer version of yarn

* fix: styled component with static API

* Added changes in https://github.com/JakeGinnivan/emotion/pull/1/files to other functions with similar signatures

* fix: type issue where styled component passed in

* Add some additional tests around theming and fix them

* Restrict css function to css interpolation

* Fixed emotion-theming linting issue

* Reversed some incorrect type changes, withComponent has to include the previous components props otherwise styles on the original component may fail at runtime

* Fixed some accidently formatted package.json files

* Allowed theming of CreateStyled and StyledTags

* Restructured generic type params to make it explicit about what component props should transfer with withComponent and which shouldn't

Added tests

* Cleaned up some tests and added additional assertions

* Default the type of SpecificComponentProps in StyledComponent

* Reverted changes around ThemeProvider and added tests

* Added changeset

* Fixed ThemeProvider after revert

* Update tslint rules to fix error

* Fixed linting issues

* Fixed import path for css and clarified docs

* Added comment about fragment shorthand without babel being a typescript limitation

* Removed breaking change around some of the internal types

It's implementation detail user doesn't need to know

* Reverted changes around removing function interpolation from the  return types of function interpolation

* Renamed Omit to DistributiveOmit

To make it clear it's different to the inbuilt Omit

* Removed duplicate intersected type

* Renamed all usages of SFC to FC

* Fixed poor grammar

* Ignore lint rule rather than exporting type

* Updated generic constraints

* Reverted TypeScript version bump in create-emotion types

* Sync docs and test code

* Constrained Theme to extend {}

* Add tests for broken examples in emotion-js#1298

* Fix typo

* Add test which verifies emotion-js#1226 is fixed by type changes
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.

None yet

10 participants