MCPcopy
hub / github.com/webpack/webpack / createObjectForExportsInfo

Function createObjectForExportsInfo

lib/json/JsonGenerator.js:53–124  ·  view source on GitHub ↗
(data, exportsInfo, runtime)

Source from the content-addressed store, hash-verified

51 * @returns {JsonObject | JsonArray} reduced data
52 */
53const createObjectForExportsInfo = (data, exportsInfo, runtime) => {
54 if (exportsInfo.otherExportsInfo.getUsed(runtime) !== UsageState.Unused) {
55 return data;
56 }
57 const isArray = Array.isArray(data);
58 /** @type {JsonObject | JsonArray} */
59 const reducedData = isArray ? [] : {};
60 for (const key of Object.keys(data)) {
61 const exportInfo = exportsInfo.getReadOnlyExportInfo(key);
62 const used = exportInfo.getUsed(runtime);
63 if (used === UsageState.Unused) continue;
64
65 // The real type is `JsonObject | JsonArray`, but typescript doesn't work `Object.keys(['string', 'other-string', 'etc'])` properly
66 const newData = /** @type {JsonObject} */ (data)[key];
67 const value =
68 used === UsageState.OnlyPropertiesUsed &&
69 exportInfo.exportsInfo &&
70 typeof newData === "object" &&
71 newData
72 ? createObjectForExportsInfo(newData, exportInfo.exportsInfo, runtime)
73 : newData;
74
75 const name = /** @type {string} */ (exportInfo.getUsedName(key, runtime));
76 /** @type {JsonObject} */
77 (reducedData)[name] = value;
78 }
79 if (isArray) {
80 const arrayLengthWhenUsed =
81 exportsInfo.getReadOnlyExportInfo("length").getUsed(runtime) !==
82 UsageState.Unused
83 ? data.length
84 : undefined;
85
86 let sizeObjectMinusArray = 0;
87 const reducedDataLength =
88 /** @type {JsonArray} */
89 (reducedData).length;
90 for (let i = 0; i < reducedDataLength; i++) {
91 if (/** @type {JsonArray} */ (reducedData)[i] === undefined) {
92 sizeObjectMinusArray -= 2;
93 } else {
94 sizeObjectMinusArray += `${i}`.length + 3;
95 }
96 }
97 if (arrayLengthWhenUsed !== undefined) {
98 sizeObjectMinusArray +=
99 `${arrayLengthWhenUsed}`.length +
100 8 -
101 (arrayLengthWhenUsed - reducedDataLength) * 2;
102 }
103 if (sizeObjectMinusArray < 0) {
104 return Object.assign(
105 arrayLengthWhenUsed === undefined
106 ? {}
107 : { length: arrayLengthWhenUsed },
108 reducedData
109 );
110 }

Callers 1

generateMethod · 0.85

Calls 5

isArrayMethod · 0.80
keysMethod · 0.65
getUsedMethod · 0.45
getReadOnlyExportInfoMethod · 0.45
getUsedNameMethod · 0.45

Tested by

no test coverage detected