Skip to content

Commit 27f01fe

Browse files
committed
Handle exceptions thrown from postcss when calling adaptCssForReplay
- Apply formatting changes - Remove import of postcss in test file
1 parent 5fbb904 commit 27f01fe

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

.changeset/angry-turtles-provide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"rrweb-snapshot": patch
3+
---
4+
5+
Handle exceptions thrown from postcss when calling adaptCssForReplay

packages/rrweb-snapshot/src/rebuild.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,17 @@ export function adaptCssForReplay(cssText: string, cache: BuildCache): string {
6464
const cachedStyle = cache?.stylesWithHoverClass.get(cssText);
6565
if (cachedStyle) return cachedStyle;
6666

67-
const ast: { css: string } = postcss([
68-
mediaSelectorPlugin,
69-
pseudoClassPlugin,
70-
]).process(cssText);
71-
const result = ast.css;
67+
let result = cssText;
68+
try {
69+
const ast: { css: string } = postcss([
70+
mediaSelectorPlugin,
71+
pseudoClassPlugin,
72+
]).process(cssText);
73+
result = ast.css;
74+
} catch (error) {
75+
console.warn('Failed to adapt css for replay', error);
76+
}
77+
7278
cache?.stylesWithHoverClass.set(cssText, result);
7379
return result;
7480
}

packages/rrweb-snapshot/test/rebuild.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
import * as fs from 'fs';
55
import * as path from 'path';
6-
import { beforeEach, describe, expect as _expect, it } from 'vitest';
6+
import { beforeEach, describe, expect as _expect, it, vi } from 'vitest';
77
import {
88
adaptCssForReplay,
99
buildNodeWithSN,
@@ -244,4 +244,17 @@ ul li.specified c.\\:hover img {
244244
should_not_modify,
245245
);
246246
});
247+
248+
it('handles exceptions from postcss when calling adaptCssForReplay', () => {
249+
const consoleWarnSpy = vi
250+
.spyOn(console, 'warn')
251+
.mockImplementation(() => {});
252+
// trigger CssSyntaxError by passing invalid css
253+
const cssText = 'a{';
254+
adaptCssForReplay(cssText, cache);
255+
expect(consoleWarnSpy).toHaveBeenLastCalledWith(
256+
'Failed to adapt css for replay',
257+
expect.any(Error),
258+
);
259+
});
247260
});

0 commit comments

Comments
 (0)