@@ -46,70 +46,51 @@ const getTarget = (function getTarget() {
46
46
} ;
47
47
} ) ( ) ;
48
48
49
- function listToStyles ( list , options ) {
50
- const styles = [ ] ;
51
- const newStyles = { } ;
49
+ function addModulesToDom ( id , list , options ) {
50
+ id = options . base ? id + options . base : id ;
51
+
52
+ if ( ! stylesInDom [ id ] ) {
53
+ stylesInDom [ id ] = [ ] ;
54
+ }
52
55
53
56
for ( let i = 0 ; i < list . length ; i ++ ) {
54
57
const item = list [ i ] ;
55
- const id = options . base ? item [ 0 ] + options . base : item [ 0 ] ;
56
- const css = item [ 1 ] ;
57
- const media = item [ 2 ] ;
58
- const sourceMap = item [ 3 ] ;
59
- const part = { css, media, sourceMap } ;
60
-
61
- if ( ! newStyles [ id ] ) {
62
- styles . push ( ( newStyles [ id ] = { id, parts : [ part ] } ) ) ;
58
+ const part = { css : item [ 1 ] , media : item [ 2 ] , sourceMap : item [ 3 ] } ;
59
+ const styleInDomById = stylesInDom [ id ] ;
60
+
61
+ if ( styleInDomById [ i ] ) {
62
+ styleInDomById [ i ] . updater ( part ) ;
63
63
} else {
64
- newStyles [ id ] . parts . push ( part ) ;
64
+ styleInDomById . push ( { updater : addStyle ( part , options ) } ) ;
65
65
}
66
66
}
67
67
68
- return styles ;
69
- }
70
-
71
- function addStylesToDom ( styles , options ) {
72
- for ( let i = 0 ; i < styles . length ; i ++ ) {
73
- const item = styles [ i ] ;
74
- const domStyle = stylesInDom [ item . id ] ;
75
- let j = 0 ;
76
-
77
- if ( domStyle ) {
78
- domStyle . refs ++ ;
79
-
80
- for ( ; j < domStyle . parts . length ; j ++ ) {
81
- domStyle . parts [ j ] ( item . parts [ j ] ) ;
82
- }
68
+ for ( let j = list . length ; j < stylesInDom [ id ] . length ; j ++ ) {
69
+ stylesInDom [ id ] [ j ] . updater ( ) ;
70
+ }
83
71
84
- for ( ; j < item . parts . length ; j ++ ) {
85
- domStyle . parts . push ( addStyle ( item . parts [ j ] , options ) ) ;
86
- }
87
- } else {
88
- const parts = [ ] ;
72
+ stylesInDom [ id ] . length = list . length ;
89
73
90
- for ( ; j < item . parts . length ; j ++ ) {
91
- parts . push ( addStyle ( item . parts [ j ] , options ) ) ;
92
- }
93
-
94
- stylesInDom [ item . id ] = { id : item . id , refs : 1 , parts } ;
95
- }
74
+ if ( stylesInDom [ id ] . length === 0 ) {
75
+ delete stylesInDom [ id ] ;
96
76
}
97
77
}
98
78
99
79
function insertStyleElement ( options ) {
100
80
const style = document . createElement ( 'style' ) ;
81
+ const attributes = options . attributes || { } ;
101
82
102
- if ( typeof options . attributes . nonce === 'undefined' ) {
83
+ if ( typeof attributes . nonce === 'undefined' ) {
103
84
const nonce =
104
85
typeof __webpack_nonce__ !== 'undefined' ? __webpack_nonce__ : null ;
105
86
106
87
if ( nonce ) {
107
- options . attributes . nonce = nonce ;
88
+ attributes . nonce = nonce ;
108
89
}
109
90
}
110
91
111
- Object . keys ( options . attributes ) . forEach ( ( key ) => {
112
- style . setAttribute ( key , options . attributes [ key ] ) ;
92
+ Object . keys ( attributes ) . forEach ( ( key ) => {
93
+ style . setAttribute ( key , attributes [ key ] ) ;
113
94
} ) ;
114
95
115
96
if ( typeof options . insert === 'function' ) {
@@ -179,6 +160,8 @@ function applyToTag(style, options, obj) {
179
160
180
161
if ( media ) {
181
162
style . setAttribute ( 'media' , media ) ;
163
+ } else {
164
+ style . removeAttribute ( 'media' ) ;
182
165
}
183
166
184
167
if ( sourceMap && btoa ) {
@@ -243,51 +226,18 @@ function addStyle(obj, options) {
243
226
} ;
244
227
}
245
228
246
- module . exports = ( list , options ) => {
229
+ module . exports = ( id , list , options ) => {
247
230
options = options || { } ;
248
231
249
- options . attributes =
250
- typeof options . attributes === 'object' ? options . attributes : { } ;
251
-
252
232
// Force single-tag solution on IE6-9, which has a hard limit on the # of <style>
253
233
// tags it will allow on a page
254
234
if ( ! options . singleton && typeof options . singleton !== 'boolean' ) {
255
235
options . singleton = isOldIE ( ) ;
256
236
}
257
237
258
- const styles = listToStyles ( list , options ) ;
259
-
260
- addStylesToDom ( styles , options ) ;
238
+ addModulesToDom ( id , list , options ) ;
261
239
262
240
return function update ( newList ) {
263
- const mayRemove = [ ] ;
264
-
265
- for ( let i = 0 ; i < styles . length ; i ++ ) {
266
- const item = styles [ i ] ;
267
- const domStyle = stylesInDom [ item . id ] ;
268
-
269
- if ( domStyle ) {
270
- domStyle . refs -- ;
271
- mayRemove . push ( domStyle ) ;
272
- }
273
- }
274
-
275
- if ( newList ) {
276
- const newStyles = listToStyles ( newList , options ) ;
277
-
278
- addStylesToDom ( newStyles , options ) ;
279
- }
280
-
281
- for ( let i = 0 ; i < mayRemove . length ; i ++ ) {
282
- const domStyle = mayRemove [ i ] ;
283
-
284
- if ( domStyle . refs === 0 ) {
285
- for ( let j = 0 ; j < domStyle . parts . length ; j ++ ) {
286
- domStyle . parts [ j ] ( ) ;
287
- }
288
-
289
- delete stylesInDom [ domStyle . id ] ;
290
- }
291
- }
241
+ addModulesToDom ( id , newList || [ ] , options ) ;
292
242
} ;
293
243
} ;
0 commit comments