MCPcopy
hub / github.com/webpack/webpack / generate

Method generate

lib/runtime/LoadScriptRuntimeModule.js:38–155  ·  view source on GitHub ↗

* Generates runtime code for this runtime module. * @returns {string | null} runtime code

()

Source from the content-addressed store, hash-verified

36 * @returns {string | null} runtime code
37 */
38 generate() {
39 const compilation = /** @type {Compilation} */ (this.compilation);
40 const { runtimeTemplate, outputOptions } = compilation;
41 const {
42 scriptType,
43 chunkLoadTimeout: loadTimeout,
44 crossOriginLoading,
45 uniqueName,
46 charset
47 } = outputOptions;
48 const fn = RuntimeGlobals.loadScript;
49
50 const { createScript } =
51 LoadScriptRuntimeModule.getCompilationHooks(compilation);
52
53 const code = Template.asString([
54 "script = document.createElement('script');",
55 scriptType ? `script.type = ${JSON.stringify(scriptType)};` : "",
56 charset ? "script.charset = 'utf-8';" : "",
57 `if (${RuntimeGlobals.scriptNonce}) {`,
58 Template.indent(
59 `script.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});`
60 ),
61 "}",
62 uniqueName
63 ? 'script.setAttribute("data-webpack", dataWebpackPrefix + key);'
64 : "",
65 this._withFetchPriority
66 ? Template.asString([
67 "if(fetchPriority) {",
68 Template.indent(
69 'script.setAttribute("fetchpriority", fetchPriority);'
70 ),
71 "}"
72 ])
73 : "",
74 `script.src = ${
75 this._withCreateScriptUrl
76 ? `${RuntimeGlobals.createScriptUrl}(url)`
77 : "url"
78 };`,
79 crossOriginLoading
80 ? crossOriginLoading === "use-credentials"
81 ? 'script.crossOrigin = "use-credentials";'
82 : Template.asString([
83 "if (script.src.indexOf(window.location.origin + '/') !== 0) {",
84 Template.indent(
85 `script.crossOrigin = ${JSON.stringify(crossOriginLoading)};`
86 ),
87 "}"
88 ])
89 : ""
90 ]);
91
92 const cst = runtimeTemplate.renderConst();
93 const lt = runtimeTemplate.renderLet();
94 return Template.asString([
95 `${cst} inProgress = {};`,

Callers

nothing calls this directly

Calls 9

indentMethod · 0.80
renderConstMethod · 0.80
renderLetMethod · 0.80
basicFunctionMethod · 0.80
optionalChainingMethod · 0.80
returningFunctionMethod · 0.80
getCompilationHooksMethod · 0.45
asStringMethod · 0.45
callMethod · 0.45

Tested by

no test coverage detected