MCPcopy
hub / github.com/webpack/webpack / getSourceForScriptExternal

Function getSourceForScriptExternal

lib/ExternalModule.js:666–696  ·  view source on GitHub ↗
(urlAndGlobal, runtimeTemplate)

Source from the content-addressed store, hash-verified

664 * @returns {SourceData} the generated source
665 */
666const getSourceForScriptExternal = (urlAndGlobal, runtimeTemplate) => {
667 if (typeof urlAndGlobal === "string") {
668 urlAndGlobal = extractUrlAndGlobal(urlAndGlobal);
669 }
670 const url = urlAndGlobal[0];
671 const globalName = urlAndGlobal[1];
672 return {
673 init: `${runtimeTemplate.renderConst()} __webpack_error__ = new Error();`,
674 expression: `new Promise(${runtimeTemplate.basicFunction(
675 "resolve, reject",
676 [
677 `if(typeof ${globalName} !== "undefined") return resolve();`,
678 `${RuntimeGlobals.loadScript}(${JSON.stringify(
679 url
680 )}, ${runtimeTemplate.basicFunction("event", [
681 `if(typeof ${globalName} !== "undefined") return resolve();`,
682 `${runtimeTemplate.renderConst()} errorType = event && (event.type === 'load' ? 'missing' : event.type);`,
683 `${runtimeTemplate.renderConst()} realSrc = event && event.target && event.target.src;`,
684 "__webpack_error__.message = 'Loading script failed.\\n(' + errorType + ': ' + realSrc + ')';",
685 "__webpack_error__.name = 'ScriptExternalLoadError';",
686 "__webpack_error__.type = errorType;",
687 "__webpack_error__.request = realSrc;",
688 "reject(__webpack_error__);"
689 ])}, ${JSON.stringify(globalName)});`
690 ]
691 )}).then(${runtimeTemplate.returningFunction(
692 `${globalName}${propertyAccess(urlAndGlobal, 2)}`
693 )})`,
694 runtimeRequirements: RUNTIME_REQUIREMENTS_FOR_SCRIPT
695 };
696};
697
698/**
699 * Checks external variable.

Callers 1

_getSourceDataMethod · 0.85

Calls 4

propertyAccessFunction · 0.85
renderConstMethod · 0.80
basicFunctionMethod · 0.80
returningFunctionMethod · 0.80

Tested by

no test coverage detected