Skip to content

Commit 16d8a8c

Browse files
Migrate @emotion/utils to TypeScript (#2359)
* feat(ts-utils): Initial migration of utils * Small tweaks * More small tweaks Co-authored-by: Mateusz Burzyński <[email protected]>
1 parent 52aadc6 commit 16d8a8c

File tree

8 files changed

+47
-100
lines changed

8 files changed

+47
-100
lines changed

.changeset/old-years-breathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@emotion/utils': minor
3+
---
4+
5+
Source code has been migrated to TypeScript. From now on type declarations will be emitted based on that, instead of being hand-written.

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ coverage/
44
node_modules/
55
stylis.min.js
66
/demo/dist
7-
/packages/site/build
7+
/packages/site/build

packages/utils/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
"description": "internal utils for emotion",
55
"main": "dist/emotion-utils.cjs.js",
66
"module": "dist/emotion-utils.esm.js",
7+
"types": "dist/emotion-utils.cjs.d.ts",
78
"browser": {
89
"./dist/emotion-utils.cjs.js": "./dist/emotion-utils.browser.cjs.js",
910
"./dist/emotion-utils.esm.js": "./dist/emotion-utils.browser.esm.js"
1011
},
11-
"types": "types/index.d.ts",
1212
"license": "MIT",
1313
"scripts": {
1414
"test:typescript": "dtslint types"
@@ -19,8 +19,7 @@
1919
},
2020
"files": [
2121
"src",
22-
"dist",
23-
"types/*.d.ts"
22+
"dist"
2423
],
2524
"devDependencies": {
2625
"dtslint": "^0.3.0"

packages/utils/src/index.js renamed to packages/utils/src/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
/* import type { RegisteredCache, EmotionCache, SerializedStyles } from './types' */
1+
import { RegisteredCache, EmotionCache, SerializedStyles } from './types'
22

33
const isBrowser = typeof document !== 'undefined'
44

55
export function getRegisteredStyles(
6-
registered /*: RegisteredCache */,
7-
registeredStyles /*: string[] */,
8-
classNames /*: string */
9-
) {
6+
registered: RegisteredCache,
7+
registeredStyles: string[],
8+
classNames: string
9+
): string {
1010
let rawClassName = ''
1111

1212
classNames.split(' ').forEach(className => {
@@ -20,10 +20,10 @@ export function getRegisteredStyles(
2020
}
2121

2222
export const insertStyles = (
23-
cache /*: EmotionCache */,
24-
serialized /*: SerializedStyles */,
25-
isStringTag /*: boolean */
26-
) => {
23+
cache: EmotionCache,
24+
serialized: SerializedStyles,
25+
isStringTag: boolean
26+
): string | void => {
2727
let className = `${cache.key}-${serialized.name}`
2828
if (
2929
// we only need to add the styles to the registered cache if the
@@ -43,7 +43,7 @@ export const insertStyles = (
4343
}
4444
if (cache.inserted[serialized.name] === undefined) {
4545
let stylesForSSR = ''
46-
let current = serialized
46+
let current: SerializedStyles | undefined = serialized
4747
do {
4848
let maybeStyles = cache.insert(
4949
serialized === current ? `.${className}` : '',

packages/utils/src/types.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

packages/utils/src/types.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { StyleSheet } from '@emotion/sheet'
2+
3+
export { StyleSheet }
4+
5+
export type RegisteredCache = Record<string, string | undefined>
6+
7+
export type SerializedStyles = {
8+
name: string
9+
styles: string
10+
map?: string
11+
next?: SerializedStyles
12+
}
13+
14+
export type EmotionCache = {
15+
inserted: Record<string, string | true | undefined>
16+
registered: RegisteredCache
17+
sheet: StyleSheet
18+
key: string
19+
compat?: true
20+
nonce?: string
21+
insert: (
22+
selector: string,
23+
serialized: SerializedStyles,
24+
sheet: StyleSheet,
25+
shouldCache: boolean
26+
) => string | void
27+
}

packages/utils/types/index.d.ts

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,3 @@
1-
// Definitions by: Junyoung Clare Jang <https://github.com/Ailrun>
21
// TypeScript Version: 2.2
32

4-
export interface RegisteredCache {
5-
[key: string]: string
6-
}
7-
8-
export interface StyleSheet {
9-
container: HTMLElement
10-
nonce?: string
11-
key: string
12-
insert(rule: string): void
13-
flush(): void
14-
tags: Array<HTMLStyleElement>
15-
}
16-
17-
export interface EmotionCache {
18-
inserted: {
19-
[key: string]: string | true
20-
}
21-
registered: RegisteredCache
22-
sheet: StyleSheet
23-
key: string
24-
compat?: true
25-
nonce?: string
26-
insert(
27-
selector: string,
28-
serialized: SerializedStyles,
29-
sheet: StyleSheet,
30-
shouldCache: boolean
31-
): string | void
32-
}
33-
34-
export interface SerializedStyles {
35-
name: string
36-
styles: string
37-
map?: string
38-
next?: SerializedStyles
39-
}
40-
41-
export const isBrowser: boolean
42-
export function getRegisteredStyles(
43-
registered: RegisteredCache,
44-
registeredStyles: Array<string>,
45-
classNames: string
46-
): string
47-
export function insertStyles(
48-
cache: EmotionCache,
49-
serialized: SerializedStyles,
50-
isStringTag: boolean
51-
): string | void
3+
export * from '../src'

packages/utils/types/tests.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import {
22
EmotionCache,
33
RegisteredCache,
4-
SerializedStyles,
5-
StyleSheet,
64
getRegisteredStyles,
7-
insertStyles,
8-
isBrowser
5+
insertStyles
96
} from '@emotion/utils'
107

118
declare const testCache: EmotionCache
@@ -39,7 +36,3 @@ insertStyles(testCache, {
3936
name: 'abc',
4037
styles: 'font-size: 18px;'
4138
})
42-
43-
const test0: boolean = isBrowser
44-
// $ExpectError
45-
const test1: number = isBrowser

0 commit comments

Comments
 (0)