From a5bb326f737bd607db36f69f9656ddf9c271e055 Mon Sep 17 00:00:00 2001 From: Justin Halsall Date: Wed, 20 May 2026 13:36:28 +0200 Subject: [PATCH] [codex] Add rrweb-snapshot utility boundaries (#1838) * docs: design record tree-shaking fix * fix: tree-shake postcss from record bundle * chore: add changeset for record tree-shaking * refactor: add rrweb-snapshot utility boundaries --- .../0001-record-source-boundary-resolver.md | 4 ++++ packages/rrweb-snapshot/src/index.ts | 2 ++ packages/rrweb-snapshot/src/rebuild-utils.ts | 14 ++++++++++++ packages/rrweb-snapshot/src/rebuild.ts | 2 +- packages/rrweb-snapshot/src/snapshot-utils.ts | 22 +++++++++++++++++++ packages/rrweb-snapshot/src/snapshot.ts | 2 +- packages/rrweb-snapshot/src/utils.ts | 12 ++++++++++ 7 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 packages/rrweb-snapshot/src/rebuild-utils.ts create mode 100644 packages/rrweb-snapshot/src/snapshot-utils.ts diff --git a/docs/adr/0001-record-source-boundary-resolver.md b/docs/adr/0001-record-source-boundary-resolver.md index 210778a6f0..eb9b261cec 100644 --- a/docs/adr/0001-record-source-boundary-resolver.md +++ b/docs/adr/0001-record-source-boundary-resolver.md @@ -1,3 +1,7 @@ # Use a record-local source-boundary resolver `@rrweb/record` needs to build without replay-only `rrweb-snapshot` code such as `postcss`, but adding public snapshot/rebuild subpath exports would expand the package API and create compatibility obligations. We will keep the public package API unchanged and add a resolver scoped to the `@rrweb/record` build that maps selected bare package imports to local source entrypoints, restoring Rollup's module visibility for tree-shaking. + +## Follow-up Refactor + +`rrweb-snapshot` now has internal snapshot-domain and rebuild-domain utility entrypoints. These entrypoints currently re-export from the legacy shared `utils.ts` module, preserving public API compatibility while making a future split of snapshot-only, rebuild-only, and shared helpers incremental. diff --git a/packages/rrweb-snapshot/src/index.ts b/packages/rrweb-snapshot/src/index.ts index 1908a0d9d9..0218c36bb7 100644 --- a/packages/rrweb-snapshot/src/index.ts +++ b/packages/rrweb-snapshot/src/index.ts @@ -18,6 +18,8 @@ import rebuild, { rebuildIntoSandboxedIframe, } from './rebuild'; export * from './types'; +// Legacy broad export kept for compatibility. New internal imports should +// prefer snapshot-utils.ts / rebuild-utils.ts domain entrypoints. export * from './utils'; export { diff --git a/packages/rrweb-snapshot/src/rebuild-utils.ts b/packages/rrweb-snapshot/src/rebuild-utils.ts new file mode 100644 index 0000000000..da4a0f6704 --- /dev/null +++ b/packages/rrweb-snapshot/src/rebuild-utils.ts @@ -0,0 +1,14 @@ +/** + * Rebuild-domain utility entrypoint. + * + * Intent: + * - Keep rebuild.ts imports explicit and isolated from snapshot-only helpers. + * - Serve as a stable seam for future extraction into rebuild-only modules. + * - Preserve current public API behavior by re-exporting from legacy utils.ts. + */ +export { + isElement, + Mirror, + isNodeMetaEqual, + extractFileExtension, +} from './utils'; diff --git a/packages/rrweb-snapshot/src/rebuild.ts b/packages/rrweb-snapshot/src/rebuild.ts index 0ee9b0f983..a6ef85f01b 100644 --- a/packages/rrweb-snapshot/src/rebuild.ts +++ b/packages/rrweb-snapshot/src/rebuild.ts @@ -12,7 +12,7 @@ import { Mirror, isNodeMetaEqual, extractFileExtension, -} from './utils'; +} from './rebuild-utils'; import postcss from 'postcss'; const tagMap: tagMap = { diff --git a/packages/rrweb-snapshot/src/snapshot-utils.ts b/packages/rrweb-snapshot/src/snapshot-utils.ts new file mode 100644 index 0000000000..33455e212a --- /dev/null +++ b/packages/rrweb-snapshot/src/snapshot-utils.ts @@ -0,0 +1,22 @@ +/** + * Snapshot-domain utility entrypoint. + * + * Intent: + * - Keep snapshot.ts imports explicit and decoupled from rebuild concerns. + * - Serve as a stable seam for future extraction into snapshot-only modules. + * - Preserve current public API behavior by re-exporting from legacy utils.ts. + */ +export { + Mirror, + is2DCanvasBlank, + isElement, + isShadowRoot, + maskInputValue, + isNativeShadowDom, + stringifyStylesheet, + getInputType, + toLowerCase, + extractFileExtension, + absolutifyURLs, + markCssSplits, +} from './utils'; diff --git a/packages/rrweb-snapshot/src/snapshot.ts b/packages/rrweb-snapshot/src/snapshot.ts index b5426a1751..3d32a88a71 100644 --- a/packages/rrweb-snapshot/src/snapshot.ts +++ b/packages/rrweb-snapshot/src/snapshot.ts @@ -30,7 +30,7 @@ import { extractFileExtension, absolutifyURLs, markCssSplits, -} from './utils'; +} from './snapshot-utils'; import dom from '@rrweb/utils'; let _id = 1; diff --git a/packages/rrweb-snapshot/src/utils.ts b/packages/rrweb-snapshot/src/utils.ts index 418ce8230a..260ba6e47a 100644 --- a/packages/rrweb-snapshot/src/utils.ts +++ b/packages/rrweb-snapshot/src/utils.ts @@ -1,3 +1,15 @@ +/** + * Legacy shared utility module. + * + * This file currently contains helpers used by both snapshot and rebuild paths + * and is also part of the public API surface, re-exported from index.ts. + * + * Migration intent: + * - snapshot.ts should consume snapshot-domain helpers via snapshot-utils.ts + * - rebuild.ts should consume rebuild-domain helpers via rebuild-utils.ts + * - when safe, split internals into snapshot-only / rebuild-only / shared + * modules while keeping this module as a compatibility shim for external users + */ import type { idNodeMap, MaskInputFn,