@@ -58,6 +58,16 @@ function stripLineComments(css: string): string {
5858 continue ;
5959 }
6060
61+ // Handle CSS block comments: skip /* ... */ entirely
62+ if ( code === SLASH && i + 1 < len && css . charCodeAt ( i + 1 ) === ASTERISK ) {
63+ i += 2 ;
64+ while ( i + 1 < len && ! ( css . charCodeAt ( i ) === ASTERISK && css . charCodeAt ( i + 1 ) === SLASH ) ) {
65+ i ++ ;
66+ }
67+ i += 2 ; // skip past */
68+ continue ;
69+ }
70+
6171 // Track url() context - check for 'url(' (case insensitive via | 32)
6272 if (
6373 code === OPEN_PAREN &&
@@ -79,6 +89,14 @@ function stripLineComments(css: string): string {
7989 continue ;
8090 }
8191
92+ // Strip orphaned */ (no matching /*) — invalid CSS that breaks parsing
93+ if ( code === ASTERISK && i + 1 < len && css . charCodeAt ( i + 1 ) === SLASH ) {
94+ if ( i > start ) parts . push ( css . substring ( start , i ) ) ;
95+ i += 2 ;
96+ start = i ;
97+ continue ;
98+ }
99+
82100 // Check for line comment (only when not in url())
83101 if ( code === SLASH && i + 1 < len && css . charCodeAt ( i + 1 ) === SLASH ) {
84102 if ( i > start ) parts . push ( css . substring ( start , i ) ) ;
@@ -283,7 +301,7 @@ export default function createStylisInstance(
283301) {
284302 let _componentId : string ;
285303 let _selector : string ;
286- let _selectorRegexp : RegExp ;
304+ let _selectorRegexp : RegExp | undefined ;
287305
288306 const selfReferenceReplacer = ( match : string , offset : number , string : string ) => {
289307 if (
@@ -315,6 +333,11 @@ export default function createStylisInstance(
315333 */
316334 const selfReferenceReplacementPlugin : stylis . Middleware = element => {
317335 if ( element . type === stylis . RULESET && element . value . includes ( '&' ) ) {
336+ // Lazy RegExp creation: only allocate when self-reference pattern is actually used
337+ if ( ! _selectorRegexp ) {
338+ _selectorRegexp = new RegExp ( `\\${ _selector } \\b` , 'g' ) ;
339+ }
340+
318341 ( element . props as string [ ] ) [ 0 ] = element . props [ 0 ]
319342 // catch any hanging references that stylis missed
320343 . replace ( AMP_REGEX , _selector )
@@ -335,6 +358,14 @@ export default function createStylisInstance(
335358
336359 middlewares . push ( stylis . stringify ) ;
337360
361+ // Pre-build the middleware chain once to avoid allocating closures,
362+ // arrays, and middleware wrappers on every stringifyRules call.
363+ // Safe because JS is single-threaded and _stack is consumed before next call.
364+ let _stack : string [ ] = [ ] ;
365+ const _middleware = stylis . middleware (
366+ middlewares . concat ( stylis . rulesheet ( value => _stack . push ( value ) ) )
367+ ) ;
368+
338369 const stringifyRules : Stringifier = (
339370 css : string ,
340371 selector = '' ,
@@ -349,7 +380,7 @@ export default function createStylisInstance(
349380 // these properties stay in sync with the current stylis run
350381 _componentId = componentId ;
351382 _selector = selector ;
352- _selectorRegexp = new RegExp ( `\\ ${ _selector } \\b` , 'g' ) ;
383+ _selectorRegexp = undefined ; // Reset for lazy creation per call
353384
354385 const flatCSS = sanitizeCSS ( stripLineComments ( css ) ) ;
355386 let compiled = stylis . compile (
@@ -360,14 +391,10 @@ export default function createStylisInstance(
360391 compiled = recursivelySetNamepace ( compiled , options . namespace ) ;
361392 }
362393
363- const stack : string [ ] = [ ] ;
364-
365- stylis . serialize (
366- compiled ,
367- stylis . middleware ( middlewares . concat ( stylis . rulesheet ( value => stack . push ( value ) ) ) )
368- ) ;
394+ _stack = [ ] ;
395+ stylis . serialize ( compiled , _middleware ) ;
369396
370- return stack ;
397+ return _stack ;
371398 } ;
372399
373400 stringifyRules . hash = plugins . length
0 commit comments