@module-federation/sdk
Advanced tools
+35
-4
@@ -108,2 +108,4 @@ const require_utils = require('./utils.cjs'); | ||
| let needAttach = true; | ||
| let timeout = 2e4; | ||
| let timeoutId; | ||
| const links = document.getElementsByTagName("link"); | ||
@@ -124,15 +126,38 @@ for (let i = 0; i < links.length; i++) { | ||
| let createLinkRes = void 0; | ||
| let shouldApplyAttrs = true; | ||
| const attrs = info.attrs; | ||
| if (info.createLinkHook) { | ||
| createLinkRes = info.createLinkHook(info.url, attrs); | ||
| if (createLinkRes instanceof HTMLLinkElement) link = createLinkRes; | ||
| if (createLinkRes instanceof HTMLLinkElement) { | ||
| link = createLinkRes; | ||
| shouldApplyAttrs = false; | ||
| } else if (typeof createLinkRes === "object") { | ||
| if ("link" in createLinkRes && createLinkRes.link) { | ||
| link = createLinkRes.link; | ||
| shouldApplyAttrs = false; | ||
| } | ||
| if ("timeout" in createLinkRes && createLinkRes.timeout) timeout = createLinkRes.timeout; | ||
| } | ||
| } | ||
| if (attrs && !createLinkRes) Object.keys(attrs).forEach((name) => { | ||
| if (attrs && shouldApplyAttrs) Object.keys(attrs).forEach((name) => { | ||
| if (link && !link.getAttribute(name)) link.setAttribute(name, attrs[name]); | ||
| }); | ||
| } | ||
| if (!needAttach) { | ||
| Promise.resolve().then(() => { | ||
| info?.cb && info?.cb(); | ||
| }); | ||
| return { | ||
| link, | ||
| needAttach | ||
| }; | ||
| } | ||
| const onLinkComplete = (prev, event) => { | ||
| if (timeoutId) clearTimeout(timeoutId); | ||
| const onLinkCompleteCallback = () => { | ||
| if (event?.type === "error") info?.onErrorCallback && info?.onErrorCallback(event); | ||
| else info?.cb && info?.cb(); | ||
| if (event?.type === "error") { | ||
| const linkError = /* @__PURE__ */ new Error(event?.isTimeout ? `LinkNetworkError: Link "${info.url}" timed out.` : `LinkNetworkError: Failed to load link "${info.url}" - the URL is unreachable or the server returned an error.`); | ||
| linkError.name = "LinkNetworkError"; | ||
| info?.onErrorCallback && info?.onErrorCallback(linkError); | ||
| } else info?.cb && info?.cb(); | ||
| }; | ||
@@ -156,2 +181,8 @@ if (link) { | ||
| link.onload = onLinkComplete.bind(null, link.onload); | ||
| timeoutId = setTimeout(() => { | ||
| onLinkComplete(null, { | ||
| type: "error", | ||
| isTimeout: true | ||
| }); | ||
| }, timeout); | ||
| return { | ||
@@ -158,0 +189,0 @@ link, |
+1
-1
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"dom.cjs","names":["warn"],"sources":["../src/dom.ts"],"sourcesContent":["import type { CreateScriptHookDom, CreateScriptHookReturnDom } from './types';\nimport { warn } from './utils';\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport async function safeWrapper<T extends (...args: Array<any>) => any>(\n callback: T,\n disableWarn?: boolean,\n): Promise<ReturnType<T> | undefined> {\n try {\n const res = await callback();\n return res;\n } catch (e) {\n !disableWarn && warn(e);\n return;\n }\n}\n\nexport function isStaticResourcesEqual(url1: string, url2: string): boolean {\n const REG_EXP = /^(https?:)?\\/\\//i;\n // Transform url1 and url2 into relative paths\n const relativeUrl1 = url1.replace(REG_EXP, '').replace(/\\/$/, '');\n const relativeUrl2 = url2.replace(REG_EXP, '').replace(/\\/$/, '');\n // Check if the relative paths are identical\n return relativeUrl1 === relativeUrl2;\n}\n\nexport function createScript(info: {\n url: string;\n cb?: (value: void | PromiseLike<void>) => void;\n onErrorCallback?: (error: Error) => void;\n attrs?: Record<string, any>;\n needDeleteScript?: boolean;\n createScriptHook?: CreateScriptHookDom;\n}): { script: HTMLScriptElement; needAttach: boolean } {\n // Retrieve the existing script element by its src attribute\n let script: HTMLScriptElement | null = null;\n let needAttach = true;\n let timeout = 20000;\n let timeoutId: NodeJS.Timeout;\n const scripts = document.getElementsByTagName('script');\n\n for (let i = 0; i < scripts.length; i++) {\n const s = scripts[i];\n const scriptSrc = s.getAttribute('src');\n if (scriptSrc && isStaticResourcesEqual(scriptSrc, info.url)) {\n script = s;\n needAttach = false;\n break;\n }\n }\n\n if (!script) {\n const attrs = info.attrs;\n script = document.createElement('script');\n script.type = attrs?.['type'] === 'module' ? 'module' : 'text/javascript';\n let createScriptRes: CreateScriptHookReturnDom = undefined;\n if (info.createScriptHook) {\n createScriptRes = info.createScriptHook(info.url, info.attrs);\n\n if (createScriptRes instanceof HTMLScriptElement) {\n script = createScriptRes;\n } else if (typeof createScriptRes === 'object') {\n if ('script' in createScriptRes && createScriptRes.script) {\n script = createScriptRes.script;\n }\n if ('timeout' in createScriptRes && createScriptRes.timeout) {\n timeout = createScriptRes.timeout;\n }\n }\n }\n if (!script.src) {\n script.src = info.url;\n }\n if (attrs && !createScriptRes) {\n Object.keys(attrs).forEach((name) => {\n if (script) {\n if (name === 'async' || name === 'defer') {\n script[name] = attrs[name];\n // Attributes that do not exist are considered overridden\n } else if (!script.getAttribute(name)) {\n script.setAttribute(name, attrs[name]);\n }\n }\n });\n }\n }\n\n // Capture JS runtime errors thrown by this script's IIFE during execution.\n // The browser still fires onload even when the script throws, so without this\n // listener the execution error would silently become an uncaught global exception.\n let executionError: Error | null = null;\n const executionErrorHandler =\n typeof window !== 'undefined'\n ? (evt: ErrorEvent) => {\n if (evt.filename && isStaticResourcesEqual(evt.filename, info.url)) {\n const err = new Error(\n `ScriptExecutionError: Script \"${info.url}\" loaded but threw a runtime error during execution: ${evt.message} (${evt.filename}:${evt.lineno}:${evt.colno})`,\n );\n err.name = 'ScriptExecutionError';\n executionError = err;\n }\n }\n : null;\n if (executionErrorHandler) {\n window.addEventListener('error', executionErrorHandler);\n }\n\n const onScriptComplete = async (\n prev: OnErrorEventHandler | GlobalEventHandlers['onload'] | null,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n event: any,\n ): Promise<void> => {\n clearTimeout(timeoutId);\n if (executionErrorHandler) {\n window.removeEventListener('error', executionErrorHandler);\n }\n const onScriptCompleteCallback = () => {\n if (event?.type === 'error') {\n const networkError = new Error(\n event?.isTimeout\n ? `ScriptNetworkError: Script \"${info.url}\" timed out.`\n : `ScriptNetworkError: Failed to load script \"${info.url}\" - the script URL is unreachable or the server returned an error (network failure, 404, CORS, etc.)`,\n );\n networkError.name = 'ScriptNetworkError';\n info?.onErrorCallback && info?.onErrorCallback(networkError);\n } else if (executionError) {\n // Script fetched OK (onload fired) but IIFE threw during execution.\n info?.onErrorCallback && info?.onErrorCallback(executionError);\n } else {\n info?.cb && info?.cb();\n }\n };\n\n // Prevent memory leaks in IE.\n if (script) {\n script.onerror = null;\n script.onload = null;\n safeWrapper(() => {\n const { needDeleteScript = true } = info;\n if (needDeleteScript) {\n script?.parentNode && script.parentNode.removeChild(script);\n }\n });\n if (prev && typeof prev === 'function') {\n const result = (prev as any)(event);\n if (result instanceof Promise) {\n const res = await result;\n onScriptCompleteCallback();\n return res;\n }\n onScriptCompleteCallback();\n return result;\n }\n }\n onScriptCompleteCallback();\n };\n\n script.onerror = onScriptComplete.bind(null, script.onerror);\n script.onload = onScriptComplete.bind(null, script.onload);\n\n timeoutId = setTimeout(() => {\n onScriptComplete(null, { type: 'error', isTimeout: true });\n }, timeout);\n\n return { script, needAttach };\n}\n\nexport function createLink(info: {\n url: string;\n cb?: (value: void | PromiseLike<void>) => void;\n onErrorCallback?: (error: Error) => void;\n attrs: Record<string, string>;\n needDeleteLink?: boolean;\n createLinkHook?: (\n url: string,\n attrs?: Record<string, any>,\n ) => HTMLLinkElement | void;\n}) {\n // <link rel=\"preload\" href=\"script.js\" as=\"script\">\n\n // Retrieve the existing script element by its src attribute\n let link: HTMLLinkElement | null = null;\n let needAttach = true;\n const links = document.getElementsByTagName('link');\n for (let i = 0; i < links.length; i++) {\n const l = links[i];\n const linkHref = l.getAttribute('href');\n const linkRel = l.getAttribute('rel');\n if (\n linkHref &&\n isStaticResourcesEqual(linkHref, info.url) &&\n linkRel === info.attrs['rel']\n ) {\n link = l;\n needAttach = false;\n break;\n }\n }\n\n if (!link) {\n link = document.createElement('link');\n link.setAttribute('href', info.url);\n\n let createLinkRes: void | HTMLLinkElement = undefined;\n const attrs = info.attrs;\n\n if (info.createLinkHook) {\n createLinkRes = info.createLinkHook(info.url, attrs);\n if (createLinkRes instanceof HTMLLinkElement) {\n link = createLinkRes;\n }\n }\n\n if (attrs && !createLinkRes) {\n Object.keys(attrs).forEach((name) => {\n if (link && !link.getAttribute(name)) {\n link.setAttribute(name, attrs[name]);\n }\n });\n }\n }\n\n const onLinkComplete = (\n prev: OnErrorEventHandler | GlobalEventHandlers['onload'] | null,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n event: any,\n ): void => {\n const onLinkCompleteCallback = () => {\n if (event?.type === 'error') {\n info?.onErrorCallback && info?.onErrorCallback(event);\n } else {\n info?.cb && info?.cb();\n }\n };\n // Prevent memory leaks in IE.\n if (link) {\n link.onerror = null;\n link.onload = null;\n safeWrapper(() => {\n const { needDeleteLink = true } = info;\n if (needDeleteLink) {\n link?.parentNode && link.parentNode.removeChild(link);\n }\n });\n if (prev) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const res = (prev as any)(event);\n onLinkCompleteCallback();\n return res;\n }\n }\n onLinkCompleteCallback();\n };\n\n link.onerror = onLinkComplete.bind(null, link.onerror);\n link.onload = onLinkComplete.bind(null, link.onload);\n\n return { link, needAttach };\n}\n\nexport function loadScript(\n url: string,\n info: {\n attrs?: Record<string, any>;\n createScriptHook?: CreateScriptHookDom;\n },\n) {\n const { attrs = {}, createScriptHook } = info;\n return new Promise<void>((resolve, reject) => {\n const { script, needAttach } = createScript({\n url,\n cb: resolve,\n onErrorCallback: reject,\n attrs: {\n fetchpriority: 'high',\n ...attrs,\n },\n createScriptHook,\n needDeleteScript: true,\n });\n needAttach && document.head.appendChild(script);\n });\n}\n"],"mappings":";;;AAGA,eAAsB,YACpB,UACA,aACoC;AACpC,KAAI;AAEF,SADY,MAAM,UAAU;UAErB,GAAG;AACV,GAAC,eAAeA,mBAAK,EAAE;AACvB;;;AAIJ,SAAgB,uBAAuB,MAAc,MAAuB;CAC1E,MAAM,UAAU;AAKhB,QAHqB,KAAK,QAAQ,SAAS,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AG7B,SAAgB,WACd,KACA,MAIA;CACA,MAAM,EAAE,QAAQ,EAAE,EAAE,qBAAqB;AACzC,QAAO,IAAI,SAAe,SAAS,WAAW;EAC5C,MAAM,EAAE,QAAQ,eAAe,aAAa;GAC1C;GACA,IAAI;GACJ,iBAAiB;GACjB,OAAO;IACL,eAAe;IACf,GAAG;IACJ;GACD;GACA,kBAAkB;GACnB,CAAC;AACF,gBAAc,SAAS,KAAK,YAAY,OAAO;GAC/C"} | ||
| {"version":3,"file":"dom.cjs","names":["warn"],"sources":["../src/dom.ts"],"sourcesContent":["import type {\n CreateLinkHookDom,\n CreateLinkHookReturnDom,\n CreateScriptHookDom,\n CreateScriptHookReturnDom,\n} from './types';\nimport { warn } from './utils';\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport async function safeWrapper<T extends (...args: Array<any>) => any>(\n callback: T,\n disableWarn?: boolean,\n): Promise<ReturnType<T> | undefined> {\n try {\n const res = await callback();\n return res;\n } catch (e) {\n !disableWarn && warn(e);\n return;\n }\n}\n\nexport function isStaticResourcesEqual(url1: string, url2: string): boolean {\n const REG_EXP = /^(https?:)?\\/\\//i;\n // Transform url1 and url2 into relative paths\n const relativeUrl1 = url1.replace(REG_EXP, '').replace(/\\/$/, '');\n const relativeUrl2 = url2.replace(REG_EXP, '').replace(/\\/$/, '');\n // Check if the relative paths are identical\n return relativeUrl1 === relativeUrl2;\n}\n\nexport function createScript(info: {\n url: string;\n cb?: (value: void | PromiseLike<void>) => void;\n onErrorCallback?: (error: Error) => void;\n attrs?: Record<string, any>;\n needDeleteScript?: boolean;\n createScriptHook?: CreateScriptHookDom;\n}): { script: HTMLScriptElement; needAttach: boolean } {\n // Retrieve the existing script element by its src attribute\n let script: HTMLScriptElement | null = null;\n let needAttach = true;\n let timeout = 20000;\n let timeoutId: NodeJS.Timeout;\n const scripts = document.getElementsByTagName('script');\n\n for (let i = 0; i < scripts.length; i++) {\n const s = scripts[i];\n const scriptSrc = s.getAttribute('src');\n if (scriptSrc && isStaticResourcesEqual(scriptSrc, info.url)) {\n script = s;\n needAttach = false;\n break;\n }\n }\n\n if (!script) {\n const attrs = info.attrs;\n script = document.createElement('script');\n script.type = attrs?.['type'] === 'module' ? 'module' : 'text/javascript';\n let createScriptRes: CreateScriptHookReturnDom = undefined;\n if (info.createScriptHook) {\n createScriptRes = info.createScriptHook(info.url, info.attrs);\n\n if (createScriptRes instanceof HTMLScriptElement) {\n script = createScriptRes;\n } else if (typeof createScriptRes === 'object') {\n if ('script' in createScriptRes && createScriptRes.script) {\n script = createScriptRes.script;\n }\n if ('timeout' in createScriptRes && createScriptRes.timeout) {\n timeout = createScriptRes.timeout;\n }\n }\n }\n if (!script.src) {\n script.src = info.url;\n }\n if (attrs && !createScriptRes) {\n Object.keys(attrs).forEach((name) => {\n if (script) {\n if (name === 'async' || name === 'defer') {\n script[name] = attrs[name];\n // Attributes that do not exist are considered overridden\n } else if (!script.getAttribute(name)) {\n script.setAttribute(name, attrs[name]);\n }\n }\n });\n }\n }\n\n // Capture JS runtime errors thrown by this script's IIFE during execution.\n // The browser still fires onload even when the script throws, so without this\n // listener the execution error would silently become an uncaught global exception.\n let executionError: Error | null = null;\n const executionErrorHandler =\n typeof window !== 'undefined'\n ? (evt: ErrorEvent) => {\n if (evt.filename && isStaticResourcesEqual(evt.filename, info.url)) {\n const err = new Error(\n `ScriptExecutionError: Script \"${info.url}\" loaded but threw a runtime error during execution: ${evt.message} (${evt.filename}:${evt.lineno}:${evt.colno})`,\n );\n err.name = 'ScriptExecutionError';\n executionError = err;\n }\n }\n : null;\n if (executionErrorHandler) {\n window.addEventListener('error', executionErrorHandler);\n }\n\n const onScriptComplete = async (\n prev: OnErrorEventHandler | GlobalEventHandlers['onload'] | null,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n event: any,\n ): Promise<void> => {\n clearTimeout(timeoutId);\n if (executionErrorHandler) {\n window.removeEventListener('error', executionErrorHandler);\n }\n const onScriptCompleteCallback = () => {\n if (event?.type === 'error') {\n const networkError = new Error(\n event?.isTimeout\n ? `ScriptNetworkError: Script \"${info.url}\" timed out.`\n : `ScriptNetworkError: Failed to load script \"${info.url}\" - the script URL is unreachable or the server returned an error (network failure, 404, CORS, etc.)`,\n );\n networkError.name = 'ScriptNetworkError';\n info?.onErrorCallback && info?.onErrorCallback(networkError);\n } else if (executionError) {\n // Script fetched OK (onload fired) but IIFE threw during execution.\n info?.onErrorCallback && info?.onErrorCallback(executionError);\n } else {\n info?.cb && info?.cb();\n }\n };\n\n // Prevent memory leaks in IE.\n if (script) {\n script.onerror = null;\n script.onload = null;\n safeWrapper(() => {\n const { needDeleteScript = true } = info;\n if (needDeleteScript) {\n script?.parentNode && script.parentNode.removeChild(script);\n }\n });\n if (prev && typeof prev === 'function') {\n const result = (prev as any)(event);\n if (result instanceof Promise) {\n const res = await result;\n onScriptCompleteCallback();\n return res;\n }\n onScriptCompleteCallback();\n return result;\n }\n }\n onScriptCompleteCallback();\n };\n\n script.onerror = onScriptComplete.bind(null, script.onerror);\n script.onload = onScriptComplete.bind(null, script.onload);\n\n timeoutId = setTimeout(() => {\n onScriptComplete(null, { type: 'error', isTimeout: true });\n }, timeout);\n\n return { script, needAttach };\n}\n\nexport function createLink(info: {\n url: string;\n cb?: (value: void | PromiseLike<void>) => void;\n onErrorCallback?: (error: Error) => void;\n attrs: Record<string, string>;\n needDeleteLink?: boolean;\n createLinkHook?: CreateLinkHookDom;\n}) {\n // <link rel=\"preload\" href=\"script.js\" as=\"script\">\n\n // Retrieve the existing script element by its src attribute\n let link: HTMLLinkElement | null = null;\n let needAttach = true;\n let timeout = 20000;\n let timeoutId: NodeJS.Timeout | undefined;\n const links = document.getElementsByTagName('link');\n for (let i = 0; i < links.length; i++) {\n const l = links[i];\n const linkHref = l.getAttribute('href');\n const linkRel = l.getAttribute('rel');\n if (\n linkHref &&\n isStaticResourcesEqual(linkHref, info.url) &&\n linkRel === info.attrs['rel']\n ) {\n link = l;\n needAttach = false;\n break;\n }\n }\n\n if (!link) {\n link = document.createElement('link');\n link.setAttribute('href', info.url);\n\n let createLinkRes: CreateLinkHookReturnDom = undefined;\n let shouldApplyAttrs = true;\n const attrs = info.attrs;\n\n if (info.createLinkHook) {\n createLinkRes = info.createLinkHook(info.url, attrs);\n if (createLinkRes instanceof HTMLLinkElement) {\n link = createLinkRes;\n shouldApplyAttrs = false;\n } else if (typeof createLinkRes === 'object') {\n if ('link' in createLinkRes && createLinkRes.link) {\n link = createLinkRes.link;\n shouldApplyAttrs = false;\n }\n if ('timeout' in createLinkRes && createLinkRes.timeout) {\n timeout = createLinkRes.timeout;\n }\n }\n }\n\n if (attrs && shouldApplyAttrs) {\n Object.keys(attrs).forEach((name) => {\n if (link && !link.getAttribute(name)) {\n link.setAttribute(name, attrs[name]);\n }\n });\n }\n }\n\n if (!needAttach) {\n Promise.resolve().then(() => {\n info?.cb && info?.cb();\n });\n return { link, needAttach };\n }\n\n const onLinkComplete = (\n prev: OnErrorEventHandler | GlobalEventHandlers['onload'] | null,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n event: any,\n ): void => {\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n const onLinkCompleteCallback = () => {\n if (event?.type === 'error') {\n const linkError = new Error(\n event?.isTimeout\n ? `LinkNetworkError: Link \"${info.url}\" timed out.`\n : `LinkNetworkError: Failed to load link \"${info.url}\" - the URL is unreachable or the server returned an error.`,\n );\n linkError.name = 'LinkNetworkError';\n info?.onErrorCallback && info?.onErrorCallback(linkError);\n } else {\n info?.cb && info?.cb();\n }\n };\n // Prevent memory leaks in IE.\n if (link) {\n link.onerror = null;\n link.onload = null;\n safeWrapper(() => {\n const { needDeleteLink = true } = info;\n if (needDeleteLink) {\n link?.parentNode && link.parentNode.removeChild(link);\n }\n });\n if (prev) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const res = (prev as any)(event);\n onLinkCompleteCallback();\n return res;\n }\n }\n onLinkCompleteCallback();\n };\n\n link.onerror = onLinkComplete.bind(null, link.onerror);\n link.onload = onLinkComplete.bind(null, link.onload);\n timeoutId = setTimeout(() => {\n onLinkComplete(null, { type: 'error', isTimeout: true });\n }, timeout);\n\n return { link, needAttach };\n}\n\nexport function loadScript(\n url: string,\n info: {\n attrs?: Record<string, any>;\n createScriptHook?: CreateScriptHookDom;\n },\n) {\n const { attrs = {}, createScriptHook } = info;\n return new Promise<void>((resolve, reject) => {\n const { script, needAttach } = createScript({\n url,\n cb: resolve,\n onErrorCallback: reject,\n attrs: {\n fetchpriority: 'high',\n ...attrs,\n },\n createScriptHook,\n needDeleteScript: true,\n });\n needAttach && document.head.appendChild(script);\n });\n}\n"],"mappings":";;;AAQA,eAAsB,YACpB,UACA,aACoC;AACpC,KAAI;AAEF,SADY,MAAM,UAAU;UAErB,GAAG;AACV,GAAC,eAAeA,mBAAK,EAAE;AACvB;;;AAIJ,SAAgB,uBAAuB,MAAc,MAAuB;CAC1E,MAAM,UAAU;AAKhB,QAHqB,KAAK,QAAQ,SAAS,GAAG,CAAC,QAAQ,OAAO,GAAG,KAC5C,KAAK,QAAQ,SAAS,GAAG,CAAC,QAAQ,OAAO,GAAG;;AAKnE,SAAgB,aAAa,MAO0B;CAErD,IAAI,SAAmC;CACvC,IAAI,aAAa;CACjB,IAAI,UAAU;CACd,IAAI;CACJ,MAAM,UAAU,SAAS,qBAAqB,SAAS;AAEvD,MAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;EACvC,MAAM,IAAI,QAAQ;EAClB,MAAM,YAAY,EAAE,aAAa,MAAM;AACvC,MAAI,aAAa,uBAAuB,WAAW,KAAK,IAAI,EAAE;AAC5D,YAAS;AACT,gBAAa;AACb;;;AAIJ,KAAI,CAAC,QAAQ;EACX,MAAM,QAAQ,KAAK;AACnB,WAAS,SAAS,cAAc,SAAS;AACzC,SAAO,OAAO,QAAQ,YAAY,WAAW,WAAW;EACxD,IAAI,kBAA6C;AACjD,MAAI,KAAK,kBAAkB;AACzB,qBAAkB,KAAK,iBA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,EAAE,QAAQ,EAAE,EAAE,qBAAqB;AACzC,QAAO,IAAI,SAAe,SAAS,WAAW;EAC5C,MAAM,EAAE,QAAQ,eAAe,aAAa;GAC1C;GACA,IAAI;GACJ,iBAAiB;GACjB,OAAO;IACL,eAAe;IACf,GAAG;IACJ;GACD;GACA,kBAAkB;GACnB,CAAC;AACF,gBAAc,SAAS,KAAK,YAAY,OAAO;GAC/C"} |
+2
-2
@@ -1,2 +0,2 @@ | ||
| import { CreateScriptHookDom } from "./types/hooks.js"; | ||
| import { CreateLinkHookDom, CreateScriptHookDom } from "./types/hooks.js"; | ||
| //#region src/dom.d.ts | ||
@@ -22,3 +22,3 @@ declare function safeWrapper<T extends (...args: Array<any>) => any>(callback: T, disableWarn?: boolean): Promise<ReturnType<T> | undefined>; | ||
| needDeleteLink?: boolean; | ||
| createLinkHook?: (url: string, attrs?: Record<string, any>) => HTMLLinkElement | void; | ||
| createLinkHook?: CreateLinkHookDom; | ||
| }): { | ||
@@ -25,0 +25,0 @@ link: HTMLLinkElement; |
+35
-4
@@ -108,2 +108,4 @@ import { warn } from "./utils.js"; | ||
| let needAttach = true; | ||
| let timeout = 2e4; | ||
| let timeoutId; | ||
| const links = document.getElementsByTagName("link"); | ||
@@ -124,15 +126,38 @@ for (let i = 0; i < links.length; i++) { | ||
| let createLinkRes = void 0; | ||
| let shouldApplyAttrs = true; | ||
| const attrs = info.attrs; | ||
| if (info.createLinkHook) { | ||
| createLinkRes = info.createLinkHook(info.url, attrs); | ||
| if (createLinkRes instanceof HTMLLinkElement) link = createLinkRes; | ||
| if (createLinkRes instanceof HTMLLinkElement) { | ||
| link = createLinkRes; | ||
| shouldApplyAttrs = false; | ||
| } else if (typeof createLinkRes === "object") { | ||
| if ("link" in createLinkRes && createLinkRes.link) { | ||
| link = createLinkRes.link; | ||
| shouldApplyAttrs = false; | ||
| } | ||
| if ("timeout" in createLinkRes && createLinkRes.timeout) timeout = createLinkRes.timeout; | ||
| } | ||
| } | ||
| if (attrs && !createLinkRes) Object.keys(attrs).forEach((name) => { | ||
| if (attrs && shouldApplyAttrs) Object.keys(attrs).forEach((name) => { | ||
| if (link && !link.getAttribute(name)) link.setAttribute(name, attrs[name]); | ||
| }); | ||
| } | ||
| if (!needAttach) { | ||
| Promise.resolve().then(() => { | ||
| info?.cb && info?.cb(); | ||
| }); | ||
| return { | ||
| link, | ||
| needAttach | ||
| }; | ||
| } | ||
| const onLinkComplete = (prev, event) => { | ||
| if (timeoutId) clearTimeout(timeoutId); | ||
| const onLinkCompleteCallback = () => { | ||
| if (event?.type === "error") info?.onErrorCallback && info?.onErrorCallback(event); | ||
| else info?.cb && info?.cb(); | ||
| if (event?.type === "error") { | ||
| const linkError = /* @__PURE__ */ new Error(event?.isTimeout ? `LinkNetworkError: Link "${info.url}" timed out.` : `LinkNetworkError: Failed to load link "${info.url}" - the URL is unreachable or the server returned an error.`); | ||
| linkError.name = "LinkNetworkError"; | ||
| info?.onErrorCallback && info?.onErrorCallback(linkError); | ||
| } else info?.cb && info?.cb(); | ||
| }; | ||
@@ -156,2 +181,8 @@ if (link) { | ||
| link.onload = onLinkComplete.bind(null, link.onload); | ||
| timeoutId = setTimeout(() => { | ||
| onLinkComplete(null, { | ||
| type: "error", | ||
| isTimeout: true | ||
| }); | ||
| }, timeout); | ||
| return { | ||
@@ -158,0 +189,0 @@ link, |
+1
-1
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"dom.js","names":[],"sources":["../src/dom.ts"],"sourcesContent":["import type { CreateScriptHookDom, CreateScriptHookReturnDom } from './types';\nimport { warn } from './utils';\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport async function safeWrapper<T extends (...args: Array<any>) => any>(\n callback: T,\n disableWarn?: boolean,\n): Promise<ReturnType<T> | undefined> {\n try {\n const res = await callback();\n return res;\n } catch (e) {\n !disableWarn && warn(e);\n return;\n }\n}\n\nexport function isStaticResourcesEqual(url1: string, url2: string): boolean {\n const REG_EXP = /^(https?:)?\\/\\//i;\n // Transform url1 and url2 into relative paths\n const relativeUrl1 = url1.replace(REG_EXP, '').replace(/\\/$/, '');\n const relativeUrl2 = url2.replace(REG_EXP, '').replace(/\\/$/, '');\n // Check if the relative paths are identical\n return relativeUrl1 === relativeUrl2;\n}\n\nexport function createScript(info: {\n url: string;\n cb?: (value: void | PromiseLike<void>) => void;\n onErrorCallback?: (error: Error) => void;\n attrs?: Record<string, any>;\n needDeleteScript?: boolean;\n createScriptHook?: CreateScriptHookDom;\n}): { script: HTMLScriptElement; needAttach: boolean } {\n // Retrieve the existing script element by its src attribute\n let script: HTMLScriptElement | null = null;\n let needAttach = true;\n let timeout = 20000;\n let timeoutId: NodeJS.Timeout;\n const scripts = document.getElementsByTagName('script');\n\n for (let i = 0; i < scripts.length; i++) {\n const s = scripts[i];\n const scriptSrc = s.getAttribute('src');\n if (scriptSrc && isStaticResourcesEqual(scriptSrc, info.url)) {\n script = s;\n needAttach = false;\n break;\n }\n }\n\n if (!script) {\n const attrs = info.attrs;\n script = document.createElement('script');\n script.type = attrs?.['type'] === 'module' ? 'module' : 'text/javascript';\n let createScriptRes: CreateScriptHookReturnDom = undefined;\n if (info.createScriptHook) {\n createScriptRes = info.createScriptHook(info.url, info.attrs);\n\n if (createScriptRes instanceof HTMLScriptElement) {\n script = createScriptRes;\n } else if (typeof createScriptRes === 'object') {\n if ('script' in createScriptRes && createScriptRes.script) {\n script = createScriptRes.script;\n }\n if ('timeout' in createScriptRes && createScriptRes.timeout) {\n timeout = createScriptRes.timeout;\n }\n }\n }\n if (!script.src) {\n script.src = info.url;\n }\n if (attrs && !createScriptRes) {\n Object.keys(attrs).forEach((name) => {\n if (script) {\n if (name === 'async' || name === 'defer') {\n script[name] = attrs[name];\n // Attributes that do not exist are considered overridden\n } else if (!script.getAttribute(name)) {\n script.setAttribute(name, attrs[name]);\n }\n }\n });\n }\n }\n\n // Capture JS runtime errors thrown by this script's IIFE during execution.\n // The browser still fires onload even when the script throws, so without this\n // listener the execution error would silently become an uncaught global exception.\n let executionError: Error | null = null;\n const executionErrorHandler =\n typeof window !== 'undefined'\n ? (evt: ErrorEvent) => {\n if (evt.filename && isStaticResourcesEqual(evt.filename, info.url)) {\n const err = new Error(\n `ScriptExecutionError: Script \"${info.url}\" loaded but threw a runtime error during execution: ${evt.message} (${evt.filename}:${evt.lineno}:${evt.colno})`,\n );\n err.name = 'ScriptExecutionError';\n executionError = err;\n }\n }\n : null;\n if (executionErrorHandler) {\n window.addEventListener('error', executionErrorHandler);\n }\n\n const onScriptComplete = async (\n prev: OnErrorEventHandler | GlobalEventHandlers['onload'] | null,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n event: any,\n ): Promise<void> => {\n clearTimeout(timeoutId);\n if (executionErrorHandler) {\n window.removeEventListener('error', executionErrorHandler);\n }\n const onScriptCompleteCallback = () => {\n if (event?.type === 'error') {\n const networkError = new Error(\n event?.isTimeout\n ? `ScriptNetworkError: Script \"${info.url}\" timed out.`\n : `ScriptNetworkError: Failed to load script \"${info.url}\" - the script URL is unreachable or the server returned an error (network failure, 404, CORS, etc.)`,\n );\n networkError.name = 'ScriptNetworkError';\n info?.onErrorCallback && info?.onErrorCallback(networkError);\n } else if (executionError) {\n // Script fetched OK (onload fired) but IIFE threw during execution.\n info?.onErrorCallback && info?.onErrorCallback(executionError);\n } else {\n info?.cb && info?.cb();\n }\n };\n\n // Prevent memory leaks in IE.\n if (script) {\n script.onerror = null;\n script.onload = null;\n safeWrapper(() => {\n const { needDeleteScript = true } = info;\n if (needDeleteScript) {\n script?.parentNode && script.parentNode.removeChild(script);\n }\n });\n if (prev && typeof prev === 'function') {\n const result = (prev as any)(event);\n if (result instanceof Promise) {\n const res = await result;\n onScriptCompleteCallback();\n return res;\n }\n onScriptCompleteCallback();\n return result;\n }\n }\n onScriptCompleteCallback();\n };\n\n script.onerror = onScriptComplete.bind(null, script.onerror);\n script.onload = onScriptComplete.bind(null, script.onload);\n\n timeoutId = setTimeout(() => {\n onScriptComplete(null, { type: 'error', isTimeout: true });\n }, timeout);\n\n return { script, needAttach };\n}\n\nexport function createLink(info: {\n url: string;\n cb?: (value: void | PromiseLike<void>) => void;\n onErrorCallback?: (error: Error) => void;\n attrs: Record<string, string>;\n needDeleteLink?: boolean;\n createLinkHook?: (\n url: string,\n attrs?: Record<string, any>,\n ) => HTMLLinkElement | void;\n}) {\n // <link rel=\"preload\" href=\"script.js\" as=\"script\">\n\n // Retrieve the existing script element by its src attribute\n let link: HTMLLinkElement | null = null;\n let needAttach = true;\n const links = document.getElementsByTagName('link');\n for (let i = 0; i < links.length; i++) {\n const l = links[i];\n const linkHref = l.getAttribute('href');\n const linkRel = l.getAttribute('rel');\n if (\n linkHref &&\n isStaticResourcesEqual(linkHref, info.url) &&\n linkRel === info.attrs['rel']\n ) {\n link = l;\n needAttach = false;\n break;\n }\n }\n\n if (!link) {\n link = document.createElement('link');\n link.setAttribute('href', info.url);\n\n let createLinkRes: void | HTMLLinkElement = undefined;\n const attrs = info.attrs;\n\n if (info.createLinkHook) {\n createLinkRes = info.createLinkHook(info.url, attrs);\n if (createLinkRes instanceof HTMLLinkElement) {\n link = createLinkRes;\n }\n }\n\n if (attrs && !createLinkRes) {\n Object.keys(attrs).forEach((name) => {\n if (link && !link.getAttribute(name)) {\n link.setAttribute(name, attrs[name]);\n }\n });\n }\n }\n\n const onLinkComplete = (\n prev: OnErrorEventHandler | GlobalEventHandlers['onload'] | null,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n event: any,\n ): void => {\n const onLinkCompleteCallback = () => {\n if (event?.type === 'error') {\n info?.onErrorCallback && info?.onErrorCallback(event);\n } else {\n info?.cb && info?.cb();\n }\n };\n // Prevent memory leaks in IE.\n if (link) {\n link.onerror = null;\n link.onload = null;\n safeWrapper(() => {\n const { needDeleteLink = true } = info;\n if (needDeleteLink) {\n link?.parentNode && link.parentNode.removeChild(link);\n }\n });\n if (prev) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const res = (prev as any)(event);\n onLinkCompleteCallback();\n return res;\n }\n }\n onLinkCompleteCallback();\n };\n\n link.onerror = onLinkComplete.bind(null, link.onerror);\n link.onload = onLinkComplete.bind(null, link.onload);\n\n return { link, needAttach };\n}\n\nexport function loadScript(\n url: string,\n info: {\n attrs?: Record<string, any>;\n createScriptHook?: CreateScriptHookDom;\n },\n) {\n const { attrs = {}, createScriptHook } = info;\n return new Promise<void>((resolve, reject) => {\n const { script, needAttach } = createScript({\n url,\n cb: resolve,\n onErrorCallback: reject,\n attrs: {\n fetchpriority: 'high',\n ...attrs,\n },\n createScriptHook,\n needDeleteScript: true,\n });\n needAttach && document.head.appendChild(script);\n });\n}\n"],"mappings":";;;AAGA,eAAsB,YACpB,UACA,aACoC;AACpC,KAAI;AAEF,SADY,MAAM,UAAU;UAErB,GAAG;AACV,GAAC,eAAe,KAAK,EAAE;AACvB;;;AAIJ,SAAgB,uBAAuB,MAAc,MAAuB;CAC1E,MAAM,UAAU;AAKhB,QAHqB,KAAK,QAAQ,SAAS,GAAG,CAAC,QAAQ,OAAO,GAAG,KAC5C,KAAK,QAAQ,SAAS,GAAG,CAAC,QAAQ,OAAO,GAAG;;AAKnE,SAAgB,aAAa,MAO0B;CAErD,IAAI,SAAmC;CACvC,IAAI,aAAa;CACjB,IAAI,UAAU;CACd,IAAI;CACJ,MAAM,UAAU,SAAS,qBAAqB,SAAS;AAEvD,MAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;EACvC,MAAM,IAAI,QAAQ;EAClB,MAAM,YAAY,EAAE,aAAa,MAAM;AACvC,MAAI,aAAa,uBAAuB,WAAW,KAAK,IAAI,EAAE;AAC5D,YAAS;AACT,gBAAa;AACb;;;AAIJ,KAAI,CAAC,QAAQ;EACX,MAAM,QAAQ,KAAK;AACnB,WAAS,SAAS,cAAc,SAAS;AACzC,SAAO,OAAO,QAAQ,YAAY,WAAW,WAAW;EACxD,IAAI,kBAA6C;AACjD,MAAI,KAAK,kBAAkB;AACzB,qBAAkB,KAAK,iBAAi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,OAAI,QAAQ,CAAC,KAAK,aAAa,KAAK,CAClC,MAAK,aAAa,MAAM,MAAM,MAAM;IAEtC;;CAIN,MAAM,kBACJ,MAEA,UACS;EACT,MAAM,+BAA+B;AACnC,OAAI,OAAO,SAAS,QAClB,OAAM,mBAAmB,MAAM,gBAAgB,MAAM;OAErD,OAAM,MAAM,MAAM,IAAI;;AAI1B,MAAI,MAAM;AACR,QAAK,UAAU;AACf,QAAK,SAAS;AACd,qBAAkB;IAChB,MAAM,EAAE,iBAAiB,SAAS;AAClC,QAAI,eACF,OAAM,cAAc,KAAK,WAAW,YAAY,KAAK;KAEvD;AACF,OAAI,MAAM;IAER,MAAM,MAAO,KAAa,MAAM;AAChC,4BAAwB;AACxB,WAAO;;;AAGX,0BAAwB;;AAG1B,MAAK,UAAU,eAAe,KAAK,MAAM,KAAK,QAAQ;AACtD,MAAK,SAAS,eAAe,KAAK,MAAM,KAAK,OAAO;AAEpD,QAAO;EAAE;EAAM;EAAY;;AAG7B,SAAgB,WACd,KACA,MAIA;CACA,MAAM,EAAE,QAAQ,EAAE,EAAE,qBAAqB;AACzC,QAAO,IAAI,SAAe,SAAS,WAAW;EAC5C,MAAM,EAAE,QAAQ,eAAe,aAAa;GAC1C;GACA,IAAI;GACJ,iBAAiB;GACjB,OAAO;IACL,eAAe;IACf,GAAG;IACJ;GACD;GACA,kBAAkB;GACnB,CAAC;AACF,gBAAc,SAAS,KAAK,YAAY,OAAO;GAC/C"} | ||
| {"version":3,"file":"dom.js","names":[],"sources":["../src/dom.ts"],"sourcesContent":["import type {\n CreateLinkHookDom,\n CreateLinkHookReturnDom,\n CreateScriptHookDom,\n CreateScriptHookReturnDom,\n} from './types';\nimport { warn } from './utils';\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport async function safeWrapper<T extends (...args: Array<any>) => any>(\n callback: T,\n disableWarn?: boolean,\n): Promise<ReturnType<T> | undefined> {\n try {\n const res = await callback();\n return res;\n } catch (e) {\n !disableWarn && warn(e);\n return;\n }\n}\n\nexport function isStaticResourcesEqual(url1: string, url2: string): boolean {\n const REG_EXP = /^(https?:)?\\/\\//i;\n // Transform url1 and url2 into relative paths\n const relativeUrl1 = url1.replace(REG_EXP, '').replace(/\\/$/, '');\n const relativeUrl2 = url2.replace(REG_EXP, '').replace(/\\/$/, '');\n // Check if the relative paths are identical\n return relativeUrl1 === relativeUrl2;\n}\n\nexport function createScript(info: {\n url: string;\n cb?: (value: void | PromiseLike<void>) => void;\n onErrorCallback?: (error: Error) => void;\n attrs?: Record<string, any>;\n needDeleteScript?: boolean;\n createScriptHook?: CreateScriptHookDom;\n}): { script: HTMLScriptElement; needAttach: boolean } {\n // Retrieve the existing script element by its src attribute\n let script: HTMLScriptElement | null = null;\n let needAttach = true;\n let timeout = 20000;\n let timeoutId: NodeJS.Timeout;\n const scripts = document.getElementsByTagName('script');\n\n for (let i = 0; i < scripts.length; i++) {\n const s = scripts[i];\n const scriptSrc = s.getAttribute('src');\n if (scriptSrc && isStaticResourcesEqual(scriptSrc, info.url)) {\n script = s;\n needAttach = false;\n break;\n }\n }\n\n if (!script) {\n const attrs = info.attrs;\n script = document.createElement('script');\n script.type = attrs?.['type'] === 'module' ? 'module' : 'text/javascript';\n let createScriptRes: CreateScriptHookReturnDom = undefined;\n if (info.createScriptHook) {\n createScriptRes = info.createScriptHook(info.url, info.attrs);\n\n if (createScriptRes instanceof HTMLScriptElement) {\n script = createScriptRes;\n } else if (typeof createScriptRes === 'object') {\n if ('script' in createScriptRes && createScriptRes.script) {\n script = createScriptRes.script;\n }\n if ('timeout' in createScriptRes && createScriptRes.timeout) {\n timeout = createScriptRes.timeout;\n }\n }\n }\n if (!script.src) {\n script.src = info.url;\n }\n if (attrs && !createScriptRes) {\n Object.keys(attrs).forEach((name) => {\n if (script) {\n if (name === 'async' || name === 'defer') {\n script[name] = attrs[name];\n // Attributes that do not exist are considered overridden\n } else if (!script.getAttribute(name)) {\n script.setAttribute(name, attrs[name]);\n }\n }\n });\n }\n }\n\n // Capture JS runtime errors thrown by this script's IIFE during execution.\n // The browser still fires onload even when the script throws, so without this\n // listener the execution error would silently become an uncaught global exception.\n let executionError: Error | null = null;\n const executionErrorHandler =\n typeof window !== 'undefined'\n ? (evt: ErrorEvent) => {\n if (evt.filename && isStaticResourcesEqual(evt.filename, info.url)) {\n const err = new Error(\n `ScriptExecutionError: Script \"${info.url}\" loaded but threw a runtime error during execution: ${evt.message} (${evt.filename}:${evt.lineno}:${evt.colno})`,\n );\n err.name = 'ScriptExecutionError';\n executionError = err;\n }\n }\n : null;\n if (executionErrorHandler) {\n window.addEventListener('error', executionErrorHandler);\n }\n\n const onScriptComplete = async (\n prev: OnErrorEventHandler | GlobalEventHandlers['onload'] | null,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n event: any,\n ): Promise<void> => {\n clearTimeout(timeoutId);\n if (executionErrorHandler) {\n window.removeEventListener('error', executionErrorHandler);\n }\n const onScriptCompleteCallback = () => {\n if (event?.type === 'error') {\n const networkError = new Error(\n event?.isTimeout\n ? `ScriptNetworkError: Script \"${info.url}\" timed out.`\n : `ScriptNetworkError: Failed to load script \"${info.url}\" - the script URL is unreachable or the server returned an error (network failure, 404, CORS, etc.)`,\n );\n networkError.name = 'ScriptNetworkError';\n info?.onErrorCallback && info?.onErrorCallback(networkError);\n } else if (executionError) {\n // Script fetched OK (onload fired) but IIFE threw during execution.\n info?.onErrorCallback && info?.onErrorCallback(executionError);\n } else {\n info?.cb && info?.cb();\n }\n };\n\n // Prevent memory leaks in IE.\n if (script) {\n script.onerror = null;\n script.onload = null;\n safeWrapper(() => {\n const { needDeleteScript = true } = info;\n if (needDeleteScript) {\n script?.parentNode && script.parentNode.removeChild(script);\n }\n });\n if (prev && typeof prev === 'function') {\n const result = (prev as any)(event);\n if (result instanceof Promise) {\n const res = await result;\n onScriptCompleteCallback();\n return res;\n }\n onScriptCompleteCallback();\n return result;\n }\n }\n onScriptCompleteCallback();\n };\n\n script.onerror = onScriptComplete.bind(null, script.onerror);\n script.onload = onScriptComplete.bind(null, script.onload);\n\n timeoutId = setTimeout(() => {\n onScriptComplete(null, { type: 'error', isTimeout: true });\n }, timeout);\n\n return { script, needAttach };\n}\n\nexport function createLink(info: {\n url: string;\n cb?: (value: void | PromiseLike<void>) => void;\n onErrorCallback?: (error: Error) => void;\n attrs: Record<string, string>;\n needDeleteLink?: boolean;\n createLinkHook?: CreateLinkHookDom;\n}) {\n // <link rel=\"preload\" href=\"script.js\" as=\"script\">\n\n // Retrieve the existing script element by its src attribute\n let link: HTMLLinkElement | null = null;\n let needAttach = true;\n let timeout = 20000;\n let timeoutId: NodeJS.Timeout | undefined;\n const links = document.getElementsByTagName('link');\n for (let i = 0; i < links.length; i++) {\n const l = links[i];\n const linkHref = l.getAttribute('href');\n const linkRel = l.getAttribute('rel');\n if (\n linkHref &&\n isStaticResourcesEqual(linkHref, info.url) &&\n linkRel === info.attrs['rel']\n ) {\n link = l;\n needAttach = false;\n break;\n }\n }\n\n if (!link) {\n link = document.createElement('link');\n link.setAttribute('href', info.url);\n\n let createLinkRes: CreateLinkHookReturnDom = undefined;\n let shouldApplyAttrs = true;\n const attrs = info.attrs;\n\n if (info.createLinkHook) {\n createLinkRes = info.createLinkHook(info.url, attrs);\n if (createLinkRes instanceof HTMLLinkElement) {\n link = createLinkRes;\n shouldApplyAttrs = false;\n } else if (typeof createLinkRes === 'object') {\n if ('link' in createLinkRes && createLinkRes.link) {\n link = createLinkRes.link;\n shouldApplyAttrs = false;\n }\n if ('timeout' in createLinkRes && createLinkRes.timeout) {\n timeout = createLinkRes.timeout;\n }\n }\n }\n\n if (attrs && shouldApplyAttrs) {\n Object.keys(attrs).forEach((name) => {\n if (link && !link.getAttribute(name)) {\n link.setAttribute(name, attrs[name]);\n }\n });\n }\n }\n\n if (!needAttach) {\n Promise.resolve().then(() => {\n info?.cb && info?.cb();\n });\n return { link, needAttach };\n }\n\n const onLinkComplete = (\n prev: OnErrorEventHandler | GlobalEventHandlers['onload'] | null,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n event: any,\n ): void => {\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n const onLinkCompleteCallback = () => {\n if (event?.type === 'error') {\n const linkError = new Error(\n event?.isTimeout\n ? `LinkNetworkError: Link \"${info.url}\" timed out.`\n : `LinkNetworkError: Failed to load link \"${info.url}\" - the URL is unreachable or the server returned an error.`,\n );\n linkError.name = 'LinkNetworkError';\n info?.onErrorCallback && info?.onErrorCallback(linkError);\n } else {\n info?.cb && info?.cb();\n }\n };\n // Prevent memory leaks in IE.\n if (link) {\n link.onerror = null;\n link.onload = null;\n safeWrapper(() => {\n const { needDeleteLink = true } = info;\n if (needDeleteLink) {\n link?.parentNode && link.parentNode.removeChild(link);\n }\n });\n if (prev) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const res = (prev as any)(event);\n onLinkCompleteCallback();\n return res;\n }\n }\n onLinkCompleteCallback();\n };\n\n link.onerror = onLinkComplete.bind(null, link.onerror);\n link.onload = onLinkComplete.bind(null, link.onload);\n timeoutId = setTimeout(() => {\n onLinkComplete(null, { type: 'error', isTimeout: true });\n }, timeout);\n\n return { link, needAttach };\n}\n\nexport function loadScript(\n url: string,\n info: {\n attrs?: Record<string, any>;\n createScriptHook?: CreateScriptHookDom;\n },\n) {\n const { attrs = {}, createScriptHook } = info;\n return new Promise<void>((resolve, reject) => {\n const { script, needAttach } = createScript({\n url,\n cb: resolve,\n onErrorCallback: reject,\n attrs: {\n fetchpriority: 'high',\n ...attrs,\n },\n createScriptHook,\n needDeleteScript: true,\n });\n needAttach && document.head.appendChild(script);\n });\n}\n"],"mappings":";;;AAQA,eAAsB,YACpB,UACA,aACoC;AACpC,KAAI;AAEF,SADY,MAAM,UAAU;UAErB,GAAG;AACV,GAAC,eAAe,KAAK,EAAE;AACvB;;;AAIJ,SAAgB,uBAAuB,MAAc,MAAuB;CAC1E,MAAM,UAAU;AAKhB,QAHqB,KAAK,QAAQ,SAAS,GAAG,CAAC,QAAQ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D;AACD,cAAU,OAAO;AACjB,UAAM,mBAAmB,MAAM,gBAAgB,UAAU;SAEzD,OAAM,MAAM,MAAM,IAAI;;AAI1B,MAAI,MAAM;AACR,QAAK,UAAU;AACf,QAAK,SAAS;AACd,qBAAkB;IAChB,MAAM,EAAE,iBAAiB,SAAS;AAClC,QAAI,eACF,OAAM,cAAc,KAAK,WAAW,YAAY,KAAK;KAEvD;AACF,OAAI,MAAM;IAER,MAAM,MAAO,KAAa,MAAM;AAChC,4BAAwB;AACxB,WAAO;;;AAGX,0BAAwB;;AAG1B,MAAK,UAAU,eAAe,KAAK,MAAM,KAAK,QAAQ;AACtD,MAAK,SAAS,eAAe,KAAK,MAAM,KAAK,OAAO;AACpD,aAAY,iBAAiB;AAC3B,iBAAe,MAAM;GAAE,MAAM;GAAS,WAAW;GAAM,CAAC;IACvD,QAAQ;AAEX,QAAO;EAAE;EAAM;EAAY;;AAG7B,SAAgB,WACd,KACA,MAIA;CACA,MAAM,EAAE,QAAQ,EAAE,EAAE,qBAAqB;AACzC,QAAO,IAAI,SAAe,SAAS,WAAW;EAC5C,MAAM,EAAE,QAAQ,eAAe,aAAa;GAC1C;GACA,IAAI;GACJ,iBAAiB;GACjB,OAAO;IACL,eAAe;IACf,GAAG;IACJ;GACD;GACA,kBAAkB;GACnB,CAAC;AACF,gBAAc,SAAS,KAAK,YAAY,OAAO;GAC/C"} |
+2
-2
@@ -12,3 +12,3 @@ import { BROWSER_LOG_KEY, ENCODE_NAME_PREFIX, EncodedNameTransformMap, FederationModuleManifest, MANIFEST_EXT, MFModuleType, MODULE_DEVTOOL_IDENTIFIER, ManifestFileName, NameTransformMap, NameTransformSymbol, SEPARATOR, StatsFileName, TEMP_DIR, TreeShakingStatus } from "./constant.js"; | ||
| import { ProvideSharedPlugin_d_exports } from "./types/plugins/ProvideSharedPlugin.js"; | ||
| import { CreateScriptHook, CreateScriptHookDom, CreateScriptHookNode, CreateScriptHookReturn, CreateScriptHookReturnDom, CreateScriptHookReturnNode, FetchHook } from "./types/hooks.js"; | ||
| import { CreateLinkHookDom, CreateLinkHookReturnDom, CreateScriptHook, CreateScriptHookDom, CreateScriptHookNode, CreateScriptHookReturn, CreateScriptHookReturnDom, CreateScriptHookReturnNode, FetchHook } from "./types/hooks.js"; | ||
| import { assert, composeKeyWithSeparator, decodeName, encodeName, error, generateExposeFilename, generateShareFilename, getResourceUrl, isRequiredVersion, parseEntry, safeToString, warn } from "./utils.js"; | ||
@@ -22,2 +22,2 @@ import { generateSnapshotFromManifest, getManifestFileName, inferAutoPublicPath, isManifestProvider, simpleJoinRemoteEntry } from "./generateSnapshotFromManifest.js"; | ||
| import { createModuleFederationConfig } from "./createModuleFederationConfig.js"; | ||
| export { BROWSER_LOG_KEY, BasicProviderModuleInfo, BasicStatsMetaData, ConsumerModuleInfo, ConsumerModuleInfoWithPublicPath, CreateScriptHook, CreateScriptHookDom, CreateScriptHookNode, CreateScriptHookReturn, CreateScriptHookReturnDom, CreateScriptHookReturnNode, ENCODE_NAME_PREFIX, EncodedNameTransformMap, FederationModuleManifest, FetchHook, GlobalModuleInfo, type InfrastructureLogger, type Logger, MANIFEST_EXT, MFModuleType, MODULE_DEVTOOL_IDENTIFIER, Manifest, ManifestExpose, ManifestFileName, ManifestModuleInfos, ManifestProvider, ManifestRemote, ManifestRemoteCommonInfo, ManifestShared, MetaDataTypes, Module, ModuleInfo, NameTransformMap, NameTransformSymbol, ProviderModuleInfo, PureConsumerModuleInfo, PureEntryProvider, RemoteEntryInfo, RemoteEntryType, RemoteWithEntry, RemoteWithVersion, ResourceInfo, SEPARATOR, Stats, StatsAssets, StatsBuildInfo, StatsExpose, StatsFileName, StatsMetaData, StatsMetaDataWithGetPublicPath, StatsMetaDataWithPublicPath, StatsModuleInfo, StatsRemote, StatsRemoteVal, StatsRemoteWithEntry, StatsRemoteWithVersion, StatsShared, TEMP_DIR, TreeShakingStatus, assert, bindLoggerToCompiler, composeKeyWithSeparator, ConsumeSharedPlugin_d_exports as consumeSharedPlugin, ContainerPlugin_d_exports as containerPlugin, ContainerReferencePlugin_d_exports as containerReferencePlugin, createInfrastructureLogger, createLink, createLogger, createModuleFederationConfig, createScript, createScriptNode, decodeName, encodeName, error, generateExposeFilename, generateShareFilename, generateSnapshotFromManifest, getManifestFileName, getProcessEnv, getResourceUrl, inferAutoPublicPath, infrastructureLogger, isBrowserEnv, isBrowserEnvValue, isDebugMode, isManifestProvider, isReactNativeEnv, isRequiredVersion, isStaticResourcesEqual, loadScript, loadScriptNode, logger, ModuleFederationPlugin_d_exports as moduleFederationPlugin, normalizeOptions, parseEntry, ProvideSharedPlugin_d_exports as provideSharedPlugin, safeToString, safeWrapper, SharePlugin_d_exports as sharePlugin, simpleJoinRemoteEntry, warn }; | ||
| export { BROWSER_LOG_KEY, BasicProviderModuleInfo, BasicStatsMetaData, ConsumerModuleInfo, ConsumerModuleInfoWithPublicPath, CreateLinkHookDom, CreateLinkHookReturnDom, CreateScriptHook, CreateScriptHookDom, CreateScriptHookNode, CreateScriptHookReturn, CreateScriptHookReturnDom, CreateScriptHookReturnNode, ENCODE_NAME_PREFIX, EncodedNameTransformMap, FederationModuleManifest, FetchHook, GlobalModuleInfo, type InfrastructureLogger, type Logger, MANIFEST_EXT, MFModuleType, MODULE_DEVTOOL_IDENTIFIER, Manifest, ManifestExpose, ManifestFileName, ManifestModuleInfos, ManifestProvider, ManifestRemote, ManifestRemoteCommonInfo, ManifestShared, MetaDataTypes, Module, ModuleInfo, NameTransformMap, NameTransformSymbol, ProviderModuleInfo, PureConsumerModuleInfo, PureEntryProvider, RemoteEntryInfo, RemoteEntryType, RemoteWithEntry, RemoteWithVersion, ResourceInfo, SEPARATOR, Stats, StatsAssets, StatsBuildInfo, StatsExpose, StatsFileName, StatsMetaData, StatsMetaDataWithGetPublicPath, StatsMetaDataWithPublicPath, StatsModuleInfo, StatsRemote, StatsRemoteVal, StatsRemoteWithEntry, StatsRemoteWithVersion, StatsShared, TEMP_DIR, TreeShakingStatus, assert, bindLoggerToCompiler, composeKeyWithSeparator, ConsumeSharedPlugin_d_exports as consumeSharedPlugin, ContainerPlugin_d_exports as containerPlugin, ContainerReferencePlugin_d_exports as containerReferencePlugin, createInfrastructureLogger, createLink, createLogger, createModuleFederationConfig, createScript, createScriptNode, decodeName, encodeName, error, generateExposeFilename, generateShareFilename, generateSnapshotFromManifest, getManifestFileName, getProcessEnv, getResourceUrl, inferAutoPublicPath, infrastructureLogger, isBrowserEnv, isBrowserEnvValue, isDebugMode, isManifestProvider, isReactNativeEnv, isRequiredVersion, isStaticResourcesEqual, loadScript, loadScriptNode, logger, ModuleFederationPlugin_d_exports as moduleFederationPlugin, normalizeOptions, parseEntry, ProvideSharedPlugin_d_exports as provideSharedPlugin, safeToString, safeWrapper, SharePlugin_d_exports as sharePlugin, simpleJoinRemoteEntry, warn }; |
@@ -9,9 +9,14 @@ //#region src/types/hooks.d.ts | ||
| } | void; | ||
| type CreateLinkHookReturnDom = HTMLLinkElement | { | ||
| link?: HTMLLinkElement; | ||
| timeout?: number; | ||
| } | void; | ||
| type CreateScriptHookReturn = CreateScriptHookReturnNode | CreateScriptHookReturnDom; | ||
| type CreateScriptHookNode = (url: string, attrs?: Record<string, any> | undefined) => CreateScriptHookReturnNode; | ||
| type CreateScriptHookDom = (url: string, attrs?: Record<string, any> | undefined) => CreateScriptHookReturnDom; | ||
| type CreateLinkHookDom = (url: string, attrs?: Record<string, any> | undefined) => CreateLinkHookReturnDom; | ||
| type CreateScriptHook = (url: string, attrs?: Record<string, any> | undefined) => CreateScriptHookReturn; | ||
| type FetchHook = (args: [string, RequestInit]) => Promise<Response> | void | false; | ||
| //#endregion | ||
| export { CreateScriptHook, CreateScriptHookDom, CreateScriptHookNode, CreateScriptHookReturn, CreateScriptHookReturnDom, CreateScriptHookReturnNode, FetchHook }; | ||
| export { CreateLinkHookDom, CreateLinkHookReturnDom, CreateScriptHook, CreateScriptHookDom, CreateScriptHookNode, CreateScriptHookReturn, CreateScriptHookReturnDom, CreateScriptHookReturnNode, FetchHook }; | ||
| //# sourceMappingURL=hooks.d.ts.map |
@@ -11,2 +11,2 @@ import { Module, RemoteEntryInfo, RemoteWithEntry, RemoteWithVersion } from "./common.js"; | ||
| import { ProvideSharedPlugin_d_exports } from "./plugins/ProvideSharedPlugin.js"; | ||
| import { CreateScriptHook, CreateScriptHookDom, CreateScriptHookNode, CreateScriptHookReturn, CreateScriptHookReturnDom, CreateScriptHookReturnNode, FetchHook } from "./hooks.js"; | ||
| import { CreateLinkHookDom, CreateLinkHookReturnDom, CreateScriptHook, CreateScriptHookDom, CreateScriptHookNode, CreateScriptHookReturn, CreateScriptHookReturnDom, CreateScriptHookReturnNode, FetchHook } from "./hooks.js"; |
@@ -13,2 +13,3 @@ import { RemoteWithEntry, RemoteWithVersion } from "./common.js"; | ||
| buildName: string; | ||
| hash?: string; | ||
| target?: string[]; | ||
@@ -89,2 +90,3 @@ plugins?: string[]; | ||
| assets: StatsAssets; | ||
| hash?: string; | ||
| } | ||
@@ -91,0 +93,0 @@ interface Stats<T = BasicStatsMetaData, K = StatsRemoteVal> { |
+1
-1
| { | ||
| "name": "@module-federation/sdk", | ||
| "version": "2.4.0", | ||
| "version": "2.5.0", | ||
| "type": "module", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 2 instances in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 2 instances in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
305946
1.83%3097
2.28%