@hytts/hytts
Advanced tools
Comparing version 0.0.4 to 0.0.5
129
index.js
@@ -32,2 +32,6 @@ "use strict"; | ||
createEventHandler: () => createEventHandler, | ||
jsx: () => jsx, | ||
jsxDEV: () => jsx, | ||
jsxs: () => jsxs, | ||
jsxsDEV: () => jsxs, | ||
renderToString: () => renderToString, | ||
@@ -91,8 +95,9 @@ useContext: () => useContext, | ||
// source/jsx/jsx-types.ts | ||
var jsxExpressionSymbol = Symbol(); | ||
function toJsxExpression(action) { | ||
action.__isJsxExpression = null; | ||
action[jsxExpressionSymbol] = null; | ||
return action; | ||
} | ||
function isJsxExpression(value) { | ||
return !!value && typeof value === "function" && "__isJsxExpression" in value; | ||
return !!value && typeof value === "function" && jsxExpressionSymbol in value; | ||
} | ||
@@ -197,2 +202,4 @@ | ||
// source/jsx/browser-script.ts | ||
var browserScriptSymbol = Symbol(); | ||
var browserFuncSymbol = Symbol(); | ||
function BrowserScriptRenderer(props) { | ||
@@ -249,3 +256,3 @@ const registeredScripts = /* @__PURE__ */ new Set(); | ||
return { | ||
__isBrowserScript: null, | ||
[browserScriptSymbol]: null, | ||
hasContext: true, | ||
@@ -257,3 +264,3 @@ serializeScript: serializeScript(script, context) | ||
return { | ||
__isBrowserFunc: null, | ||
[browserFuncSymbol]: null, | ||
serializeScript: serializeScript(script, context) | ||
@@ -268,7 +275,17 @@ }; | ||
} | ||
function useRegisterBrowserEventHandler(id, eventName, handler) { | ||
useContext(scriptContext)({ | ||
[browserScriptSymbol]: null, | ||
hasContext: false, | ||
serializeScript: (registerFunction) => ({ | ||
script: `document.getElementById("${id}").${eventName}=${registerFunction(handler)}`, | ||
context: "" | ||
}) | ||
}); | ||
} | ||
function isBrowserScript(value) { | ||
return value !== null && typeof value === "object" && "__isBrowserScript" in value; | ||
return value !== null && typeof value === "object" && browserScriptSymbol in value; | ||
} | ||
function isBrowserFunc(value) { | ||
return value !== null && typeof value === "object" && "__isBrowserFunc" in value; | ||
return value !== null && typeof value === "object" && browserFuncSymbol in value; | ||
} | ||
@@ -331,2 +348,98 @@ function serializeScript(script, context) { | ||
} | ||
function jsxs(element, allProps) { | ||
const { children, ...props } = allProps; | ||
if (element === Fragment) { | ||
return toJsxExpression(() => renderChildren(children)); | ||
} | ||
if (typeof element === "string") { | ||
return voidElements.has(element) ? toJsxExpression(() => `<${element}${renderProps(props)}>`) : toJsxExpression( | ||
() => renderChildren(children, `<${element}${renderProps(props)}>`, `</${element}>`) | ||
); | ||
} | ||
if (typeof element === "function") { | ||
return toJsxExpression(() => { | ||
const jsxElement = element(allProps); | ||
if (jsxElement === null) | ||
return ""; | ||
if (typeof jsxElement === "function") | ||
return jsxElement(); | ||
return (async () => { | ||
const contentOrPromise = (await jsxElement)?.(); | ||
return !contentOrPromise ? "" : typeof contentOrPromise === "string" ? contentOrPromise : await contentOrPromise; | ||
})(); | ||
}); | ||
} | ||
throw new Error("Unexpected JSX element type."); | ||
} | ||
var jsx = jsxs; | ||
function renderProps(props) { | ||
let propsString = ""; | ||
let hasEventProps = false; | ||
const propEntries = Object.entries(props); | ||
for (const [name, value] of propEntries) { | ||
if (value === null || value === void 0) { | ||
continue; | ||
} | ||
if (name.startsWith(browserEventPrefix)) { | ||
hasEventProps = true; | ||
continue; | ||
} | ||
if (typeof value === "boolean" || typeof value === "number") | ||
propsString += ` ${name}="${value}"`; | ||
else if (typeof value === "string") | ||
propsString += ` ${name}="${escapeString(value, true)}"`; | ||
else { | ||
throw new Error(`Unsupported value for prop '${name}'.`); | ||
} | ||
} | ||
if (hasEventProps) { | ||
let id = props.id; | ||
if (!id) { | ||
id = useUniqueName(); | ||
propsString += ` id="${id}"`; | ||
} | ||
for (const [name, handler] of propEntries) { | ||
if (handler === null || handler === void 0) { | ||
continue; | ||
} | ||
if (name.startsWith(browserEventPrefix)) { | ||
if (isBrowserFunc(handler)) { | ||
useRegisterBrowserEventHandler( | ||
id, | ||
name.slice(browserEventPrefixLength), | ||
handler | ||
); | ||
} else { | ||
throw new Error(`Expected a browser function for event handler '${name}'.`); | ||
} | ||
} | ||
} | ||
} | ||
return propsString; | ||
} | ||
var voidElements = /* @__PURE__ */ new Set([ | ||
"area", | ||
"base", | ||
"basefont", | ||
"bgsound", | ||
"br", | ||
"col", | ||
"command", | ||
"embed", | ||
"frame", | ||
"hr", | ||
"image", | ||
"img", | ||
"input", | ||
"isindex", | ||
"keygen", | ||
"link", | ||
"menuitem", | ||
"meta", | ||
"nextid", | ||
"param", | ||
"source", | ||
"track", | ||
"wbr" | ||
]); | ||
var browserEventPrefix = "browser:"; | ||
@@ -345,2 +458,6 @@ var browserEventPrefixLength = browserEventPrefix.length; | ||
createEventHandler, | ||
jsx, | ||
jsxDEV, | ||
jsxs, | ||
jsxsDEV, | ||
renderToString, | ||
@@ -347,0 +464,0 @@ useContext, |
{ | ||
"name": "@hytts/hytts", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"author": "Axel Habermaier", | ||
@@ -12,4 +12,9 @@ "license": "MIT", | ||
"main": "index.js", | ||
"exports": { | ||
"./jsx-runtime": "./index.js", | ||
"./jsx-dev-runtime": "./index.js", | ||
".": "./index.js" | ||
}, | ||
"scripts": { | ||
"build": "tsup ./source/index.ts --clean --dts --format=cjs --target=node18 --env.NODE_ENV production && tsup ./source/jsx-dev-runtime.ts --dts --format=cjs --target=node18 --env.NODE_ENV production && tsup ./source/jsx-runtime.ts --dts --format=cjs --target=node18 --env.NODE_ENV production && cp ./package.json ./dist/package.json && cp ./README.md ./dist/README.md", | ||
"build": "tsup ./source/index.ts --clean --dts --format=cjs --target=node18 --env.NODE_ENV production && cp ./package.json ./dist/package.json && cp ./README.md ./dist/README.md && echo 'export { JSX } from \"./index\";' > ./dist/jsx-runtime.d.ts", | ||
"publish:local": "yarn build && cd ./dist && npm pack --pack-destination ../tmp", | ||
@@ -55,2 +60,2 @@ "format:check": "prettier --check ./", | ||
"sideEffects": false | ||
} | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1
90990
5
1909
1