MCPcopy
hub / github.com/webpack/webpack / getSourceForImportExternal

Function getSourceForImportExternal

lib/ExternalModule.js:275–331  ·  view source on GitHub ↗
(
	moduleAndSpecifiers,
	runtimeTemplate,
	dependencyMeta
)

Source from the content-addressed store, hash-verified

273 * @returns {SourceData} the generated source
274 */
275const getSourceForImportExternal = (
276 moduleAndSpecifiers,
277 runtimeTemplate,
278 dependencyMeta
279) => {
280 const baseImportName = runtimeTemplate.outputOptions.importFunctionName;
281 if (
282 !runtimeTemplate.supportsDynamicImport() &&
283 (baseImportName === "import" || baseImportName === "module-import")
284 ) {
285 throw new Error(
286 "The target environment doesn't support 'import()' so it's not possible to use external type 'import'"
287 );
288 }
289 const phase = dependencyMeta && dependencyMeta.phase;
290 // `import.defer(…)` and `import.source(…)` are only valid forms of the
291 // native `import(…)` function, so we only emit the phase suffix when the
292 // importFunctionName is the default `"import"`.
293 const importName =
294 baseImportName === "import" && ImportPhaseUtils.isDefer(phase)
295 ? "import.defer"
296 : baseImportName === "import" && ImportPhaseUtils.isSource(phase)
297 ? "import.source"
298 : baseImportName;
299 const attributes =
300 dependencyMeta && dependencyMeta.attributes
301 ? dependencyMeta.attributes._isLegacyAssert
302 ? `, { assert: ${JSON.stringify(
303 dependencyMeta.attributes,
304 importAssertionReplacer
305 )} }`
306 : `, { with: ${JSON.stringify(dependencyMeta.attributes)} }`
307 : "";
308 if (!Array.isArray(moduleAndSpecifiers)) {
309 return {
310 expression: `${importName}(${JSON.stringify(
311 moduleAndSpecifiers
312 )}${attributes});`
313 };
314 }
315 if (moduleAndSpecifiers.length === 1) {
316 return {
317 expression: `${importName}(${JSON.stringify(
318 moduleAndSpecifiers[0]
319 )}${attributes});`
320 };
321 }
322 const moduleName = moduleAndSpecifiers[0];
323 return {
324 expression: `${importName}(${JSON.stringify(
325 moduleName
326 )}${attributes}).then(${runtimeTemplate.returningFunction(
327 `module${propertyAccess(moduleAndSpecifiers, 1)}`,
328 "module"
329 )});`
330 };
331};
332

Callers 1

_getSourceDataMethod · 0.85

Calls 4

propertyAccessFunction · 0.85
supportsDynamicImportMethod · 0.80
isArrayMethod · 0.80
returningFunctionMethod · 0.80

Tested by

no test coverage detected