From ec36208919a443d4eb7f39ac30099213257d01f6 Mon Sep 17 00:00:00 2001 From: Jesse Wang Date: Tue, 22 Oct 2024 16:39:26 -0700 Subject: [PATCH] fix(rrweb): try/catch to postcss --- packages/rrweb-snapshot/src/rebuild.ts | 16 +++++++++++----- packages/rrweb-snapshot/test/rebuild.test.ts | 15 ++++++++++++++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/packages/rrweb-snapshot/src/rebuild.ts b/packages/rrweb-snapshot/src/rebuild.ts index 9b5e14ef65..5dc312ac20 100644 --- a/packages/rrweb-snapshot/src/rebuild.ts +++ b/packages/rrweb-snapshot/src/rebuild.ts @@ -62,11 +62,17 @@ export function adaptCssForReplay(cssText: string, cache: BuildCache): string { const cachedStyle = cache?.stylesWithHoverClass.get(cssText); if (cachedStyle) return cachedStyle; - const ast: { css: string } = postcss([ - mediaSelectorPlugin, - pseudoClassPlugin, - ]).process(cssText); - const result = ast.css; + let result = cssText; + try { + const ast: { css: string } = postcss([ + mediaSelectorPlugin, + pseudoClassPlugin, + ]).process(cssText); + result = ast.css; + } catch (error) { + console.warn('Failed to adapt css for replay', error); + } + cache?.stylesWithHoverClass.set(cssText, result); return result; } diff --git a/packages/rrweb-snapshot/test/rebuild.test.ts b/packages/rrweb-snapshot/test/rebuild.test.ts index 490b515f5b..d40a23e889 100644 --- a/packages/rrweb-snapshot/test/rebuild.test.ts +++ b/packages/rrweb-snapshot/test/rebuild.test.ts @@ -3,7 +3,7 @@ */ import * as fs from 'fs'; import * as path from 'path'; -import { beforeEach, describe, expect as _expect, it } from 'vitest'; +import { beforeEach, describe, expect as _expect, it, vi } from 'vitest'; import { adaptCssForReplay, buildNodeWithSN, @@ -248,4 +248,17 @@ ul li.specified c.\\:hover img { should_not_modify, ); }); + + it('handles exceptions from postcss when calling adaptCssForReplay', () => { + const consoleWarnSpy = vi + .spyOn(console, 'warn') + .mockImplementation(() => {}); + // trigger CssSyntaxError by passing invalid css + const cssText = 'a{'; + adaptCssForReplay(cssText, cache); + expect(consoleWarnSpy).toHaveBeenLastCalledWith( + 'Failed to adapt css for replay', + expect.any(Error), + ); + }); });