Last active
January 18, 2024 16:04
-
-
Save guiseek/07d0e195b91d64869411e484161cfe50 to your computer and use it in GitHub Desktop.
JSX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare namespace JSX { | |
type ElementTagNameMap = HTMLElementTagNameMap & | |
SVGElementTagNameMap & | |
MathMLElementTagNameMap; | |
type Element<K extends keyof ElementTagNameMap = 'div'> = { | |
[A in keyof ElementTagNameMap[K]]: ElementTagNameMap[K][A]; | |
}; | |
type IntrinsicElements = { | |
[K in keyof ElementTagNameMap]: Partial<Element<K>>; | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function factory<K extends keyof JSX.ElementTagNameMap>( | |
tagOrFn: K | ReturnType<ObjectConstructor> | Function, | |
props: Partial<HTMLElement>, | |
...nodes: Node[] | |
) { | |
let component; | |
if (typeof tagOrFn === "string") { | |
component = document.createElement(tagOrFn); | |
} else { | |
try { | |
component = new tagOrFn(props); | |
} catch { | |
component = tagOrFn(props); | |
} | |
} | |
component.append( | |
...nodes.flatMap((node) => | |
typeof node === "string" ? new Text(node) : node | |
) | |
); | |
const attrs = Object.entries(props ?? {}); | |
for (const [attr, value] of attrs) { | |
if (component.hasOwnProperty(attr)) { | |
component[attr] = value; | |
} | |
} | |
return component as JSX.Element<K>; | |
} | |
export function fragmentFactory() { | |
return new DocumentFragment(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
{ | |
"compilerOptions": { | |
"target": "ES2020", | |
"useDefineForClassFields": true, | |
"module": "ESNext", | |
"lib": ["ES2020", "DOM", "DOM.Iterable"], | |
"skipLibCheck": true, | |
/* Bundler mode */ | |
"moduleResolution": "bundler", | |
"allowImportingTsExtensions": true, | |
"resolveJsonModule": true, | |
"isolatedModules": true, | |
"noEmit": true, | |
/* Linting */ | |
"strict": true, | |
"noUnusedLocals": true, | |
"noUnusedParameters": true, | |
"noFallthroughCasesInSwitch": true, | |
"jsx": "preserve", | |
"jsxFactory": "factory", | |
"jsxFragmentFactory": "fragmentFactory" | |
}, | |
"include": ["src", "types"] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { defineConfig } from "vite"; | |
export default defineConfig({ | |
esbuild: { | |
jsxInject: `import { factory, fragmentFactory } from "/src/lib/jsx"`, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment