Repository: babel/babili
Pull request #518 (2017-05-04 20:41:55)

Fix mangling of Named Exports in Modules
+ Ref #479

This completes one part of the problem where now Named exports are no
more mangled. But the other part, where we can convert

```js
const foo = bar;
export {foo};
```

to

```js
export const foo = bar;
```

is yet to be explored in DCE or other plugins.

codecov[bot]: # [Codecov](https://codecov.io/gh/babel/babili/pull/518?src=pr&el=h1) Report
> Merging [#518](https://codecov.io/gh/babel/babili/pull/518?src=pr&el=desc) into [master](https://codecov.io/gh/babel/babili/commit/713fb8f56529da6dc02b51357f13b7640d20996f?src=pr&el=desc) will **increase** coverage by `0.14%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/babel/babili/pull/518/graphs/tree.svg?width=650&token=yjfidKkIaL&height=150&src=pr)](https://codecov.io/gh/babel/babili/pull/518?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #518      +/-   ##
==========================================
+ Coverage   82.96%   83.11%   +0.14%     
==========================================
  Files          34       34              
  Lines        2524     2570      +46     
  Branches      897      922      +25     
==========================================
+ Hits         2094     2136      +42     
- Misses        257      258       +1     
- Partials      173      176       +3
```


| [Impacted Files](https://codecov.io/gh/babel/babili/pull/518?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [...ages/babel-plugin-minify-mangle-names/src/index.js](https://codecov.io/gh/babel/babili/pull/518?src=pr&el=tree#diff-cGFja2FnZXMvYmFiZWwtcGx1Z2luLW1pbmlmeS1tYW5nbGUtbmFtZXMvc3JjL2luZGV4Lmpz) | `77.64% <100%> (+1.1%)` | :arrow_up: |
| [packages/babel-plugin-minify-simplify/src/index.js](https://codecov.io/gh/babel/babili/pull/518?src=pr&el=tree#diff-cGFja2FnZXMvYmFiZWwtcGx1Z2luLW1pbmlmeS1zaW1wbGlmeS9zcmMvaW5kZXguanM=) | `80.9% <0%> (+0.4%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/babel/babili/pull/518?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/babel/babili/pull/518?src=pr&el=footer). Last update [713fb8f...37878a5](https://codecov.io/gh/babel/babili/pull/518?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/packages/babel-plugin-minify-mangle-names/src/index.js b/packages/babel-plugin-minify-mangle-names/src/index.js
index a185869..a68021f 100644
--- a/packages/babel-plugin-minify-mangle-names/src/index.js
+++ b/packages/babel-plugin-minify-mangle-names/src/index.js
@@ -88,6 +88,24 @@ module.exports = ({ types: t, traverse }) => {
       }
     }
 
+    isExportedWithName(binding) {
+      // short circuit
+      if (!this.topLevel) {
+        return false;
+      }
+
+      const refs = binding.referencePaths;
+
+      for (const ref of refs) {
+        if (ref.isExportNamedDeclaration()) {
+          return true;
+        }
+      }
+
+      // default
+      return false;
+    }
+
     mangleScope(scope) {
       const mangler = this;
 
@@ -139,7 +157,9 @@ module.exports = ({ types: t, traverse }) => {
           // function names
           (mangler.keepFnName ? isFunction(binding.path) : false) ||
           // class names
-          (mangler.keepClassName ? isClass(binding.path) : false)
+          (mangler.keepClassName ? isClass(binding.path) : false) ||
+          // named export
+          mangler.isExportedWithName(binding)
         ) {
           continue;
         }


Test patch:
diff --git a/packages/babel-plugin-minify-mangle-names/__tests__/mangle-names-test.js b/packages/babel-plugin-minify-mangle-names/__tests__/mangle-names-test.js
index 44e117e..2299ee5 100644
--- a/packages/babel-plugin-minify-mangle-names/__tests__/mangle-names-test.js
+++ b/packages/babel-plugin-minify-mangle-names/__tests__/mangle-names-test.js
@@ -1325,4 +1325,45 @@ describe("mangle-names", () => {
     );
     expect(transform(source, { topLevel: true })).toBe(source);
   });
+
+  it("should not mangle named exports - 1", () => {
+    const source = unpad(
+      `
+      export const Foo = foo;
+    `
+    );
+    expect(transform(source, { topLevel: true }, "module")).toBe(source);
+  });
+
+  it("should not mangle named exports - 2", () => {
+    const source = unpad(
+      `
+      const Foo = a;
+      export {Foo};
+    `
+    );
+    const expected = unpad(
+      `
+      const b = a;
+      export { b as Foo };
+    `
+    );
+    expect(transform(source, { topLevel: true }, "module")).toBe(expected);
+  });
+
+  it("should not mangle named exports - 3", () => {
+    const source = unpad(
+      `
+      const Foo = a;
+      export {Foo as Bar};
+    `
+    );
+    const expected = unpad(
+      `
+      const b = a;
+      export { b as Bar };
+    `
+    );
+    expect(transform(source, { topLevel: true }, "module")).toBe(expected);
+  });
 });