1- const stylesInDom = [ ] ;
1+ const stylesInDOM = [ ] ;
22
33function getIndexByIdentifier ( identifier ) {
44 let result = - 1 ;
55
6- for ( let i = 0 ; i < stylesInDom . length ; i ++ ) {
7- if ( stylesInDom [ i ] . identifier === identifier ) {
6+ for ( let i = 0 ; i < stylesInDOM . length ; i ++ ) {
7+ if ( stylesInDOM [ i ] . identifier === identifier ) {
88 result = i ;
99 break ;
1010 }
@@ -25,7 +25,7 @@ function modulesToDom(list, options) {
2525
2626 idCountMap [ id ] = count + 1 ;
2727
28- const index = getIndexByIdentifier ( identifier ) ;
28+ const indexByIdentifier = getIndexByIdentifier ( identifier ) ;
2929 const obj = {
3030 css : item [ 1 ] ,
3131 media : item [ 2 ] ,
@@ -34,13 +34,17 @@ function modulesToDom(list, options) {
3434 layer : item [ 5 ] ,
3535 } ;
3636
37- if ( index !== - 1 ) {
38- stylesInDom [ index ] . references ++ ;
39- stylesInDom [ index ] . updater ( obj ) ;
37+ if ( indexByIdentifier !== - 1 ) {
38+ stylesInDOM [ indexByIdentifier ] . references ++ ;
39+ stylesInDOM [ indexByIdentifier ] . updater ( obj ) ;
4040 } else {
41- stylesInDom . push ( {
41+ const updater = addElementStyle ( obj , options ) ;
42+
43+ options . byIndex = i ;
44+
45+ stylesInDOM . splice ( i , 0 , {
4246 identifier,
43- updater : addStyle ( obj , options ) ,
47+ updater,
4448 references : 1 ,
4549 } ) ;
4650 }
@@ -51,12 +55,12 @@ function modulesToDom(list, options) {
5155 return identifiers ;
5256}
5357
54- function addStyle ( obj , options ) {
58+ function addElementStyle ( obj , options ) {
5559 const api = options . domAPI ( options ) ;
5660
5761 api . update ( obj ) ;
5862
59- return function updateStyle ( newObj ) {
63+ const updater = ( newObj ) => {
6064 if ( newObj ) {
6165 if (
6266 newObj . css === obj . css &&
@@ -73,6 +77,8 @@ function addStyle(obj, options) {
7377 api . remove ( ) ;
7478 }
7579 } ;
80+
81+ return updater ;
7682}
7783
7884module . exports = ( list , options ) => {
@@ -89,7 +95,7 @@ module.exports = (list, options) => {
8995 const identifier = lastIdentifiers [ i ] ;
9096 const index = getIndexByIdentifier ( identifier ) ;
9197
92- stylesInDom [ index ] . references -- ;
98+ stylesInDOM [ index ] . references -- ;
9399 }
94100
95101 const newLastIdentifiers = modulesToDom ( newList , options ) ;
@@ -98,9 +104,9 @@ module.exports = (list, options) => {
98104 const identifier = lastIdentifiers [ i ] ;
99105 const index = getIndexByIdentifier ( identifier ) ;
100106
101- if ( stylesInDom [ index ] . references === 0 ) {
102- stylesInDom [ index ] . updater ( ) ;
103- stylesInDom . splice ( index , 1 ) ;
107+ if ( stylesInDOM [ index ] . references === 0 ) {
108+ stylesInDOM [ index ] . updater ( ) ;
109+ stylesInDOM . splice ( index , 1 ) ;
104110 }
105111 }
106112
0 commit comments