From 3f4e32f8da03035ab91b48e497195ae762f29ed0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20K=C3=BChne?= <18653821+ZerGo0@users.noreply.github.com> Date: Mon, 27 Oct 2025 22:06:01 +0100 Subject: [PATCH] fix: Optimize `splitShadowRootCss` (#1934) Co-authored-by: Aaron --- .../wxt/src/utils/split-shadow-root-css.ts | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/packages/wxt/src/utils/split-shadow-root-css.ts b/packages/wxt/src/utils/split-shadow-root-css.ts index dbd18754..a13b22f5 100644 --- a/packages/wxt/src/utils/split-shadow-root-css.ts +++ b/packages/wxt/src/utils/split-shadow-root-css.ts @@ -1,5 +1,7 @@ /** @module wxt/utils/split-shadow-root-css */ +const AT_RULE_BLOCKS = /(\s*@(property|font-face)[\s\S]*?{[\s\S]*?})/gm; + /** * Given a CSS string that will be loaded into a shadow root, split it into two parts: * - `documentCss`: CSS that needs to be applied to the document (like `@property`) @@ -10,18 +12,13 @@ export function splitShadowRootCss(css: string): { documentCss: string; shadowCss: string; } { - let shadowCss = css; - let documentCss = ''; - - const rulesRegex = /(\s*@(property|font-face)[\s\S]*?{[\s\S]*?})/gm; - let match; - while ((match = rulesRegex.exec(css)) !== null) { - documentCss += match[1]; - shadowCss = shadowCss.replace(match[1], ''); - } + const documentCss = Array.from(css.matchAll(AT_RULE_BLOCKS), (m) => m[0]) + .join('') + .trim(); + const shadowCss = css.replace(AT_RULE_BLOCKS, '').trim(); return { - documentCss: documentCss.trim(), - shadowCss: shadowCss.trim(), + documentCss: documentCss, + shadowCss: shadowCss, }; }