From 1534dcf0d8fb6bb3d6ec1adb7a42d30a43a6a63c Mon Sep 17 00:00:00 2001 From: Yanzhen Yu Date: Wed, 1 Apr 2026 12:00:00 +0800 Subject: [PATCH] add typings autogen command --- .release-it.json | 2 +- index.d.ts | 21 ----------------- package.json | 5 ++-- typings/index.d.ts | 4 ++++ typings/rebuild.d.ts | 5 ++++ typings/snapshot.d.ts | 6 +++++ typings/types.d.ts | 55 +++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 74 insertions(+), 24 deletions(-) delete mode 100644 index.d.ts create mode 100644 typings/index.d.ts create mode 100644 typings/rebuild.d.ts create mode 100644 typings/snapshot.d.ts create mode 100644 typings/types.d.ts diff --git a/.release-it.json b/.release-it.json index 4aff1037..15b39fa3 100644 --- a/.release-it.json +++ b/.release-it.json @@ -1,4 +1,4 @@ { "non-interactive": true, - "buildCommand": "npm run bundle" + "buildCommand": "npm run bundle && npm run typings" } diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 27eec303..00000000 --- a/index.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { serializedNodeWithId, idNodeMap, INode } from './src/types'; -export * from './src/types'; - -export function snapshot(n: Document): [serializedNodeWithId | null, idNodeMap]; -export function rebuild( - n: serializedNodeWithId, - doc: Document, -): [Node | null, idNodeMap]; -export function serializeNodeWithId( - n: Node, - doc: Document, - map: idNodeMap, - skipChild?: boolean, -): serializedNodeWithId | null; -export function resetId(): void; -export function buildNodeWithSN( - n: serializedNodeWithId, - doc: Document, - map: idNodeMap, - skipChild?: boolean, -): INode | null; diff --git a/package.json b/package.json index e25bea56..5ca4698c 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "rrweb's component to take a snapshot of DOM, aka DOM serializer", "scripts": { "test": "cross-env TS_NODE_CACHE=false TS_NODE_FILES=true mocha -r ts-node/register test/**/*.ts", - "bundle": "rollup --config" + "bundle": "rollup --config", + "typings": "tsc -d --declarationDir typings" }, "repository": { "type": "git", @@ -18,7 +19,7 @@ "main": "lib/rrweb-snapshot.js", "module": "es/rrweb-snapshot.js", "unpkg": "dist/rrweb-snapshot.js", - "typings": "./index.d.ts", + "typings": "typings/index.d.ts", "files": [ "dist", "lib", diff --git a/typings/index.d.ts b/typings/index.d.ts new file mode 100644 index 00000000..60e49702 --- /dev/null +++ b/typings/index.d.ts @@ -0,0 +1,4 @@ +import snapshot, { serializeNodeWithId, resetId } from './snapshot'; +import rebuild, { buildNodeWithSN } from './rebuild'; +export * from './types'; +export { snapshot, serializeNodeWithId, resetId, rebuild, buildNodeWithSN }; diff --git a/typings/rebuild.d.ts b/typings/rebuild.d.ts new file mode 100644 index 00000000..99c76da5 --- /dev/null +++ b/typings/rebuild.d.ts @@ -0,0 +1,5 @@ +import { serializedNodeWithId, idNodeMap, INode } from './types'; +export declare function addHoverClass(cssText: string): string; +export declare function buildNodeWithSN(n: serializedNodeWithId, doc: Document, map: idNodeMap, skipChild?: boolean): INode | null; +declare function rebuild(n: serializedNodeWithId, doc: Document): [Node | null, idNodeMap]; +export default rebuild; diff --git a/typings/snapshot.d.ts b/typings/snapshot.d.ts new file mode 100644 index 00000000..090eac84 --- /dev/null +++ b/typings/snapshot.d.ts @@ -0,0 +1,6 @@ +import { serializedNodeWithId, idNodeMap } from './types'; +export declare function resetId(): void; +export declare function absoluteToStylesheet(cssText: string, href: string): string; +export declare function serializeNodeWithId(n: Node, doc: Document, map: idNodeMap, blockClass: string, skipChild?: boolean): serializedNodeWithId | null; +declare function snapshot(n: Document, blockClass?: string): [serializedNodeWithId | null, idNodeMap]; +export default snapshot; diff --git a/typings/types.d.ts b/typings/types.d.ts new file mode 100644 index 00000000..556e0bdf --- /dev/null +++ b/typings/types.d.ts @@ -0,0 +1,55 @@ +export declare enum NodeType { + Document = 0, + DocumentType = 1, + Element = 2, + Text = 3, + CDATA = 4, + Comment = 5 +} +export declare type documentNode = { + type: NodeType.Document; + childNodes: serializedNodeWithId[]; +}; +export declare type documentTypeNode = { + type: NodeType.DocumentType; + name: string; + publicId: string; + systemId: string; +}; +export declare type attributes = { + [key: string]: string | boolean; +}; +export declare type elementNode = { + type: NodeType.Element; + tagName: string; + attributes: attributes; + childNodes: serializedNodeWithId[]; + isSVG?: true; + needBlock?: boolean; +}; +export declare type textNode = { + type: NodeType.Text; + textContent: string; + isStyle?: true; +}; +export declare type cdataNode = { + type: NodeType.CDATA; + textContent: ''; +}; +export declare type commentNode = { + type: NodeType.Comment; + textContent: string; +}; +export declare type serializedNode = documentNode | documentTypeNode | elementNode | textNode | cdataNode | commentNode; +export declare type serializedNodeWithId = serializedNode & { + id: number; +}; +export declare type tagMap = { + [key: string]: string; +}; +export interface INode extends Node { + __sn: serializedNodeWithId; +} +export declare type idNodeMap = { + [key: number]: INode; +};