@@ -101,32 +101,11 @@ function moveImportsToTop(root: postcss.Root) {
101101 atRule . remove ( ) ;
102102 } ) ;
103103
104- // Add imports at the beginning of the file with proper formatting
104+ // Add imports at the beginning of the file
105105 if ( imports . length > 0 ) {
106106 // Process imports in reverse order since we're prepending
107107 for ( let i = imports . length - 1 ; i >= 0 ; i -- ) {
108- const importRule = imports [ i ] ;
109-
110- // Set the raws for this specific import
111- if ( i === 0 ) {
112- // First import (will be at the top after prepend)
113- importRule . raws . before = '' ;
114- importRule . raws . after = ';' ; // Ensure semicolon
115- } else {
116- // Other imports
117- importRule . raws . before = '' ;
118- importRule . raws . after = ';\n' ; // Semicolon + newline
119- }
120-
121- root . prepend ( importRule ) ;
122- }
123-
124- // Add spacing after all imports by modifying the next node
125- if ( root . nodes . length > imports . length ) {
126- const firstNonImport = root . nodes [ imports . length ] ;
127- if ( firstNonImport ) {
128- firstNonImport . raws . before = '\n\n' ;
129- }
108+ root . prepend ( imports [ i ] ) ;
130109 }
131110 }
132111}
@@ -160,31 +139,48 @@ function consolidateAllLayers(root: postcss.Root) {
160139 }
161140 } ) ;
162141
163- // Create single consolidated layer with all content in correct order
142+ // Insert layer contents directly into root without wrapper
164143 if ( Object . keys ( layerContents ) . length > 0 ) {
165- const consolidatedLayer = postcss . atRule ( {
166- name : 'layer' ,
167- params : 'onchainkit' ,
144+ // Find where to insert content (after imports)
145+ let insertAfterNode : postcss . ChildNode | null = null ;
146+ root . each ( ( node ) => {
147+ if ( node . type === 'atrule' && node . name === 'import' ) {
148+ insertAfterNode = node ;
149+ }
168150 } ) ;
169151
170- // Add contents in the correct order with section comments
152+ // Build all nodes to insert
153+ const nodesToInsert : postcss . ChildNode [ ] = [ ] ;
154+
171155 layerOrder . forEach ( ( layerName ) => {
172156 if ( layerContents [ layerName ] ) {
173157 // Add section comment
174158 const comment = postcss . comment ( {
175159 text : ` ${ layerName . charAt ( 0 ) . toUpperCase ( ) + layerName . slice ( 1 ) } section ` ,
176160 } ) ;
177- consolidatedLayer . append ( comment ) ;
161+
162+ nodesToInsert . push ( comment ) ;
178163
179164 // Add all rules from this layer
180165 layerContents [ layerName ] . forEach ( ( node ) => {
181- consolidatedLayer . append ( node ) ;
166+ nodesToInsert . push ( node ) ;
182167 } ) ;
183168 }
184169 } ) ;
185170
186- // Insert the consolidated layer at the beginning of the file
187- root . prepend ( consolidatedLayer ) ;
171+ // Insert all nodes after imports (or at beginning if no imports)
172+ if ( insertAfterNode ) {
173+ nodesToInsert . reverse ( ) . forEach ( ( node ) => {
174+ insertAfterNode ! . after ( node ) ;
175+ } ) ;
176+ } else {
177+ nodesToInsert . reverse ( ) . forEach ( ( node ) => {
178+ root . prepend ( node ) ;
179+ } ) ;
180+ }
181+
182+ // Format the inserted content by cleaning up the root
183+ root . cleanRaws ( ) ;
188184 }
189185}
190186
0 commit comments