MCPcopy
hub / github.com/webpack/webpack / trimIdsToThoseImported

Function trimIdsToThoseImported

lib/util/chainedImports.js:73–99  ·  view source on GitHub ↗

* @summary Determine which IDs in the id chain are actually referring to namespaces or imports, * and which are deeper member accessors on the imported object. * @param {string[]} ids untrimmed ids * @param {ModuleGraph} moduleGraph moduleGraph * @param {Dependency} dependency dependency * @ret

(ids, moduleGraph, dependency)

Source from the content-addressed store, hash-verified

71 * @returns {string[]} trimmed ids
72 */
73function trimIdsToThoseImported(ids, moduleGraph, dependency) {
74 /** @type {string[] | undefined} */
75 let trimmedIds;
76 let currentExportsInfo = moduleGraph.getExportsInfo(
77 /** @type {Module} */ (moduleGraph.getModule(dependency))
78 );
79 for (let i = 0; i < ids.length; i++) {
80 if (i === 0 && ids[i] === "default") {
81 continue; // ExportInfo for the next level under default is still at the root ExportsInfo, so don't advance currentExportsInfo
82 }
83 const exportInfo = currentExportsInfo.getExportInfo(ids[i]);
84 if (exportInfo.provided === false) {
85 // json imports have nested ExportInfo for elements that things that are not actually exported, so check .provided
86 trimmedIds = ids.slice(0, i);
87 break;
88 }
89 const nestedInfo = exportInfo.getNestedExportsInfo();
90 if (!nestedInfo) {
91 // once all nested exports are traversed, the next item is the actual import so stop there
92 trimmedIds = ids.slice(0, i + 1);
93 break;
94 }
95 currentExportsInfo = nestedInfo;
96 }
97 // Never trim to nothing. This can happen for invalid imports (e.g. import { notThere } from "./module", or import { anything } from "./missingModule")
98 return trimmedIds !== undefined && trimmedIds.length ? trimmedIds : ids;
99}

Callers 1

chainedImports.jsFile · 0.85

Calls 5

getExportsInfoMethod · 0.80
sliceMethod · 0.80
getModuleMethod · 0.45
getExportInfoMethod · 0.45
getNestedExportsInfoMethod · 0.45

Tested by

no test coverage detected