@module-federation/sdk
Advanced tools
Comparing version 0.0.0-next-20240205081245 to 0.0.0-next-20240205095446
@@ -760,2 +760,59 @@ 'use strict'; | ||
} | ||
function createLink(url, cb, attrs, createLinkHook) { | ||
// <link rel="preload" href="script.js" as="script"> | ||
// Retrieve the existing script element by its src attribute | ||
var link = null; | ||
var needAttach = true; | ||
var links = document.getElementsByTagName("link"); | ||
for(var i = 0; i < links.length; i++){ | ||
var l = links[i]; | ||
var linkHref = l.getAttribute("href"); | ||
if (linkHref && isStaticResourcesEqual(linkHref, url)) { | ||
link = l; | ||
needAttach = false; | ||
break; | ||
} | ||
} | ||
if (!link) { | ||
link = document.createElement("link"); | ||
link.setAttribute("href", url); | ||
if (createLinkHook) { | ||
var createLinkRes = createLinkHook(url); | ||
if (_instanceof(createLinkRes, HTMLLinkElement)) { | ||
link = createLinkRes; | ||
} | ||
} | ||
} | ||
if (attrs) { | ||
Object.keys(attrs).forEach(function(name) { | ||
if (link) { | ||
link.setAttribute(name, attrs[name]); | ||
} | ||
}); | ||
} | ||
var onLinkComplete = function(prev, // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
event) { | ||
// Prevent memory leaks in IE. | ||
if (link) { | ||
link.onerror = null; | ||
link.onload = null; | ||
safeWrapper(function() { | ||
(link === null || link === void 0 ? void 0 : link.parentNode) && link.parentNode.removeChild(link); | ||
}); | ||
if (prev) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
var res = prev(event); | ||
cb(); | ||
return res; | ||
} | ||
} | ||
cb(); | ||
}; | ||
link.onerror = onLinkComplete.bind(null, link.onerror); | ||
link.onload = onLinkComplete.bind(null, link.onload); | ||
return { | ||
link: link, | ||
needAttach: needAttach | ||
}; | ||
} | ||
function loadScript(url, info) { | ||
@@ -1092,2 +1149,3 @@ var attrs = info.attrs, createScriptHook = info.createScriptHook; | ||
exports.composeKeyWithSeparator = composeKeyWithSeparator; | ||
exports.createLink = createLink; | ||
exports.createScript = createScript; | ||
@@ -1094,0 +1152,0 @@ exports.createScriptNode = createScriptNode; |
@@ -756,2 +756,59 @@ function _define_property$2(obj, key, value) { | ||
} | ||
function createLink(url, cb, attrs, createLinkHook) { | ||
// <link rel="preload" href="script.js" as="script"> | ||
// Retrieve the existing script element by its src attribute | ||
var link = null; | ||
var needAttach = true; | ||
var links = document.getElementsByTagName("link"); | ||
for(var i = 0; i < links.length; i++){ | ||
var l = links[i]; | ||
var linkHref = l.getAttribute("href"); | ||
if (linkHref && isStaticResourcesEqual(linkHref, url)) { | ||
link = l; | ||
needAttach = false; | ||
break; | ||
} | ||
} | ||
if (!link) { | ||
link = document.createElement("link"); | ||
link.setAttribute("href", url); | ||
if (createLinkHook) { | ||
var createLinkRes = createLinkHook(url); | ||
if (_instanceof(createLinkRes, HTMLLinkElement)) { | ||
link = createLinkRes; | ||
} | ||
} | ||
} | ||
if (attrs) { | ||
Object.keys(attrs).forEach(function(name) { | ||
if (link) { | ||
link.setAttribute(name, attrs[name]); | ||
} | ||
}); | ||
} | ||
var onLinkComplete = function(prev, // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
event) { | ||
// Prevent memory leaks in IE. | ||
if (link) { | ||
link.onerror = null; | ||
link.onload = null; | ||
safeWrapper(function() { | ||
(link === null || link === void 0 ? void 0 : link.parentNode) && link.parentNode.removeChild(link); | ||
}); | ||
if (prev) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
var res = prev(event); | ||
cb(); | ||
return res; | ||
} | ||
} | ||
cb(); | ||
}; | ||
link.onerror = onLinkComplete.bind(null, link.onerror); | ||
link.onload = onLinkComplete.bind(null, link.onload); | ||
return { | ||
link: link, | ||
needAttach: needAttach | ||
}; | ||
} | ||
function loadScript(url, info) { | ||
@@ -1077,2 +1134,2 @@ var attrs = info.attrs, createScriptHook = info.createScriptHook; | ||
export { BROWSER_LOG_KEY, BROWSER_LOG_VALUE, EncodedNameTransformMap, FederationModuleManifest, Logger, MANIFEST_EXT, NameTransformMap, NameTransformSymbol, SEPARATOR, assert, composeKeyWithSeparator, createScript, createScriptNode, decodeName, encodeName, error, generateExposeFilename, generateShareFilename, generateSnapshotFromManifest, getProcessEnv, getResourceUrl, isBrowserEnv, isDebugMode, isManifestProvider, isStaticResourcesEqual, loadScript, loadScriptNode, logger, parseEntry, safeWrapper, simpleJoinRemoteEntry, warn }; | ||
export { BROWSER_LOG_KEY, BROWSER_LOG_VALUE, EncodedNameTransformMap, FederationModuleManifest, Logger, MANIFEST_EXT, NameTransformMap, NameTransformSymbol, SEPARATOR, assert, composeKeyWithSeparator, createLink, createScript, createScriptNode, decodeName, encodeName, error, generateExposeFilename, generateShareFilename, generateSnapshotFromManifest, getProcessEnv, getResourceUrl, isBrowserEnv, isDebugMode, isManifestProvider, isStaticResourcesEqual, loadScript, loadScriptNode, logger, parseEntry, safeWrapper, simpleJoinRemoteEntry, warn }; |
@@ -7,2 +7,6 @@ export declare function safeWrapper<T extends (...args: Array<any>) => any>(callback: T, disableWarn?: boolean): Promise<ReturnType<T> | undefined>; | ||
}; | ||
export declare function createLink(url: string, cb: (value: void | PromiseLike<void>) => void, attrs?: Record<string, any>, createLinkHook?: (url: string) => HTMLLinkElement | void): { | ||
link: HTMLLinkElement; | ||
needAttach: boolean; | ||
}; | ||
export declare function loadScript(url: string, info: { | ||
@@ -9,0 +13,0 @@ attrs?: Record<string, any>; |
{ | ||
"name": "@module-federation/sdk", | ||
"version": "0.0.0-next-20240205081245", | ||
"version": "0.0.0-next-20240205095446", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "A sdk for support module federation", |
106674
2655