@jasonshimmy/custom-elements-runtime 0.0.1-beta.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.
- package/README.md +122 -0
- package/dist/build-tools.d.ts +71 -0
- package/dist/computed-state.d.ts +10 -0
- package/dist/custom-elements-runtime.cjs.js +46 -0
- package/dist/custom-elements-runtime.cjs.js.map +1 -0
- package/dist/custom-elements-runtime.es.js +1511 -0
- package/dist/custom-elements-runtime.es.js.map +1 -0
- package/dist/custom-elements-runtime.umd.js +46 -0
- package/dist/custom-elements-runtime.umd.js.map +1 -0
- package/dist/data-binding.d.ts +8 -0
- package/dist/dev-tools.d.ts +21 -0
- package/dist/event-bus.d.ts +101 -0
- package/dist/runtime.d.ts +109 -0
- package/dist/ssr.d.ts +49 -0
- package/dist/store.d.ts +10 -0
- package/dist/template-compiler.d.ts +117 -0
- package/dist/template-helpers.d.ts +56 -0
- package/dist/v-dom.d.ts +54 -0
- package/package.json +57 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal controlled input binding helper for data-model attributes.
|
|
3
|
+
* Handles checkboxes, radios, text, and modifiers (trim, number).
|
|
4
|
+
* @param el - Input/select/textarea element
|
|
5
|
+
* @param stateObj - State object to bind
|
|
6
|
+
* @param keyWithModifiers - Key and optional modifiers (e.g. 'name|trim|number')
|
|
7
|
+
*/
|
|
8
|
+
export declare function useDataModel<T extends Record<string, unknown>>(el: Element, stateObj: T, keyWithModifiers: string): void;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare const DevPerformance: {
|
|
2
|
+
measureRender<T>(name: string, fn: () => T): T;
|
|
3
|
+
getMemoryInfo(): any;
|
|
4
|
+
log(...args: unknown[]): void;
|
|
5
|
+
warn(...args: unknown[]): void;
|
|
6
|
+
error(...args: unknown[]): void;
|
|
7
|
+
debug(...args: unknown[]): void;
|
|
8
|
+
group(...args: unknown[]): void;
|
|
9
|
+
groupEnd(): void;
|
|
10
|
+
};
|
|
11
|
+
export declare const DevTools: {
|
|
12
|
+
startTiming(_componentName: string): () => number;
|
|
13
|
+
inspectComponent(element: HTMLElement): any;
|
|
14
|
+
trackStateChanges(element: HTMLElement, callback: (changes: any) => void): () => void;
|
|
15
|
+
log(...args: unknown[]): void;
|
|
16
|
+
warn(...args: unknown[]): void;
|
|
17
|
+
error(...args: unknown[]): void;
|
|
18
|
+
debug(...args: unknown[]): void;
|
|
19
|
+
group(...args: unknown[]): void;
|
|
20
|
+
groupEnd(): void;
|
|
21
|
+
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event handler type for global event bus
|
|
3
|
+
*/
|
|
4
|
+
export type EventHandler<T = any> = (data: T) => void;
|
|
5
|
+
/**
|
|
6
|
+
* GlobalEventBus provides a singleton event bus for cross-component communication.
|
|
7
|
+
* Uses Set for handler storage to optimize add/remove operations and prevent duplicates.
|
|
8
|
+
*/
|
|
9
|
+
export declare class GlobalEventBus extends EventTarget {
|
|
10
|
+
private handlers;
|
|
11
|
+
private static instance;
|
|
12
|
+
private eventCounters;
|
|
13
|
+
/**
|
|
14
|
+
* Returns the singleton instance of GlobalEventBus
|
|
15
|
+
*/
|
|
16
|
+
static getInstance(): GlobalEventBus;
|
|
17
|
+
/**
|
|
18
|
+
* Emit a global event with optional data. Includes event storm protection.
|
|
19
|
+
* @param eventName - Name of the event
|
|
20
|
+
* @param data - Optional event payload
|
|
21
|
+
*/
|
|
22
|
+
emit<T = any>(eventName: string, data?: T): void;
|
|
23
|
+
/**
|
|
24
|
+
* Register a handler for a global event. Returns an unsubscribe function.
|
|
25
|
+
* @param eventName - Name of the event
|
|
26
|
+
* @param handler - Handler function
|
|
27
|
+
*/
|
|
28
|
+
on<T = any>(eventName: string, handler: EventHandler<T>): () => void;
|
|
29
|
+
/**
|
|
30
|
+
* Remove a specific handler for a global event.
|
|
31
|
+
* @param eventName - Name of the event
|
|
32
|
+
* @param handler - Handler function to remove
|
|
33
|
+
*/
|
|
34
|
+
off<T = any>(eventName: string, handler: EventHandler<T>): void;
|
|
35
|
+
/**
|
|
36
|
+
* Remove all handlers for a specific event.
|
|
37
|
+
* @param eventName - Name of the event
|
|
38
|
+
*/
|
|
39
|
+
offAll(eventName: string): void;
|
|
40
|
+
/**
|
|
41
|
+
* Listen for a native CustomEvent. Returns an unsubscribe function.
|
|
42
|
+
* @param eventName - Name of the event
|
|
43
|
+
* @param handler - CustomEvent handler
|
|
44
|
+
* @param options - AddEventListener options
|
|
45
|
+
*/
|
|
46
|
+
listen<T = any>(eventName: string, handler: (event: CustomEvent<T>) => void, options?: AddEventListenerOptions): () => void;
|
|
47
|
+
/**
|
|
48
|
+
* Register a one-time event handler. Returns a promise that resolves with the event data.
|
|
49
|
+
* @param eventName - Name of the event
|
|
50
|
+
* @param handler - Handler function
|
|
51
|
+
*/
|
|
52
|
+
once<T = any>(eventName: string, handler: EventHandler<T>): Promise<T>;
|
|
53
|
+
/**
|
|
54
|
+
* Get a list of all active event names with registered handlers.
|
|
55
|
+
*/
|
|
56
|
+
getActiveEvents(): string[];
|
|
57
|
+
/**
|
|
58
|
+
* Clear all event handlers (useful for testing or cleanup).
|
|
59
|
+
*/
|
|
60
|
+
clear(): void;
|
|
61
|
+
/**
|
|
62
|
+
* Get the number of handlers registered for a specific event.
|
|
63
|
+
* @param eventName - Name of the event
|
|
64
|
+
*/
|
|
65
|
+
getHandlerCount(eventName: string): number;
|
|
66
|
+
/**
|
|
67
|
+
* Get event statistics for debugging.
|
|
68
|
+
*/
|
|
69
|
+
getEventStats(): Record<string, {
|
|
70
|
+
count: number;
|
|
71
|
+
handlersCount: number;
|
|
72
|
+
}>;
|
|
73
|
+
/**
|
|
74
|
+
* Reset event counters (useful for testing or after resolving issues).
|
|
75
|
+
*/
|
|
76
|
+
resetEventCounters(): void;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Singleton instance of the global event bus
|
|
80
|
+
*/
|
|
81
|
+
export declare const eventBus: GlobalEventBus;
|
|
82
|
+
/**
|
|
83
|
+
* Emit a global event
|
|
84
|
+
*/
|
|
85
|
+
export declare const emit: <T = any>(eventName: string, data?: T) => void;
|
|
86
|
+
/**
|
|
87
|
+
* Register a handler for a global event
|
|
88
|
+
*/
|
|
89
|
+
export declare const on: <T = any>(eventName: string, handler: EventHandler<T>) => () => void;
|
|
90
|
+
/**
|
|
91
|
+
* Remove a handler for a global event
|
|
92
|
+
*/
|
|
93
|
+
export declare const off: <T = any>(eventName: string, handler: EventHandler<T>) => void;
|
|
94
|
+
/**
|
|
95
|
+
* Register a one-time handler for a global event
|
|
96
|
+
*/
|
|
97
|
+
export declare const once: <T = any>(eventName: string, handler: EventHandler<T>) => Promise<T>;
|
|
98
|
+
/**
|
|
99
|
+
* Listen for a native CustomEvent
|
|
100
|
+
*/
|
|
101
|
+
export declare const listen: <T = any>(eventName: string, handler: (event: CustomEvent<T>) => void, options?: AddEventListenerOptions) => () => void;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents the state object for a component.
|
|
3
|
+
* Extend this interface for custom state typing.
|
|
4
|
+
*/
|
|
5
|
+
export interface ComponentState extends Record<string, unknown> {
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* API exposed to component templates and lifecycle handlers.
|
|
9
|
+
* Includes state, event emitters, and global event bus methods.
|
|
10
|
+
*/
|
|
11
|
+
export interface ComponentAPI<T extends ComponentState = ComponentState> {
|
|
12
|
+
/**
|
|
13
|
+
* Reactive state object. Mutate directly for reactivity.
|
|
14
|
+
*/
|
|
15
|
+
readonly state: T;
|
|
16
|
+
emit(eventName: string, detail?: unknown): void;
|
|
17
|
+
onGlobal<U = any>(eventName: string, handler: (data: U) => void): () => void;
|
|
18
|
+
offGlobal<U = any>(eventName: string, handler: (data: U) => void): void;
|
|
19
|
+
emitGlobal<U = any>(eventName: string, data?: U): void;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Configuration object for a custom element component.
|
|
23
|
+
* Defines template, state, computed properties, styles, refs, and lifecycle hooks.
|
|
24
|
+
* @template S - State type
|
|
25
|
+
* @template C - Computed type
|
|
26
|
+
*/
|
|
27
|
+
export interface ComponentConfig<S extends ComponentState, C extends Record<string, any> = {}> {
|
|
28
|
+
readonly template: (state: S & C, api: ComponentAPI<S & C>) => string | Promise<string> | CompiledTemplate<S & C>;
|
|
29
|
+
readonly state: S;
|
|
30
|
+
readonly computed?: {
|
|
31
|
+
[K in keyof C]: (state: S) => C[K];
|
|
32
|
+
};
|
|
33
|
+
readonly style?: string | ((state: S & C) => string);
|
|
34
|
+
readonly refs?: Record<string, RefHandler<S & C>>;
|
|
35
|
+
readonly onMounted?: LifecycleHandler<S & C>;
|
|
36
|
+
readonly onUnmounted?: LifecycleHandler<S & C>;
|
|
37
|
+
readonly debug?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Whitelist of state keys to reflect as attributes. If omitted, no keys are reflected.
|
|
40
|
+
*/
|
|
41
|
+
readonly reflect?: string[];
|
|
42
|
+
hydrate?: (el: Element | ShadowRoot, state: S & C, api: ComponentAPI<S & C>) => void;
|
|
43
|
+
[handler: string]: ((...args: unknown[]) => unknown) | unknown;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Handler for a ref element in the template.
|
|
47
|
+
* @param element - The DOM element with data-ref
|
|
48
|
+
* @param state - Current component state
|
|
49
|
+
* @param api - Component API
|
|
50
|
+
*/
|
|
51
|
+
export type RefHandler<T extends ComponentState> = (element: Element, state: T, api: ComponentAPI<T>) => void;
|
|
52
|
+
export type ComputedHandler<T extends ComponentState> = (state: T) => unknown;
|
|
53
|
+
/**
|
|
54
|
+
* Lifecycle handler for mounted/unmounted events.
|
|
55
|
+
* @param state - Current component state
|
|
56
|
+
* @param api - Component API
|
|
57
|
+
*/
|
|
58
|
+
export type LifecycleHandler<T extends ComponentState> = (state: T, api: ComponentAPI<T>) => void;
|
|
59
|
+
/**
|
|
60
|
+
* Represents a compiled template for fast rendering and hydration.
|
|
61
|
+
*/
|
|
62
|
+
export type CompiledTemplate<S extends ComponentState = ComponentState> = {
|
|
63
|
+
id: string;
|
|
64
|
+
render: (state: S, api: ComponentAPI<S>) => DocumentFragment;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Plugin interface for runtime hooks (init, render, error).
|
|
68
|
+
*/
|
|
69
|
+
export type RuntimePlugin<S extends ComponentState, C extends Record<string, any>> = {
|
|
70
|
+
onInit?: (config: ComponentConfig<S, C>) => void;
|
|
71
|
+
onRender?: (state: S & C, api: ComponentAPI<S & C>) => void;
|
|
72
|
+
onError?: (error: Error, state: S & C, api: ComponentAPI<S & C>) => void;
|
|
73
|
+
};
|
|
74
|
+
export declare const runtimePlugins: RuntimePlugin<ComponentState, Record<string, unknown>>[];
|
|
75
|
+
/**
|
|
76
|
+
* Registers a runtime plugin for hooks (init, render, error).
|
|
77
|
+
* @param plugin - RuntimePlugin instance
|
|
78
|
+
*/
|
|
79
|
+
export declare function useRuntimePlugin<S extends ComponentState, C extends Record<string, any>>(plugin: RuntimePlugin<S, C>): void;
|
|
80
|
+
export { Store } from './store';
|
|
81
|
+
export { eventBus } from './event-bus';
|
|
82
|
+
export { renderToString, renderComponentsToString, generateHydrationScript } from './ssr';
|
|
83
|
+
export type { SSRComponentConfig, SSRRenderOptions, SSRContext } from './ssr';
|
|
84
|
+
export { html, compile, css, classes, styles, ref, on } from './template-helpers';
|
|
85
|
+
export { useDataModel } from './data-binding';
|
|
86
|
+
export { compileTemplate, renderCompiledTemplate, updateCompiledTemplate } from './template-compiler';
|
|
87
|
+
export { mountVNode, patchVNode, createVNodeFromElement, parseVNodeFromHTML, safeReplaceChild, getVNodeKey } from './v-dom';
|
|
88
|
+
export type { VNode } from './v-dom';
|
|
89
|
+
/**
|
|
90
|
+
* Recursively sanitizes an object, removing dangerous keys and prototype pollution.
|
|
91
|
+
* Handles circular references using a WeakSet.
|
|
92
|
+
* @param obj - Object to sanitize
|
|
93
|
+
* @param seen - WeakSet to track visited objects
|
|
94
|
+
*/
|
|
95
|
+
export declare function deepSanitizeObject<T>(obj: T, seen?: WeakSet<object>): T;
|
|
96
|
+
/**
|
|
97
|
+
* Type guard to check if a value is Promise-like.
|
|
98
|
+
*/
|
|
99
|
+
export declare function isPromise(val: unknown): val is Promise<unknown>;
|
|
100
|
+
/**
|
|
101
|
+
* Registers a new custom element component.
|
|
102
|
+
* Validates config, sets up reactive state, and defines the custom element.
|
|
103
|
+
* Supports HMR and SSR hydration.
|
|
104
|
+
* @template S - State type
|
|
105
|
+
* @template C - Computed type
|
|
106
|
+
* @param tag - Custom element tag name
|
|
107
|
+
* @param config - Component configuration
|
|
108
|
+
*/
|
|
109
|
+
export declare function component<S extends ComponentState, C extends Record<string, any> = {}>(tag: string, config: ComponentConfig<S, C>): void;
|
package/dist/ssr.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { ComponentAPI, ComponentState } from './runtime';
|
|
2
|
+
export interface SSRComponentConfig<T extends ComponentState = ComponentState> {
|
|
3
|
+
readonly tag: string;
|
|
4
|
+
readonly template: (state: T, api: ComponentAPI<T>) => string;
|
|
5
|
+
/**
|
|
6
|
+
* State object can include computed properties as getter functions that accept state as a parameter.
|
|
7
|
+
*/
|
|
8
|
+
readonly state: T;
|
|
9
|
+
readonly style?: string | ((state: T) => string);
|
|
10
|
+
readonly attrs?: Record<string, string>;
|
|
11
|
+
}
|
|
12
|
+
export interface SSRRenderOptions {
|
|
13
|
+
/** Include component styles in the output */
|
|
14
|
+
includeStyles?: boolean;
|
|
15
|
+
/** Pretty print the HTML output */
|
|
16
|
+
prettyPrint?: boolean;
|
|
17
|
+
/** Custom attribute sanitization function */
|
|
18
|
+
sanitizeAttributes?: (attrs: Record<string, string>) => Record<string, string>;
|
|
19
|
+
}
|
|
20
|
+
export interface SSRContext {
|
|
21
|
+
/** Track rendered components for hydration */
|
|
22
|
+
components: Map<string, SSRComponentConfig>;
|
|
23
|
+
/** Global styles collected during SSR */
|
|
24
|
+
styles: Set<string>;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Create a minimal API for SSR that doesn't rely on DOM
|
|
28
|
+
*/
|
|
29
|
+
export declare function createSSRAPI<T extends ComponentState>(state: T): ComponentAPI<T>;
|
|
30
|
+
/**
|
|
31
|
+
* Render a component to HTML string on the server
|
|
32
|
+
* This function is treeshakable - only included when imported
|
|
33
|
+
*/
|
|
34
|
+
export declare function renderToString<T extends ComponentState>(config: SSRComponentConfig<T>, options?: SSRRenderOptions): string;
|
|
35
|
+
/**
|
|
36
|
+
* Render multiple components to HTML with shared context
|
|
37
|
+
*/
|
|
38
|
+
export declare function renderComponentsToString(components: SSRComponentConfig<any>[], options?: SSRRenderOptions): {
|
|
39
|
+
html: string;
|
|
40
|
+
styles: string;
|
|
41
|
+
context: SSRContext;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Generate hydration script for client-side takeover
|
|
45
|
+
*/
|
|
46
|
+
export declare function generateHydrationScript(context: SSRContext): string;
|
|
47
|
+
export declare const escapeHTML: (str: string) => string;
|
|
48
|
+
export declare const escapeAttribute: (str: string) => string;
|
|
49
|
+
export declare const formatHTML: (html: string) => string;
|
package/dist/store.d.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Template Compiler for Custom Elements Runtime
|
|
3
|
+
*
|
|
4
|
+
* Provides compile-time template optimization for better runtime performance.
|
|
5
|
+
* Features:
|
|
6
|
+
* - Static/dynamic separation
|
|
7
|
+
* - Efficient DOM updates
|
|
8
|
+
* - Treeshakable
|
|
9
|
+
* - TypeScript-friendly
|
|
10
|
+
* - Development-friendly fallbacks
|
|
11
|
+
*/
|
|
12
|
+
export interface CompiledTemplate<T = any> {
|
|
13
|
+
/** Static HTML parts that never change */
|
|
14
|
+
readonly statics: readonly string[];
|
|
15
|
+
/** Dynamic update functions for each interpolation */
|
|
16
|
+
readonly dynamics: readonly UpdateFunction<T>[];
|
|
17
|
+
/** Pre-compiled DOM fragment for initial render */
|
|
18
|
+
readonly fragment: DocumentFragment | null;
|
|
19
|
+
/** Unique template ID for caching */
|
|
20
|
+
readonly id: string;
|
|
21
|
+
/** Whether this template has dynamic content */
|
|
22
|
+
readonly hasDynamics: boolean;
|
|
23
|
+
/** Render method supporting async output */
|
|
24
|
+
render: (state: T, api: any) => string | Promise<string>;
|
|
25
|
+
}
|
|
26
|
+
export interface UpdateFunction<T = any> {
|
|
27
|
+
/** Target node path from root (e.g., [0, 1] means first child's second child) */
|
|
28
|
+
readonly path: readonly number[];
|
|
29
|
+
/** Type of update (text, attribute, property, etc.) */
|
|
30
|
+
readonly type: UpdateType;
|
|
31
|
+
/** Target property/attribute name (for non-text updates) */
|
|
32
|
+
readonly target?: string;
|
|
33
|
+
/** Function to extract value from state */
|
|
34
|
+
readonly getValue: (state: T, api: any) => unknown;
|
|
35
|
+
}
|
|
36
|
+
export type UpdateType = 'text' | 'attribute' | 'property' | 'event' | 'class' | 'style';
|
|
37
|
+
export interface TemplateCompilerOptions {
|
|
38
|
+
/** Enable development mode with better error messages */
|
|
39
|
+
development?: boolean;
|
|
40
|
+
/** Cache compiled templates */
|
|
41
|
+
cache?: boolean;
|
|
42
|
+
/** Enable static analysis optimizations */
|
|
43
|
+
optimize?: boolean;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Compile a template string into an optimized template object
|
|
47
|
+
* This is meant to be used at build time for best performance
|
|
48
|
+
*/
|
|
49
|
+
export declare function compileTemplate<T = any>(templateString: string, options?: TemplateCompilerOptions): CompiledTemplate<T>;
|
|
50
|
+
/**
|
|
51
|
+
* Tagged template literal for compile-time optimization
|
|
52
|
+
* Usage: compile`<div>${state.name}</div>`
|
|
53
|
+
*/
|
|
54
|
+
/**
|
|
55
|
+
* Find the DOM path to a placeholder in the template HTML
|
|
56
|
+
*/
|
|
57
|
+
export declare function findDOMPath(templateHTML: string, placeholder: string): number[];
|
|
58
|
+
export declare function compile<T = any>(strings: TemplateStringsArray, ...expressions: Array<(state: T, api: any) => unknown>): CompiledTemplate<T>;
|
|
59
|
+
export declare function parseAndCompileTemplate<T>(template: string, options: {
|
|
60
|
+
development: boolean;
|
|
61
|
+
optimize: boolean;
|
|
62
|
+
}): CompiledTemplate<T>;
|
|
63
|
+
export declare class TemplateAnalyzer {
|
|
64
|
+
private readonly template;
|
|
65
|
+
private readonly options;
|
|
66
|
+
private readonly dynamics;
|
|
67
|
+
private statics;
|
|
68
|
+
constructor(template: string, options: {
|
|
69
|
+
development: boolean;
|
|
70
|
+
optimize: boolean;
|
|
71
|
+
});
|
|
72
|
+
compile<T>(): CompiledTemplate<T>;
|
|
73
|
+
private parseTemplate;
|
|
74
|
+
private analyzeDynamicExpression;
|
|
75
|
+
private createValueGetter;
|
|
76
|
+
private createStaticFragment;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Render a compiled template efficiently
|
|
80
|
+
*/
|
|
81
|
+
export declare function renderCompiledTemplate<T>(compiled: CompiledTemplate<T>, state: T, api: any): DocumentFragment;
|
|
82
|
+
/**
|
|
83
|
+
* Update a rendered template with new state efficiently
|
|
84
|
+
*/
|
|
85
|
+
export declare function updateCompiledTemplate<T>(compiled: CompiledTemplate<T>, element: Element, newState: T, api: any, oldState?: T): void;
|
|
86
|
+
interface PerformanceMetrics {
|
|
87
|
+
compilationTime: number;
|
|
88
|
+
renderTime: number;
|
|
89
|
+
updateTime: number;
|
|
90
|
+
cacheHits: number;
|
|
91
|
+
cacheMisses: number;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Development helper to analyze template performance
|
|
95
|
+
*/
|
|
96
|
+
export declare function analyzeTemplate(template: string): {
|
|
97
|
+
staticParts: number;
|
|
98
|
+
dynamicParts: number;
|
|
99
|
+
complexity: 'low' | 'medium' | 'high';
|
|
100
|
+
recommendations: string[];
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Clear template cache for development/testing
|
|
104
|
+
*/
|
|
105
|
+
export declare function clearTemplateCache(): void;
|
|
106
|
+
/**
|
|
107
|
+
* Get performance metrics for development monitoring
|
|
108
|
+
*/
|
|
109
|
+
export declare function getPerformanceMetrics(): Map<string, PerformanceMetrics>;
|
|
110
|
+
/**
|
|
111
|
+
* Get cache statistics
|
|
112
|
+
*/
|
|
113
|
+
export declare function getCacheStats(): {
|
|
114
|
+
size: number;
|
|
115
|
+
entries: string[];
|
|
116
|
+
};
|
|
117
|
+
export {};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { CompiledTemplate } from './template-compiler.js';
|
|
2
|
+
/**
|
|
3
|
+
* TemplateResult type for template helpers
|
|
4
|
+
*/
|
|
5
|
+
export type TemplateResult = string | CompiledTemplate | ((state?: any) => string);
|
|
6
|
+
/**
|
|
7
|
+
* Tagged template literal for HTML strings.
|
|
8
|
+
* Returns a pure function for rendering with state and api.
|
|
9
|
+
* @param strings - Template strings
|
|
10
|
+
* @param values - Dynamic values (functions or primitives)
|
|
11
|
+
*/
|
|
12
|
+
export declare function html(strings: TemplateStringsArray, ...values: unknown[]): (state?: any, api?: any) => string | Promise<string>;
|
|
13
|
+
/**
|
|
14
|
+
* CompiledTemplateFn type for compiled templates
|
|
15
|
+
*/
|
|
16
|
+
export interface CompiledTemplateFn {
|
|
17
|
+
(state: any, api?: any): string;
|
|
18
|
+
id: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* compile helper: returns a compiled template function with a unique id property.
|
|
22
|
+
* Accepts tagged template literals and dynamic values (functions or primitives).
|
|
23
|
+
* @param strings - Template strings
|
|
24
|
+
* @param values - Dynamic values
|
|
25
|
+
*/
|
|
26
|
+
export declare function compile(strings: TemplateStringsArray, ...values: any[]): CompiledTemplateFn;
|
|
27
|
+
/**
|
|
28
|
+
* Tagged template literal for CSS strings.
|
|
29
|
+
* Returns a pure string for use in style blocks.
|
|
30
|
+
* @param strings - Template strings
|
|
31
|
+
* @param values - Dynamic values
|
|
32
|
+
*/
|
|
33
|
+
export declare function css(strings: TemplateStringsArray, ...values: unknown[]): string;
|
|
34
|
+
/**
|
|
35
|
+
* Create a ref function for element references.
|
|
36
|
+
* @param callback - Callback to run with the element
|
|
37
|
+
*/
|
|
38
|
+
export declare function ref<T extends Element = Element>(callback: (el: T) => void): (el: Element) => void;
|
|
39
|
+
/**
|
|
40
|
+
* Create event handlers with strong typing for use in templates.
|
|
41
|
+
* @param eventType - Event type
|
|
42
|
+
* @param handler - Handler function
|
|
43
|
+
*/
|
|
44
|
+
export declare function on<K extends keyof HTMLElementEventMap>(eventType: K, handler: (event: HTMLElementEventMap[K], state: any, api: any) => void): {
|
|
45
|
+
[key in K]: (event: HTMLElementEventMap[K], state: any, api: any) => void;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Helper for conditional classes. Returns a space-separated string of class names.
|
|
49
|
+
* @param obj - Object with class names as keys and boolean conditions as values
|
|
50
|
+
*/
|
|
51
|
+
export declare function classes(obj: Record<string, boolean>): string;
|
|
52
|
+
/**
|
|
53
|
+
* Helper for inline styles. Returns a CSS string for use in style attributes.
|
|
54
|
+
* @param obj - Object with style properties and values
|
|
55
|
+
*/
|
|
56
|
+
export declare function styles(obj: Record<string, string | number>): string;
|
package/dist/v-dom.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility: Generate a stable key for a VNode
|
|
3
|
+
*/
|
|
4
|
+
export declare function getVNodeKey(type: string, parentPath: string, childIndex: number, model?: string, value?: string): string;
|
|
5
|
+
/**
|
|
6
|
+
* Virtual Node (VNode) structure for incremental migration
|
|
7
|
+
*/
|
|
8
|
+
export interface VNode {
|
|
9
|
+
type: string;
|
|
10
|
+
key?: string;
|
|
11
|
+
props: Record<string, any>;
|
|
12
|
+
children: VNode[];
|
|
13
|
+
dom?: Element | Text;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Safely replaces a child node, guarding against NotFoundError and out-of-sync trees.
|
|
17
|
+
* Falls back to appendChild if oldChild is not present.
|
|
18
|
+
* Logs mutation for debugging if enabled.
|
|
19
|
+
* @param parent - Parent node
|
|
20
|
+
* @param newChild - New node to insert
|
|
21
|
+
* @param oldChild - Old node to replace
|
|
22
|
+
*/
|
|
23
|
+
export declare function safeReplaceChild(parent: Node | null, newChild: Node, oldChild: Node): void;
|
|
24
|
+
/**
|
|
25
|
+
* Mounts a VNode to the DOM and returns the created node.
|
|
26
|
+
* Handles text, fragment, and element nodes.
|
|
27
|
+
* @param vnode - Virtual node to mount
|
|
28
|
+
* @returns DOM node or null
|
|
29
|
+
*/
|
|
30
|
+
export declare function mountVNode(vnode: VNode): Element | Text | null;
|
|
31
|
+
/**
|
|
32
|
+
* Parses an HTML string into a VNode tree.
|
|
33
|
+
* Supports basic tags, attributes, text, and key/data-model.
|
|
34
|
+
* @param html - HTML string
|
|
35
|
+
* @returns VNode tree
|
|
36
|
+
*/
|
|
37
|
+
export declare function parseVNodeFromHTML(html: string): VNode;
|
|
38
|
+
/**
|
|
39
|
+
* Creates a VNode from a DOM ChildNode (Element or Text).
|
|
40
|
+
* Assigns a stable, deterministic key for VDOM reconciliation.
|
|
41
|
+
* @param node - DOM node
|
|
42
|
+
* @param parentPath - Path for key generation
|
|
43
|
+
* @param childIndex - Index for key generation
|
|
44
|
+
* @returns VNode
|
|
45
|
+
*/
|
|
46
|
+
export declare function createVNodeFromElement(node: ChildNode, parentPath?: string, childIndex?: number): VNode;
|
|
47
|
+
/**
|
|
48
|
+
* Patches two VNodes and updates the DOM, preserving controlled inputs.
|
|
49
|
+
* Handles keyed and index-based reconciliation.
|
|
50
|
+
* @param parent - Parent DOM element
|
|
51
|
+
* @param oldVNode - Previous VNode
|
|
52
|
+
* @param newVNode - New VNode
|
|
53
|
+
*/
|
|
54
|
+
export declare function patchVNode(parent: Element, oldVNode: VNode, newVNode: VNode): void;
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jasonshimmy/custom-elements-runtime",
|
|
3
|
+
"description": "A powerful, modern, and lightweight runtime for creating reactive web components with TypeScript",
|
|
4
|
+
"version": "0.0.1-beta.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"web-components",
|
|
8
|
+
"custom-elements",
|
|
9
|
+
"reactive",
|
|
10
|
+
"typescript",
|
|
11
|
+
"frontend",
|
|
12
|
+
"ui"
|
|
13
|
+
],
|
|
14
|
+
"author": "Jason Shimkoski <https://jasonshimmy.com>",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/jshimkoski/custom-elements.git"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/jshimkoski/custom-elements#readme",
|
|
21
|
+
"scripts": {
|
|
22
|
+
"dev": "vite",
|
|
23
|
+
"build": "tsc && vite build && tsc",
|
|
24
|
+
"preview": "vite preview",
|
|
25
|
+
"test": "vitest run",
|
|
26
|
+
"test:watch": "vitest",
|
|
27
|
+
"coverage": "vitest run --coverage",
|
|
28
|
+
"cy": "cypress run",
|
|
29
|
+
"cy:open": "cypress open"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@testing-library/jest-dom": "^6.6.4",
|
|
33
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
34
|
+
"cypress": "^14.5.4",
|
|
35
|
+
"jsdom": "^26.1.0",
|
|
36
|
+
"typescript": "~5.8.3",
|
|
37
|
+
"vite": "^7.0.4",
|
|
38
|
+
"vitest": "^3.2.4"
|
|
39
|
+
},
|
|
40
|
+
"main": "dist/custom-elements-runtime.cjs.js",
|
|
41
|
+
"module": "dist/custom-elements-runtime.es.js",
|
|
42
|
+
"types": "dist/runtime.d.ts",
|
|
43
|
+
"files": [
|
|
44
|
+
"dist",
|
|
45
|
+
"dist/runtime.d.ts"
|
|
46
|
+
],
|
|
47
|
+
"exports": {
|
|
48
|
+
".": {
|
|
49
|
+
"import": "./dist/custom-elements-runtime.es.js",
|
|
50
|
+
"require": "./dist/custom-elements-runtime.cjs.js",
|
|
51
|
+
"default": "./dist/custom-elements-runtime.umd.js"
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public"
|
|
56
|
+
}
|
|
57
|
+
}
|