1
1
import { StyleSheet } from '@emotion/sheet'
2
- /* import { type EmotionCache, type SerializedStyles } from '@emotion/utils' */
2
+ import type { EmotionCache , SerializedStyles } from '@emotion/utils'
3
3
import {
4
4
serialize ,
5
5
compile ,
8
8
stringify ,
9
9
COMMENT
10
10
} from 'stylis'
11
+ import type { Element as StylisElement } from 'stylis'
11
12
import weakMemoize from '@emotion/weak-memoize'
12
13
import memoize from '@emotion/memoize'
13
14
import isDevelopment from '#is-development'
@@ -19,43 +20,37 @@ import {
19
20
incorrectImportAlarm
20
21
} from './stylis-plugins'
21
22
import { prefixer } from './prefixer'
22
- /* import type { StylisPlugin } from './types' */
23
+ import { StylisPlugin } from './types'
23
24
24
- /*
25
- export type Options = {
26
- nonce ?: string,
27
- stylisPlugins?: StylisPlugin[],
28
- key: string,
29
- container ?: HTMLElement,
30
- speedy?: boolean,
31
- prepend?: boolean,
25
+ export interface Options {
26
+ nonce ?: string
27
+ stylisPlugins ?: Array < StylisPlugin >
28
+ key : string
29
+ container ?: Node
30
+ speedy ?: boolean
31
+ /** @deprecate use `insertionPoint` instead */
32
+ prepend ?: boolean
32
33
insertionPoint ?: HTMLElement
33
34
}
34
- */
35
35
36
36
let getServerStylisCache = isBrowser
37
37
? undefined
38
- : weakMemoize ( ( ) =>
39
- memoize ( ( ) => {
40
- let cache = { }
41
- return name => cache [ name ]
42
- } )
43
- )
38
+ : weakMemoize ( ( ) => memoize < Record < string , string > > ( ( ) => ( { } ) ) )
44
39
45
40
const defaultStylisPlugins = [ prefixer ]
46
41
47
- let getSourceMap
42
+ let getSourceMap : ( ( styles : string ) => string | undefined ) | undefined
48
43
if ( isDevelopment ) {
49
44
let sourceMapPattern =
50
45
/ \/ \* # \s s o u r c e M a p p i n g U R L = d a t a : a p p l i c a t i o n \/ j s o n ; \S + \s + \* \/ / g
51
- getSourceMap = ( styles /*: string */ ) => {
46
+ getSourceMap = styles => {
52
47
let matches = styles . match ( sourceMapPattern )
53
48
if ( ! matches ) return
54
49
return matches [ matches . length - 1 ]
55
50
}
56
51
}
57
52
58
- let createCache = ( options /* : Options */ ) /* : EmotionCache */ => {
53
+ let createCache = ( options : Options ) : EmotionCache => {
59
54
let key = options . key
60
55
61
56
if ( isDevelopment && ! key ) {
@@ -74,14 +69,14 @@ let createCache = (options /*: Options */) /*: EmotionCache */ => {
74
69
// document.head is a safe place to move them to(though note document.head is not necessarily the last place they will be)
75
70
// note this very very intentionally targets all style elements regardless of the key to ensure
76
71
// that creating a cache works inside of render of a React component
77
- Array . prototype . forEach . call ( ssrStyles , ( node /* : HTMLStyleElement */ ) => {
72
+ Array . prototype . forEach . call ( ssrStyles , ( node : HTMLStyleElement ) => {
78
73
// we want to only move elements which have a space in the data-emotion attribute value
79
74
// because that indicates that it is an Emotion 11 server-side rendered style elements
80
75
// while we will already ignore Emotion 11 client-side inserted styles because of the :not([data-s]) part in the selector
81
76
// Emotion 10 client-side inserted styles did not have data-s (but importantly did not have a space in their data-emotion attributes)
82
77
// so checking for the space ensures that loading Emotion 11 after Emotion 10 has inserted some styles
83
78
// will not result in the Emotion 10 styles being destroyed
84
- const dataEmotionAttribute = node . getAttribute ( 'data-emotion' )
79
+ const dataEmotionAttribute = node . getAttribute ( 'data-emotion' ) !
85
80
if ( dataEmotionAttribute . indexOf ( ' ' ) === - 1 ) {
86
81
return
87
82
}
@@ -100,18 +95,18 @@ let createCache = (options /*: Options */) /*: EmotionCache */ => {
100
95
)
101
96
}
102
97
}
103
- let inserted = { }
104
- let container /* : Node */
105
- const nodesToHydrate = [ ]
98
+ let inserted : EmotionCache [ 'inserted' ] = { }
99
+ let container : Node
100
+ const nodesToHydrate : HTMLStyleElement [ ] = [ ]
106
101
if ( isBrowser ) {
107
102
container = options . container || document . head
108
103
109
104
Array . prototype . forEach . call (
110
105
// this means we will ignore elements which don't have a space in them which
111
106
// means that the style elements we're looking at are only Emotion 11 server-rendered style elements
112
107
document . querySelectorAll ( `style[data-emotion^="${ key } "]` ) ,
113
- ( node /* : HTMLStyleElement */ ) => {
114
- const attrib = node . getAttribute ( `data-emotion` ) . split ( ' ' )
108
+ ( node : HTMLStyleElement ) => {
109
+ const attrib = node . getAttribute ( `data-emotion` ) ! . split ( ' ' )
115
110
for ( let i = 1 ; i < attrib . length ; i ++ ) {
116
111
inserted [ attrib [ i ] ] = true
117
112
}
@@ -120,12 +115,12 @@ let createCache = (options /*: Options */) /*: EmotionCache */ => {
120
115
)
121
116
}
122
117
123
- let insert /* : (
118
+ let insert : (
124
119
selector : string ,
125
120
serialized : SerializedStyles ,
126
121
sheet : StyleSheet ,
127
122
shouldCache : boolean
128
- ) => string | void */
123
+ ) => string | void
129
124
const omnipresentPlugins = [ compat , removeLabel ]
130
125
131
126
if ( isDevelopment ) {
@@ -139,13 +134,13 @@ let createCache = (options /*: Options */) /*: EmotionCache */ => {
139
134
)
140
135
}
141
136
142
- if ( isBrowser ) {
143
- let currentSheet
137
+ if ( ! getServerStylisCache ) {
138
+ let currentSheet : Pick < StyleSheet , 'insert' >
144
139
145
140
const finalizingPlugins = [
146
141
stringify ,
147
142
isDevelopment
148
- ? element => {
143
+ ? ( element : StylisElement ) => {
149
144
if ( ! element . root ) {
150
145
if ( element . return ) {
151
146
currentSheet . insert ( element . return )
@@ -164,21 +159,16 @@ let createCache = (options /*: Options */) /*: EmotionCache */ => {
164
159
const serializer = middleware (
165
160
omnipresentPlugins . concat ( stylisPlugins , finalizingPlugins )
166
161
)
167
- const stylis = styles => serialize ( compile ( styles ) , serializer )
162
+ const stylis = ( styles : string ) => serialize ( compile ( styles ) , serializer )
168
163
169
- insert = (
170
- selector /*: string */ ,
171
- serialized /*: SerializedStyles */ ,
172
- sheet /*: StyleSheet */ ,
173
- shouldCache /*: boolean */
174
- ) /*: void */ => {
164
+ insert = ( selector , serialized , sheet , shouldCache ) => {
175
165
currentSheet = sheet
176
166
177
- if ( isDevelopment ) {
167
+ if ( getSourceMap ) {
178
168
let sourceMap = getSourceMap ( serialized . styles )
179
169
if ( sourceMap ) {
180
170
currentSheet = {
181
- insert : ( rule /*: string */ ) => {
171
+ insert : rule => {
182
172
sheet . insert ( rule + sourceMap )
183
173
}
184
174
}
@@ -196,13 +186,10 @@ let createCache = (options /*: Options */) /*: EmotionCache */ => {
196
186
const serializer = middleware (
197
187
omnipresentPlugins . concat ( stylisPlugins , finalizingPlugins )
198
188
)
199
- const stylis = styles => serialize ( compile ( styles ) , serializer )
189
+ const stylis = ( styles : string ) => serialize ( compile ( styles ) , serializer )
200
190
201
191
let serverStylisCache = getServerStylisCache ( stylisPlugins ) ( key )
202
- let getRules = (
203
- selector /*: string */ ,
204
- serialized /*: SerializedStyles */
205
- ) /*: string */ => {
192
+ let getRules = ( selector : string , serialized : SerializedStyles ) : string => {
206
193
let name = serialized . name
207
194
if ( serverStylisCache [ name ] === undefined ) {
208
195
serverStylisCache [ name ] = stylis (
@@ -211,12 +198,7 @@ let createCache = (options /*: Options */) /*: EmotionCache */ => {
211
198
}
212
199
return serverStylisCache [ name ]
213
200
}
214
- insert = (
215
- selector /*: string */ ,
216
- serialized /*: SerializedStyles */ ,
217
- sheet /*: StyleSheet */ ,
218
- shouldCache /*: boolean */
219
- ) /*: string | void */ => {
201
+ insert = ( selector , serialized , sheet , shouldCache ) => {
220
202
let name = serialized . name
221
203
let rules = getRules ( selector , serialized )
222
204
if ( cache . compat === undefined ) {
@@ -226,7 +208,7 @@ let createCache = (options /*: Options */) /*: EmotionCache */ => {
226
208
if ( shouldCache ) {
227
209
cache . inserted [ name ] = true
228
210
}
229
- if ( isDevelopment ) {
211
+ if ( getSourceMap ) {
230
212
let sourceMap = getSourceMap ( serialized . styles )
231
213
if ( sourceMap ) {
232
214
return rules + sourceMap
@@ -251,11 +233,11 @@ let createCache = (options /*: Options */) /*: EmotionCache */ => {
251
233
}
252
234
}
253
235
254
- const cache /* : EmotionCache */ = {
236
+ const cache : EmotionCache = {
255
237
key,
256
238
sheet : new StyleSheet ( {
257
239
key,
258
- container,
240
+ container : container ! ,
259
241
nonce : options . nonce ,
260
242
speedy : options . speedy ,
261
243
prepend : options . prepend ,
@@ -273,3 +255,5 @@ let createCache = (options /*: Options */) /*: EmotionCache */ => {
273
255
}
274
256
275
257
export default createCache
258
+ export type { EmotionCache }
259
+ export type { StylisElement , StylisPlugin , StylisPluginCallback } from './types'
0 commit comments