Skip to content

Commit 3e116ce

Browse files
authored
fix: Handle exceptions thrown from postcss when calling adaptCssForReplay (#49)
1 parent 1b46581 commit 3e116ce

File tree

7 files changed

+257
-13
lines changed

7 files changed

+257
-13
lines changed

.husky/pre-commit

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
yarn run lint-staged

.husky/pre-push

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
protected_branch='main'
5+
current_branch=$(git branch --show-current)
6+
7+
if [ $protected_branch = $current_branch ]
8+
then
9+
echo "Pushing to $protected_branch is a sin! Instead, create a pull request and repent."
10+
exit 1 # push will not execute
11+
else
12+
exit 0 # push will execute
13+
fi

CHANGELOG.md

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
## 0.0.6 - 2025-02-01
22

3-
4-
53
## 0.0.5 - 2025-01-31
64

75
## -version - 2025-01-31

package.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
"eslint-plugin-jest": "^27.6.0",
3636
"eslint-plugin-tsdoc": "^0.2.17",
3737
"happy-dom": "^14.12.0",
38+
"husky": "^7.0.4",
39+
"lint-staged": "~15.4.3",
3840
"markdownlint": "^0.25.1",
3941
"markdownlint-cli": "^0.31.1",
4042
"prettier": "2.8.4",
@@ -56,7 +58,10 @@
5658
"live-stream": "cd packages/rrweb && yarn live-stream",
5759
"lint": "yarn run concurrently --success=all -r -m=1 'yarn run markdownlint docs' 'yarn eslint packages/*/src --ext .ts,.tsx,.js,.jsx,.svelte'",
5860
"lint:report": "yarn eslint --output-file eslint_report.json --format json packages/*/src --ext .ts,.tsx,.js,.jsx",
59-
"release": "yarn build:all && changeset publish"
61+
"release": "yarn build:all && changeset publish",
62+
"postinstall": "husky",
63+
"prepack": "pinst --disable",
64+
"postpack": "pinst --enable"
6065
},
6166
"resolutions": {
6267
"**/cssom": "https://registry.npmjs.org/rrweb-cssom/-/rrweb-cssom-0.6.0.tgz",
@@ -66,5 +71,8 @@
6671
"defaults",
6772
"not op_mini all"
6873
],
74+
"lint-staged": {
75+
"*.{ts, md}": "yarn run format"
76+
},
6977
"packageManager": "[email protected]"
7078
}

packages/rrweb-snapshot/src/rebuild.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,17 @@ export function adaptCssForReplay(cssText: string, cache: BuildCache): string {
6262
const cachedStyle = cache?.stylesWithHoverClass.get(cssText);
6363
if (cachedStyle) return cachedStyle;
6464

65-
const ast: { css: string } = postcss([
66-
mediaSelectorPlugin,
67-
pseudoClassPlugin,
68-
]).process(cssText);
69-
const result = ast.css;
65+
let result = cssText;
66+
try {
67+
const ast: { css: string } = postcss([
68+
mediaSelectorPlugin,
69+
pseudoClassPlugin,
70+
]).process(cssText);
71+
result = ast.css;
72+
} catch (error) {
73+
// on the replay side so should be ok to just log here
74+
console.warn('Failed to adapt css for replay', error);
75+
}
7076
cache?.stylesWithHoverClass.set(cssText, result);
7177
return result;
7278
}

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

+14-1
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,
@@ -270,4 +270,17 @@ ul li.specified c.\\:hover img {
270270
should_not_modify,
271271
);
272272
});
273+
274+
it('handles exceptions from postcss when calling adaptCssForReplay', () => {
275+
const consoleWarnSpy = vi
276+
.spyOn(console, 'warn')
277+
.mockImplementation(() => {});
278+
// trigger CssSyntaxError by passing invalid css
279+
const cssText = 'a{';
280+
adaptCssForReplay(cssText, cache);
281+
expect(consoleWarnSpy).toHaveBeenLastCalledWith(
282+
'Failed to adapt css for replay',
283+
expect.any(Error),
284+
);
285+
});
273286
});

0 commit comments

Comments
 (0)