@blueprint-chart/lib 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ import { RenderBackend } from './types';
2
+ export declare function createDomBackend(): RenderBackend;
@@ -0,0 +1,13 @@
1
+ import { DOMWindow } from 'jsdom';
2
+ export interface JsdomEnv {
3
+ window: DOMWindow;
4
+ container: HTMLElement;
5
+ serializeSvg: () => string;
6
+ serializeFrame: () => string | undefined;
7
+ cleanup: () => void;
8
+ }
9
+ export interface JsdomEnvOptions {
10
+ width: number;
11
+ height: number;
12
+ }
13
+ export declare function createJsdomEnv(opts: JsdomEnvOptions): JsdomEnv;
@@ -0,0 +1,5 @@
1
+ import { RenderBackend } from './types';
2
+ /** Browser build resolves the node backend to this throwing stub — the heavy
3
+ * native deps must never enter a browser bundle. Reached only if a browser
4
+ * caller forces the Node path. */
5
+ export declare function createNodeBackend(): RenderBackend;
@@ -0,0 +1,2 @@
1
+ import { RenderBackend } from './types';
2
+ export declare function createNodeBackend(): RenderBackend;
@@ -0,0 +1,5 @@
1
+ export interface RasterizeOptions {
2
+ width?: number;
3
+ height?: number;
4
+ }
5
+ export declare function rasterizeToPng(svg: string, opts?: RasterizeOptions): Promise<Buffer>;
@@ -0,0 +1,12 @@
1
+ import { DOMWindow } from 'jsdom';
2
+ /**
3
+ * Patch jsdom's SVG element prototypes so D3 layout primitives that rely on
4
+ * text measurement (axes, legends, annotations) return realistic widths.
5
+ *
6
+ * jsdom 26 does not expose `SVGTextContentElement` and does not subclass
7
+ * `<svg:text>` from `SVGGraphicsElement` — text elements are plain
8
+ * `SVGElement` instances. We therefore patch `SVGElement.prototype` so the
9
+ * methods exist on the actual prototype chain, while also patching the
10
+ * spec-correct prototypes if they happen to be available.
11
+ */
12
+ export declare function installTextShim(window: DOMWindow): void;
@@ -0,0 +1,30 @@
1
+ import { ChartDefinition } from '../types';
2
+ export interface BackendRenderOptions {
3
+ sceneIndex?: number;
4
+ thumbnail?: boolean;
5
+ theme?: string;
6
+ }
7
+ /**
8
+ * A render backend abstracts the DOM environment. The DOM backend uses the
9
+ * ambient document; the Node backend provisions jsdom. Both render via the
10
+ * lib's existing renderChart/renderBpc pipeline.
11
+ */
12
+ export interface RenderBackend {
13
+ readonly kind: 'dom' | 'node';
14
+ /** Create a render target. cleanup() detaches/closes it. */
15
+ createContainer(width: number, height: number): {
16
+ container: HTMLElement;
17
+ cleanup: () => void;
18
+ };
19
+ /** Render the resolved definition into the container (transition disabled). */
20
+ renderToContainer(container: HTMLElement, definition: ChartDefinition, opts: BackendRenderOptions): void;
21
+ /** Serialize the chart SVG markup from a rendered container. */
22
+ serializeSvg(container: HTMLElement): string;
23
+ /** Returns the `.bc-frame` outerHTML; falls back to the chart SVG markup when there is no frame (e.g. thumbnail mode). */
24
+ serializeFrame(container: HTMLElement): string;
25
+ /** Rasterize SVG markup to a PNG byte array. DOM backend throws PngBrowserUnsupportedError. */
26
+ rasterizePng(svg: string, opts: {
27
+ width?: number;
28
+ height?: number;
29
+ }): Promise<Uint8Array>;
30
+ }
@@ -0,0 +1,12 @@
1
+ /** Thrown when a BPC source cannot be parsed/resolved before any output is produced. */
2
+ export declare class ChartParseError extends Error {
3
+ constructor(message: string, options?: ErrorOptions);
4
+ }
5
+ /** Thrown when toPng() is called in a browser environment (PNG export is Node-only in v1). */
6
+ export declare class PngBrowserUnsupportedError extends Error {
7
+ constructor(message?: string);
8
+ }
9
+ /** Thrown when the Node render backend's optional dependencies are missing or failed to install. */
10
+ export declare class MissingNodeRenderDepsError extends Error {
11
+ constructor(message: string);
12
+ }
@@ -3,3 +3,6 @@ export { renderChart } from './render-chart';
3
3
  export { astToDefinition } from './ast-to-definition';
4
4
  export { resolveScene } from './resolve-scene';
5
5
  export type { ChartDefinition, RenderOptions, ResolvedChartState } from './types';
6
+ export { render } from './public-render';
7
+ export type { ChartHandle, RenderApiOptions, OutputOptions } from './public-render';
8
+ export { ChartParseError, PngBrowserUnsupportedError, MissingNodeRenderDepsError } from './errors';
@@ -0,0 +1,23 @@
1
+ import { RenderBackend } from './backends/types';
2
+ export interface RenderApiOptions {
3
+ theme?: string;
4
+ scene?: number;
5
+ width?: number;
6
+ height?: number;
7
+ frame?: boolean;
8
+ }
9
+ export interface OutputOptions {
10
+ width?: number;
11
+ height?: number;
12
+ }
13
+ export interface ChartHandle {
14
+ toSvg: (opts?: OutputOptions) => Promise<string>;
15
+ toHtml: (opts?: OutputOptions) => Promise<string>;
16
+ toPng: (opts?: OutputOptions) => Promise<Uint8Array>;
17
+ mount: (target: HTMLElement | string) => ChartHandle;
18
+ scene: (index: number) => ChartHandle;
19
+ }
20
+ /** Internal; exported for tests. Picks the DOM backend in a browser-like env,
21
+ * else dynamically loads the Node backend via the conditional subpath export. */
22
+ export declare function selectBackend(): Promise<RenderBackend>;
23
+ export declare function render(source: string, options?: RenderApiOptions): Promise<ChartHandle>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blueprint-chart/lib",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "DSL-driven charting library built on D3. Define charts as text, render them anywhere.",
5
5
  "keywords": [
6
6
  "chart",
@@ -32,6 +32,11 @@
32
32
  "import": "./dist/index.js",
33
33
  "default": "./dist/index.js"
34
34
  },
35
+ "./internal/node-backend": {
36
+ "node": "./dist/node-backend/index.js",
37
+ "browser": "./dist/node-backend/browser.js",
38
+ "default": "./dist/node-backend/browser.js"
39
+ },
35
40
  "./charts.scss": "./src/charts/chart.scss",
36
41
  "./src/charts/chart.scss": "./src/charts/chart.scss"
37
42
  },
@@ -56,18 +61,23 @@
56
61
  "devDependencies": {
57
62
  "@types/chroma-js": "^3.1.2",
58
63
  "@types/d3": "^7",
59
- "jsdom": "^26",
64
+ "@types/jsdom": "^21.1.7",
60
65
  "peggy": "^4",
61
66
  "typescript": "^5.7",
62
67
  "vite": "^6",
63
68
  "vite-plugin-dts": "^5.0.0",
64
69
  "vitest": "^3"
65
70
  },
71
+ "optionalDependencies": {
72
+ "@napi-rs/canvas": "^0.1.100",
73
+ "@resvg/resvg-js": "^2.6.2",
74
+ "jsdom": "^26.1.0"
75
+ },
66
76
  "scripts": {
67
77
  "generate:parser": "peggy --format es -o src/dsl/grammar.js src/dsl/grammar.peggy",
68
78
  "prebuild": "pnpm run generate:parser",
69
79
  "pretest": "pnpm run generate:parser",
70
- "build": "vite build && vite build -c vite.lib.config.ts",
80
+ "build": "vite build && vite build -c vite.lib.config.ts && vite build -c vite.node-backend.config.ts",
71
81
  "test": "vitest run"
72
82
  }
73
83
  }