From d0bccfe37eaa1210701931f37175ecf784889e03 Mon Sep 17 00:00:00 2001 From: Ajay Ghanwat Date: Tue, 25 Aug 2026 15:48:48 +0530 Subject: [PATCH] fix(csr): suppress inactive theme token warnings --- bun.lock | 2 +- packages/csr/package.json | 2 +- packages/csr/src/reactive-runtime.ts | 17 +++++++++++++-- packages/csr/test/reactive.test.ts | 32 ++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/bun.lock b/bun.lock index 9e7e1e0f..e2f12a3b 100644 --- a/bun.lock +++ b/bun.lock @@ -342,7 +342,7 @@ }, "packages/csr": { "name": "@wrnexus/csr", - "version": "0.8.34", + "version": "0.8.35", "dependencies": { "@wrnexus/core": "workspace:*", }, diff --git a/packages/csr/package.json b/packages/csr/package.json index 95505b29..7edb733d 100644 --- a/packages/csr/package.json +++ b/packages/csr/package.json @@ -1,6 +1,6 @@ { "name": "@wrnexus/csr", - "version": "0.8.34", + "version": "0.8.35", "type": "module", "main": "src/index.ts", "exports": { diff --git a/packages/csr/src/reactive-runtime.ts b/packages/csr/src/reactive-runtime.ts index be91445b..78ced3eb 100644 --- a/packages/csr/src/reactive-runtime.ts +++ b/packages/csr/src/reactive-runtime.ts @@ -268,8 +268,21 @@ export const REACTIVE_RUNTIME = String.raw` Array.prototype.forEach.call(rules || [], function (rule) { var cssText = rule.cssText || ""; var match; - var pattern = /var\(\s*(--wrn-[A-Za-z0-9_-]+)/g; - while ((match = pattern.exec(cssText))) referenced[match[1]] = true; + // Only inspect declarations that can affect the current document. A + // component stylesheet may contain optional variants that are not + // rendered on this page, and variables with fallbacks are intentionally + // optional rather than missing theme tokens. + if (rule.selectorText) { + try { + if (!document.querySelector(rule.selectorText)) return; + } catch (_) { + // Keep inspecting selectors that the DOM implementation cannot parse. + } + } + var pattern = /var\(\s*(--wrn-[A-Za-z0-9_-]+)\s*(,)?/g; + while ((match = pattern.exec(cssText))) { + if (!match[2]) referenced[match[1]] = true; + } if (rule.style) { Array.prototype.forEach.call(rule.style, function (property) { if (String(property).indexOf("--wrn-") === 0) declared[property] = true; diff --git a/packages/csr/test/reactive.test.ts b/packages/csr/test/reactive.test.ts index 12212f3c..96ae44e8 100644 --- a/packages/csr/test/reactive.test.ts +++ b/packages/csr/test/reactive.test.ts @@ -1088,6 +1088,38 @@ test("development runtime accepts component-local and inline wrn variables", () expect(warnings).toHaveLength(0); }); +test("development runtime ignores fallback and inactive component variables", () => { + const win = new Window() as unknown as Window & Record; + win.document.head.innerHTML = + ``; + win.document.body.innerHTML = `
`; + (globalThis as Record).window = win; + (globalThis as Record).document = win.document; + (globalThis as Record).location = win.location; + (globalThis as Record).NodeFilter = ( + win as unknown as { NodeFilter: unknown } + ).NodeFilter; + (globalThis as Record).MutationObserver = ( + win as unknown as { MutationObserver: unknown } + ).MutationObserver; + (globalThis as Record).CustomEvent = ( + win as unknown as { CustomEvent: unknown } + ).CustomEvent; + const warnings: unknown[][] = []; + const originalWarn = console.warn; + console.warn = (...args: unknown[]) => warnings.push(args); + try { + (0, eval)(getReactiveRuntime(true)); + ( + win as unknown as { __wrnexusHydrateScopes?: (root: unknown) => void } + ).__wrnexusHydrateScopes?.(win.document); + } finally { + console.warn = originalWarn; + } + expect(warnings).toHaveLength(0); +}); + test("production runtime strips development diagnostics", () => { const production = getReactiveRuntime(); expect(production).not.toContain("WRN-DEV-");