keroosha.cybergulag.today/quartz/util/resources.tsx

40 lines
975 B
TypeScript
Raw Normal View History

2023-06-13 08:41:42 +03:00
import { randomUUID } from "crypto"
import { JSX } from "preact/jsx-runtime"
export type JSResource = {
2023-07-23 03:27:41 +03:00
loadTime: "beforeDOMReady" | "afterDOMReady"
moduleType?: "module"
2023-07-02 23:08:29 +03:00
spaPreserve?: boolean
2023-07-23 03:27:41 +03:00
} & (
| {
src: string
contentType: "external"
}
| {
script: string
contentType: "inline"
}
)
2023-06-13 08:41:42 +03:00
export function JSResourceToScriptElement(resource: JSResource, preserve?: boolean): JSX.Element {
2023-07-23 03:27:41 +03:00
const scriptType = resource.moduleType ?? "application/javascript"
2023-07-02 23:08:29 +03:00
const spaPreserve = preserve ?? resource.spaPreserve
2023-07-23 03:27:41 +03:00
if (resource.contentType === "external") {
return (
<script key={resource.src} src={resource.src} type={scriptType} spa-preserve={spaPreserve} />
)
2023-06-13 08:41:42 +03:00
} else {
const content = resource.script
2023-07-23 03:27:41 +03:00
return (
<script key={randomUUID()} type={scriptType} spa-preserve={spaPreserve}>
{content}
</script>
)
2023-06-13 08:41:42 +03:00
}
}
export interface StaticResources {
2023-07-23 03:27:41 +03:00
css: string[]
2023-06-13 08:41:42 +03:00
js: JSResource[]
}