Skip to content

Commit

Permalink
feat(dom): add attributes to load script
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepolischuk committed Sep 6, 2024
1 parent 98bd0e6 commit 7610156
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ export function loadStyleSheet(source: string): Promise<HTMLLinkElement> {
}

/** Load script */
export function loadScript(source: string): Promise<HTMLScriptElement> {
export function loadScript(
source: string,
attributes: Record<string, any> = {}
): Promise<HTMLScriptElement> {
globalThis[REGISTRY][source] ??= new Promise<HTMLScriptElement>(
(resolve, reject) => {
const script = document.createElement('script')
Expand All @@ -57,6 +60,12 @@ export function loadScript(source: string): Promise<HTMLScriptElement> {
script.async = true
script.type = 'text/javascript'

if (attributes) {
Object.entries(attributes).forEach(([name, value]) => {
script.setAttribute(name, value)
})
}

script.onload = () => {
resolve(script)
}
Expand Down

0 comments on commit 7610156

Please sign in to comment.