Repository: webpack/webpack (JavaScript)

Issue:
Exported object names not minified since 5.79
### Discussed in https://github.com/webpack/webpack/discussions/17988

<div type='discussions-op-text'>

<sup>Originally posted by **vjhebbar** January 21, 2024</sup>
Hello,

Since I updated to webpack 5.79+ in my project, bundle sizes have increased because some exported object names are not minified.

It turns out that this is due to those objects being destructured in modules where they are imported. 
Refactoring the code to remove the object destructuring minimizes the name again. 

Prior to 5.79, the names were minified regardless of the use of destructuring. 

Is this behavior aligned with #16941?

If this is not expected behavior, I'll share my configs and minimum reproducible code.

Looking forward to your thoughts and suggestions.

The latest version where the issue is reproducible is 5.90.
</div>


Hints:
Repo: https://github.com/vjhebbar/webpack579test
The root cause is that canMangle is set to false in lib/dependencies/HarmonyImportDependencyParserPlugin.js. 
Should this flag be set according to the mangleExports optimization value?

Patch:
diff --git a/lib/dependencies/HarmonyImportSpecifierDependency.js b/lib/dependencies/HarmonyImportSpecifierDependency.js
--- a/lib/dependencies/HarmonyImportSpecifierDependency.js
+++ b/lib/dependencies/HarmonyImportSpecifierDependency.js
@@ -192,7 +192,7 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
 			for (const key of this.referencedPropertiesInDestructuring) {
 				refs.push({
 					name: ids ? ids.concat([key]) : [key],
-					canMangle: false
+					canMangle: Array.isArray(ids) && ids.length > 0
 				});
 			}
 			return refs;


Test patch:
diff --git a/test/configCases/mangle/mangle-with-destructuring-assignment/index.js b/test/configCases/mangle/mangle-with-destructuring-assignment/index.js
new file mode 100644
--- /dev/null
+++ b/test/configCases/mangle/mangle-with-destructuring-assignment/index.js
@@ -0,0 +1,21 @@
+import * as module from "./module";
+
+it("should not mangle export when destructuring module", () => {
+	const { obj: { a, b }, objCanMangle } = module
+	expect(a).toBe("a");
+	expect(b).toBe("b");
+	expect(objCanMangle).toBe(false)
+});
+
+it("should mangle export when destructuring module's porperty", () => {
+	const { a, b } = module.obj2
+	const { obj2CanMangle } = module
+	expect(a).toBe("a");
+	expect(b).toBe("b");
+	expect(obj2CanMangle).toBe(true)
+});
+
+it("should mangle export when using module dot property", () => {
+	expect(module.aaa).toBe("aaa");
+	expect(module.aaaCanMangle).toBe(true)
+});
diff --git a/test/configCases/mangle/mangle-with-destructuring-assignment/module.js b/test/configCases/mangle/mangle-with-destructuring-assignment/module.js
new file mode 100644
--- /dev/null
+++ b/test/configCases/mangle/mangle-with-destructuring-assignment/module.js
@@ -0,0 +1,8 @@
+export const aaa = "aaa";
+export const aaaCanMangle = __webpack_exports_info__.aaa.canMangle;
+
+export const obj = { a: "a", b: "b" }
+export const objCanMangle = __webpack_exports_info__.obj.canMangle;
+
+export const obj2 = { a: "a", b: "b" }
+export const obj2CanMangle = __webpack_exports_info__.obj2.canMangle;
diff --git a/test/configCases/mangle/mangle-with-destructuring-assignment/webpack.config.js b/test/configCases/mangle/mangle-with-destructuring-assignment/webpack.config.js
new file mode 100644
--- /dev/null
+++ b/test/configCases/mangle/mangle-with-destructuring-assignment/webpack.config.js
@@ -0,0 +1,8 @@
+/** @type {import("../../../../").Configuration} */
+module.exports = {
+	optimization: {
+		mangleExports: true,
+		usedExports: true,
+		providedExports: true
+	}
+};