@@ -172,6 +172,8 @@ const plugins = {
172172
173173// Global Cache for script promises to prevent race conditions and duplicate loads
174174const scriptCache = new Map ( ) ;
175+ // Cache the CSS marker once on load
176+ const cssMarker = typeof document !== 'undefined' ? document . querySelector ( 'link[plugins]' ) : null ;
175177
176178/**
177179 * Global Initialization Function
@@ -211,7 +213,29 @@ async function processPlugin(el) {
211213 if ( pluginDef . css ) pluginDef . css . forEach ( href => {
212214 if ( ! document . querySelector ( `link[href="${ href } "]` ) ) {
213215 const link = document . createElement ( "link" ) ;
214- link . rel = "stylesheet" ; link . href = href ; document . head . appendChild ( link ) ;
216+ link . rel = "stylesheet" ;
217+ link . href = href ;
218+
219+ // CSS INJECTION STRATEGY:
220+ // 1. Priority: Check for a specific marker element <link plugin>
221+ // (Cached globally in cssMarker)
222+
223+ if ( cssMarker ) {
224+ // Insert before the marker
225+ cssMarker . parentNode . insertBefore ( link , cssMarker ) ;
226+ } else {
227+ // 2. Fallback: Prepend before existing CSS
228+ // To allow custom CSS to easily override plugin defaults, we must ensure
229+ // plugin CSS loads BEFORE user CSS.
230+ const firstStyle = document . head . querySelector ( 'link[rel="stylesheet"], style' ) ;
231+
232+ if ( firstStyle ) {
233+ document . head . insertBefore ( link , firstStyle ) ;
234+ } else {
235+ // If no CSS exists yet, appending is safe
236+ document . head . appendChild ( link ) ;
237+ }
238+ }
215239 }
216240 } ) ;
217241
0 commit comments