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