Comparing version 2.3.1 to 3.0.0
@@ -27,3 +27,3 @@ "use strict"; | ||
module.exports = __toCommonJS(jsx_elem_to_html_exports); | ||
var import_context_map = require("../context-map/context-map.js"); | ||
var import_component_api = require("../component-api/component-api.js"); | ||
var import_error_boundary = require("../error-boundary/error-boundary.js"); | ||
@@ -35,5 +35,5 @@ var import_pad = require("../utilities/pad.js"); | ||
var isTextNode = (e) => "type" in e && e.type === "textNode"; | ||
var jsxElemToHtmlSync = (element, contextMap = import_context_map.ContextMap.create(), options) => { | ||
var jsxElemToHtmlSync = (element, _componentApi, options) => { | ||
const { attributeMap = {}, currentIndent = 0, indent = 2 } = options ?? {}; | ||
contextMap = import_context_map.ContextMap.clone(contextMap); | ||
const componentApi = _componentApi ? import_component_api.ComponentApi.clone(_componentApi) : import_component_api.ComponentApi.create(options); | ||
if (!isSyncElem(element)) | ||
@@ -51,3 +51,3 @@ throw new Error(""); | ||
element.props, | ||
contextMap | ||
componentApi | ||
); | ||
@@ -59,3 +59,3 @@ if (subElem2 instanceof Promise) { | ||
} | ||
return jsxElemToHtmlSync(subElem2, contextMap, { | ||
return jsxElemToHtmlSync(subElem2, componentApi, { | ||
indent, | ||
@@ -66,3 +66,3 @@ currentIndent, | ||
} catch (e) { | ||
const fallbackElem = boundary.onError(e, element.props, contextMap); | ||
const fallbackElem = boundary.onError(e, element.props, componentApi); | ||
if (fallbackElem instanceof Promise) { | ||
@@ -73,3 +73,3 @@ throw new Error( | ||
} | ||
return jsxElemToHtmlSync(fallbackElem, contextMap, { | ||
return jsxElemToHtmlSync(fallbackElem, componentApi, { | ||
indent, | ||
@@ -83,3 +83,3 @@ currentIndent, | ||
element.props, | ||
contextMap | ||
componentApi | ||
); | ||
@@ -91,3 +91,3 @@ if (subElem instanceof Promise) { | ||
} | ||
return jsxElemToHtmlSync(subElem, contextMap, { | ||
return jsxElemToHtmlSync(subElem, componentApi, { | ||
indent, | ||
@@ -102,3 +102,3 @@ currentIndent, | ||
for (const child of htmlStruct.children) { | ||
const renderedChild = jsxElemToHtmlSync(child, contextMap, { | ||
const renderedChild = jsxElemToHtmlSync(child, componentApi, { | ||
indent, | ||
@@ -122,3 +122,3 @@ currentIndent: currentIndent + indent, | ||
for (const child of htmlStruct.children) { | ||
const renderedChild = jsxElemToHtmlSync(child, contextMap, { | ||
const renderedChild = jsxElemToHtmlSync(child, componentApi, { | ||
indent: inlineTag ? 0 : indent, | ||
@@ -138,5 +138,5 @@ currentIndent: inlineTag ? 0 : currentIndent + indent, | ||
}; | ||
var jsxElemToHtmlAsync = async (element, contextMap = import_context_map.ContextMap.create(), options) => { | ||
var jsxElemToHtmlAsync = async (element, _componentApi, options) => { | ||
const { attributeMap = {}, currentIndent = 0, indent = 2 } = options ?? {}; | ||
contextMap = import_context_map.ContextMap.clone(contextMap); | ||
const componentApi = _componentApi ? import_component_api.ComponentApi.clone(_componentApi) : import_component_api.ComponentApi.create(options); | ||
if (!isSyncElem(element)) | ||
@@ -154,5 +154,5 @@ throw new Error(""); | ||
element.props, | ||
contextMap | ||
componentApi | ||
); | ||
return await jsxElemToHtmlAsync(subElem2, contextMap, { | ||
return await jsxElemToHtmlAsync(subElem2, componentApi, { | ||
indent, | ||
@@ -166,5 +166,5 @@ currentIndent, | ||
element.props, | ||
contextMap | ||
componentApi | ||
); | ||
return await jsxElemToHtmlAsync(fallbackElem, contextMap, { | ||
return await jsxElemToHtmlAsync(fallbackElem, componentApi, { | ||
indent, | ||
@@ -178,5 +178,5 @@ currentIndent, | ||
element.props, | ||
contextMap | ||
componentApi | ||
); | ||
return await jsxElemToHtmlAsync(subElem, contextMap, { | ||
return await jsxElemToHtmlAsync(subElem, componentApi, { | ||
indent, | ||
@@ -191,3 +191,3 @@ currentIndent, | ||
for (const child of htmlStruct.children) { | ||
const renderedChild = await jsxElemToHtmlAsync(child, contextMap, { | ||
const renderedChild = await jsxElemToHtmlAsync(child, componentApi, { | ||
indent, | ||
@@ -211,3 +211,3 @@ currentIndent: currentIndent + indent, | ||
for (const child of htmlStruct.children) { | ||
const renderedChild = await jsxElemToHtmlAsync(child, contextMap, { | ||
const renderedChild = await jsxElemToHtmlAsync(child, componentApi, { | ||
indent: inlineTag ? 0 : indent, | ||
@@ -214,0 +214,0 @@ currentIndent: inlineTag ? 0 : currentIndent + indent, |
@@ -25,3 +25,3 @@ "use strict"; | ||
ErrorBoundary: () => import_error_boundary.ErrorBoundary, | ||
defineContext: () => import_context_map.defineContext, | ||
defineContext: () => import_component_api.defineContext, | ||
memo: () => import_memo.memo, | ||
@@ -38,5 +38,5 @@ renderToHtml: () => import_render_to_html.renderToHtml, | ||
var import_error_boundary = require("./error-boundary/error-boundary.js"); | ||
var import_context_map = require("./context-map/context-map.js"); | ||
var import_component_api = require("./component-api/component-api.js"); | ||
var import_render_to_html = require("./html-parser/render-to-html.js"); | ||
var import_render_to_string_template_tag = require("./string-template-parser/render-to-string-template-tag.js"); | ||
var import_memo = require("./utilities/memo.js"); |
@@ -26,3 +26,3 @@ "use strict"; | ||
module.exports = __toCommonJS(jsx_elem_to_strings_exports); | ||
var import_context_map = require("../context-map/context-map.js"); | ||
var import_component_api = require("../component-api/component-api.js"); | ||
var import_error_boundary = require("../error-boundary/error-boundary.js"); | ||
@@ -41,4 +41,4 @@ var import_map_attribute_name = require("./map-attribute-name.js"); | ||
}; | ||
var jsxElemToTagFuncArgsSync = (element, attributeMap, contextMap = import_context_map.ContextMap.create()) => { | ||
contextMap = import_context_map.ContextMap.clone(contextMap); | ||
var jsxElemToTagFuncArgsSync = (element, attributeMap, _componentApi = import_component_api.ComponentApi.create()) => { | ||
const componentApi = _componentApi ? import_component_api.ComponentApi.clone(_componentApi) : import_component_api.ComponentApi.create({ attributeMap }); | ||
if (!isSyncElem(element)) | ||
@@ -55,3 +55,3 @@ throw new Error(""); | ||
element.props, | ||
contextMap | ||
componentApi | ||
); | ||
@@ -63,3 +63,3 @@ if (subElem2 instanceof Promise) { | ||
} | ||
return jsxElemToTagFuncArgsSync(subElem2, attributeMap, contextMap); | ||
return jsxElemToTagFuncArgsSync(subElem2, attributeMap, componentApi); | ||
} catch (e) { | ||
@@ -69,3 +69,3 @@ const fallbackElem = boundary.onError( | ||
element.props, | ||
contextMap | ||
componentApi | ||
); | ||
@@ -77,3 +77,7 @@ if (fallbackElem instanceof Promise) { | ||
} | ||
return jsxElemToTagFuncArgsSync(fallbackElem, attributeMap, contextMap); | ||
return jsxElemToTagFuncArgsSync( | ||
fallbackElem, | ||
attributeMap, | ||
componentApi | ||
); | ||
} | ||
@@ -83,3 +87,3 @@ } | ||
element.props, | ||
contextMap | ||
componentApi | ||
); | ||
@@ -91,3 +95,3 @@ if (subElem instanceof Promise) { | ||
} | ||
return jsxElemToTagFuncArgsSync(subElem, attributeMap, contextMap); | ||
return jsxElemToTagFuncArgsSync(subElem, attributeMap, componentApi); | ||
} else { | ||
@@ -101,3 +105,3 @@ const { attributes, children } = (0, import_resolve_element.resolveElement)(element); | ||
attributeMap, | ||
contextMap | ||
componentApi | ||
); | ||
@@ -130,3 +134,3 @@ concatToLastStringOrPush(results, first); | ||
attributeMap, | ||
contextMap | ||
componentApi | ||
); | ||
@@ -133,0 +137,0 @@ concatToLastStringOrPush(results, first); |
@@ -30,3 +30,3 @@ "use strict"; | ||
var ReplaceMap = (props, context) => { | ||
context.replace(props.context); | ||
context.ctx.replace(props.context.ctx); | ||
return (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {}, props.children); | ||
@@ -33,0 +33,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
import type { ContextMap } from "../context-map/context-map"; | ||
import type { ComponentApi } from "../component-api/component-api"; | ||
export type ErrorBoundaryElement<P extends object = {}> = new (props: JSXTE.PropsWithChildren<P>) => Pick<ErrorBoundary<P>, keyof ErrorBoundary>; | ||
@@ -6,4 +6,4 @@ export declare abstract class ErrorBoundary<P extends object = {}> { | ||
constructor(props: JSXTE.PropsWithChildren<P>); | ||
abstract render(props: JSXTE.PropsWithChildren<P>, contextMap: ContextMap): JSX.Element | Promise<JSX.Element>; | ||
abstract onError(error: unknown, originalProps: JSXTE.PropsWithChildren<P>, contextMap: ContextMap): JSX.Element; | ||
abstract render(props: JSXTE.PropsWithChildren<P>, contextMap: ComponentApi): JSX.Element | Promise<JSX.Element>; | ||
abstract onError(error: unknown, originalProps: JSXTE.PropsWithChildren<P>, contextMap: ComponentApi): JSX.Element; | ||
} |
@@ -1,3 +0,3 @@ | ||
import { ContextMap } from "../context-map/context-map"; | ||
type InternalOptions = { | ||
import { ComponentApi } from "../component-api/component-api"; | ||
export type RendererInternalOptions = { | ||
indent?: number; | ||
@@ -7,4 +7,3 @@ currentIndent?: number; | ||
}; | ||
export declare const jsxElemToHtmlSync: (element: JSX.Element, contextMap?: ContextMap, options?: InternalOptions) => string; | ||
export declare const jsxElemToHtmlAsync: (element: JSX.Element, contextMap?: ContextMap, options?: InternalOptions) => Promise<string>; | ||
export {}; | ||
export declare const jsxElemToHtmlSync: (element: JSX.Element, _componentApi?: ComponentApi, options?: RendererInternalOptions) => string; | ||
export declare const jsxElemToHtmlAsync: (element: JSX.Element, _componentApi?: ComponentApi, options?: RendererInternalOptions) => Promise<string>; |
@@ -6,7 +6,7 @@ import "./utilities/array-flat-polyfill"; | ||
export { ErrorBoundary } from "./error-boundary/error-boundary"; | ||
export { defineContext } from "./context-map/context-map"; | ||
export { defineContext } from "./component-api/component-api"; | ||
export { renderToHtml, renderToHtmlAsync } from "./html-parser/render-to-html"; | ||
export { renderToStringTemplateTag } from "./string-template-parser/render-to-string-template-tag"; | ||
export { memo } from "./utilities/memo"; | ||
export type { ContextDefinition, ContextMap } from "./context-map/context-map"; | ||
export type { ContextDefinition, ComponentApi, } from "./component-api/component-api"; | ||
export type { AttributeBool, HTMLProps } from "./jsx/base-html-tag-props"; | ||
@@ -13,0 +13,0 @@ export type { StringTemplateParserOptions } from "./string-template-parser/render-to-string-template-tag"; |
@@ -1,2 +0,2 @@ | ||
import type { ContextMap } from "../context-map/context-map"; | ||
import type { ComponentApi } from "../component-api/component-api"; | ||
import type { ErrorBoundaryElement } from "../error-boundary/error-boundary"; | ||
@@ -19,3 +19,3 @@ import type { Rewrap } from "../html-parser/types"; | ||
type: "tag"; | ||
tag: string | ((props: ElementProps, contextMap: ContextMap) => Element) | ((props: ElementProps, contextMap: ContextMap) => Promise<Element>) | ErrorBoundaryElement; | ||
tag: string | ((props: ElementProps, contextMap: ComponentApi) => Element) | ((props: ElementProps, contextMap: ComponentApi) => Promise<Element>) | ErrorBoundaryElement; | ||
props: ElementProps; | ||
@@ -36,4 +36,4 @@ }; | ||
}; | ||
type Component<P extends object = {}> = (props: PropsWithChildren<P>, contextMap: ContextMap) => JSX.Element; | ||
type AsyncComponent<P extends object = {}> = (props: PropsWithChildren<P>, contextMap: ContextMap) => Promise<JSX.Element>; | ||
type Component<P extends object = {}> = (props: PropsWithChildren<P>, contextMap: ComponentApi) => JSX.Element; | ||
type AsyncComponent<P extends object = {}> = (props: PropsWithChildren<P>, contextMap: ComponentApi) => Promise<JSX.Element>; | ||
interface BaseHTMLTagProps { | ||
@@ -40,0 +40,0 @@ children?: ElementChildren; |
@@ -1,2 +0,2 @@ | ||
import type { ContextMap } from "../context-map/context-map"; | ||
import type { ComponentApi } from "../component-api/component-api"; | ||
type CreateElementProps = { | ||
@@ -6,9 +6,9 @@ [k: string]: any; | ||
}; | ||
export declare const createElement: (tag: string | ((props: any, contextMap: ContextMap) => JSX.Element) | ((props: any, contextMap: ContextMap) => Promise<JSX.Element>), props?: CreateElementProps, ...children: Array<JSX.Element | string | number | Array<JSX.Element | string | number>>) => JSX.Element; | ||
export declare const jsx: (tag: string | ((props: any, contextMap: ContextMap) => JSX.Element) | ((props: any, contextMap: ContextMap) => Promise<JSX.Element>), props?: CreateElementProps, ...children: Array<JSX.Element | string | number | Array<JSX.Element | string | number>>) => JSX.Element; | ||
export declare const jsxs: (tag: string | ((props: any, contextMap: ContextMap) => JSX.Element) | ((props: any, contextMap: ContextMap) => Promise<JSX.Element>), props?: CreateElementProps, ...children: Array<JSX.Element | string | number | Array<JSX.Element | string | number>>) => JSX.Element; | ||
export declare const _jsx: (tag: string | ((props: any, contextMap: ContextMap) => JSX.Element) | ((props: any, contextMap: ContextMap) => Promise<JSX.Element>), props?: CreateElementProps, ...children: Array<JSX.Element | string | number | Array<JSX.Element | string | number>>) => JSX.Element; | ||
export declare const _jsxs: (tag: string | ((props: any, contextMap: ContextMap) => JSX.Element) | ((props: any, contextMap: ContextMap) => Promise<JSX.Element>), props?: CreateElementProps, ...children: Array<JSX.Element | string | number | Array<JSX.Element | string | number>>) => JSX.Element; | ||
export declare const createElement: (tag: string | ((props: any, contextMap: ComponentApi) => JSX.Element) | ((props: any, contextMap: ComponentApi) => Promise<JSX.Element>), props?: CreateElementProps, ...children: Array<JSX.Element | string | number | Array<JSX.Element | string | number>>) => JSX.Element; | ||
export declare const jsx: (tag: string | ((props: any, contextMap: ComponentApi) => JSX.Element) | ((props: any, contextMap: ComponentApi) => Promise<JSX.Element>), props?: CreateElementProps, ...children: Array<JSX.Element | string | number | Array<JSX.Element | string | number>>) => JSX.Element; | ||
export declare const jsxs: (tag: string | ((props: any, contextMap: ComponentApi) => JSX.Element) | ((props: any, contextMap: ComponentApi) => Promise<JSX.Element>), props?: CreateElementProps, ...children: Array<JSX.Element | string | number | Array<JSX.Element | string | number>>) => JSX.Element; | ||
export declare const _jsx: (tag: string | ((props: any, contextMap: ComponentApi) => JSX.Element) | ((props: any, contextMap: ComponentApi) => Promise<JSX.Element>), props?: CreateElementProps, ...children: Array<JSX.Element | string | number | Array<JSX.Element | string | number>>) => JSX.Element; | ||
export declare const _jsxs: (tag: string | ((props: any, contextMap: ComponentApi) => JSX.Element) | ((props: any, contextMap: ComponentApi) => Promise<JSX.Element>), props?: CreateElementProps, ...children: Array<JSX.Element | string | number | Array<JSX.Element | string | number>>) => JSX.Element; | ||
export declare const Fragment = ""; | ||
export declare const _Fragment = ""; | ||
export {}; |
@@ -1,4 +0,4 @@ | ||
import { ContextMap } from "../context-map/context-map"; | ||
import { ComponentApi } from "../component-api/component-api"; | ||
type TagFunctionArgs = [string[], any[]]; | ||
export declare const jsxElemToTagFuncArgsSync: (element: JSX.Element, attributeMap: Record<string, string>, contextMap?: ContextMap) => TagFunctionArgs; | ||
export declare const jsxElemToTagFuncArgsSync: (element: JSX.Element, attributeMap: Record<string, string>, _componentApi?: ComponentApi) => TagFunctionArgs; | ||
export {}; |
@@ -1,2 +0,2 @@ | ||
import type { ContextMap } from "../context-map/context-map"; | ||
import type { ComponentApi } from "../component-api/component-api"; | ||
/** | ||
@@ -17,3 +17,3 @@ * Creates an in-memory cache for the provided component and | ||
children?: any; | ||
}>(Component: (props: P, context: ContextMap) => JSX.Element, options?: { | ||
}>(Component: (props: P, context: ComponentApi) => JSX.Element, options?: { | ||
/** Time in milliseconds. Default: 15 minutes. */ | ||
@@ -26,4 +26,7 @@ maxCacheAge?: number; | ||
renderAsynchronously?: boolean; | ||
/** Maximum number of cached entries to keep in memory. Default: 10. */ | ||
/** | ||
* Maximum number of cached entries to keep in memory. | ||
* Default: 10. | ||
*/ | ||
maxCacheEntries?: number; | ||
}) => ((props: P, context: ContextMap) => Promise<JSXTE.TextNodeElement>) | ((props: P, context: ContextMap) => JSXTE.TextNodeElement); | ||
}) => ((props: P, context: ComponentApi) => Promise<JSXTE.TextNodeElement>) | ((props: P, context: ComponentApi) => JSXTE.TextNodeElement); |
@@ -9,3 +9,3 @@ { | ||
"crypto": "^1.0.1", | ||
"esbuild": "^0.17.4", | ||
"esbuild": "^0.18.1", | ||
"eslint": "^8.15.0", | ||
@@ -18,9 +18,10 @@ "eslint-config-prettier": "^8.5.0", | ||
"node-os-walk": "^1.0.2", | ||
"pr-changelog-gen": "^1.1.3", | ||
"prettier": "^2.6.2", | ||
"prettier-plugin-jsdoc": "^0.4.2", | ||
"ts-jest": "^28.0.2", | ||
"typescript": "^4.9.4" | ||
"typescript": "^5.0.2" | ||
}, | ||
"name": "jsxte", | ||
"version": "2.3.1", | ||
"version": "3.0.0", | ||
"main": "./dist/legacy/index.js", | ||
@@ -27,0 +28,0 @@ "types": "./dist/types/index.d.ts", |
@@ -157,5 +157,5 @@ # JSX Template Engine | ||
const App: JSXTE.Component = (props, contextMap) => { | ||
const App: JSXTE.Component = (props, componentApi) => { | ||
// Set the context to a new value, all descendants of this component will have access to it | ||
contextMap.set(myContext, { label: "Hello" }); | ||
componentApi.ctx.set(myContext, { label: "Hello" }); | ||
@@ -165,9 +165,9 @@ return <Foo />; | ||
const Foo: JSXTE.Component = (props, contextMap) => { | ||
const Foo: JSXTE.Component = (props, componentApi) => { | ||
let label = ""; | ||
// Check if `myContext` is being provided by any of the ancestors | ||
if (contextMap.has(myContext)) { | ||
if (componentApi.ctx.has(myContext)) { | ||
// Retrieve the context data | ||
label = contextMap.get(myContext).label; | ||
label = componentApi.ctx.getOrFail(myContext).label; | ||
} | ||
@@ -189,13 +189,12 @@ | ||
value: T; | ||
}> = (props, contextMap) => { | ||
contextMap.set(ctx, props.value); | ||
}> = (props, componentApi) => { | ||
componentApi.ctx.set(ctx, props.value); | ||
return <>{props.children}</>; | ||
}; | ||
const Consumer = ( | ||
props: { render: (value?: T) => JSX.Element }, | ||
contextMap: ContextMap | ||
) => { | ||
if (contextMap.has(ctx)) { | ||
const value = contextMap.get(ctx); | ||
const Consumer: JSXTE.Component<{ | ||
render: (value?: T) => JSX.Element; | ||
}> = (props, componentApi) => { | ||
if (componentApi.ctx.has(ctx)) { | ||
const value = componentApi.ctx.getOrFail(ctx); | ||
return <>{props.render(value)}</>; | ||
@@ -243,3 +242,3 @@ } else { | ||
class Boundary extends ErrorBoundary { | ||
render(props: JSXTE.ElementProps, context: ContextMap) { | ||
render(props: JSXTE.ElementProps, componentApi: ComponentApi) { | ||
return <>{props.children}</>; | ||
@@ -251,3 +250,3 @@ } | ||
originalProps: JSXTE.ElementProps, | ||
context: ContextMap | ||
componentApi: ComponentApi | ||
) { | ||
@@ -254,0 +253,0 @@ return <h1>Something went wrong!</h1>; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
255218
302
6057
19
386