2023-02-03 14:17:24 +01:00
import type { HTMLElementConstructor , ManifestElement } from '../models' ;
2022-06-03 10:15:13 +02:00
import { hasDefaultExport } from './has-default-export.function' ;
2022-12-22 15:17:48 +01:00
import { isManifestElementNameType } from './is-manifest-element-name-type.function' ;
2022-06-01 14:35:02 +02:00
import { loadExtension } from './load-extension.function' ;
2023-01-05 13:53:12 +01:00
export async function createExtensionElement ( manifest : ManifestElement ) : Promise < HTMLElement | undefined > {
2023-02-03 14:17:24 +01:00
2022-07-01 11:38:44 +02:00
//TODO: Write tests for these extension options:
const js = await loadExtension ( manifest ) ;
2022-08-25 15:46:38 +02:00
2022-12-22 15:17:48 +01:00
if ( isManifestElementNameType ( manifest ) ) {
2022-07-01 11:38:44 +02:00
// created by manifest method providing HTMLElement
return document . createElement ( manifest . elementName ) ;
}
2022-08-25 15:46:38 +02:00
// TODO: Do we need this except for the default() loader?
2022-07-01 11:38:44 +02:00
if ( js ) {
2023-02-03 14:17:24 +01:00
if ( hasDefaultExport < HTMLElementConstructor > ( js ) ) {
2022-07-01 11:38:44 +02:00
// created by default class
return new js . default ( ) ;
}
2022-08-25 15:46:38 +02:00
2022-12-22 15:17:48 +01:00
console . error ( '-- Extension did not succeed creating an element, missing a default export of the served JavaScript file' , manifest ) ;
2022-12-13 21:53:56 +01:00
2022-08-25 15:46:38 +02:00
// If some JS was loaded and it did not at least contain a default export, then we are safe to assume that it executed as a module and does not need to be returned
return undefined ;
2022-07-01 11:38:44 +02:00
}
2022-08-25 15:46:38 +02:00
2022-12-22 15:17:48 +01:00
console . error ( '-- Extension did not succeed creating an element, missing a default export or `elementName` in the manifest.' , manifest ) ;
2022-08-25 15:46:38 +02:00
return undefined ;
2022-06-02 09:44:34 +02:00
}