Repository: babel/minify (js)

Issue:
Exporting constants breaks minification
<!---
Thanks for filing an issue 😄! Before you submit, please read the following:

Search open/closed issues before submitting since someone might have asked the same thing before!

If you have a support request or question please submit them to the #minify channel on babeljs slack: https://babeljs.slack.com/messages/minify

Also have a look at the Debugging guidelines: https://github.com/babel/minify/blob/master/CONTRIBUTING.md#debugging

Things required:

For a bug:

1. Minimal code to reproduce the bug
2. Stack Trace if there is an Error thrown during minification
3. Configuration - babel-minify configuration or babelrc
4. Whether you used it with other presets/plugins - like es2015 or env
5. Environment: gulp, rollup, webpack, babel-cli, etc...?

For an enhancement:

1. Description
2. Code Examples with transformations

-->

#### Input Code

```js
 const ONE_SECOND = 1000;
 const ESCAPE_KEY = 27;
 const LEFT_ARROW_KEY = 37;
 const RIGHT_ARROW_KEY = 39;
  
 export {
   ONE_SECOND,
   ESCAPE_KEY,
   LEFT_ARROW_KEY,
   RIGHT_ARROW_KEY,
 };
```

#### Actual Output
<!-- If an error is thrown, please provide the stack trace here -->

```
ERROR in ./zanbato/frontend/utils/Constants.js
    Module build failed: TypeError: /utils/Constants.js: Property local of ExportSpecifier expected node to be of a type ["Identifier"] but instead got "NumericLiteral"
        at Object.validate (node_modules/babel-types/lib/definitions/index.js:109:13)
        at Object.validate (/node_modules/babel-types/lib/index.js:505:9)
        at NodePath._replaceWith (node_modules/babel-traverse/lib/path/replacement.js:176:7)
        at NodePath.replaceWith (node_modules/babel-traverse/lib/path/replacement.js:160:8)
        at PluginPass.Expression (node_modules/babel-plugin-minify-constant-folding/lib/index.js:199:16)
        at PluginPass.newFn (node_modules/babel-traverse/lib/visitors.js:318:17)
        at newFn (node_modules/babel-traverse/lib/visitors.js:276:21)
        at NodePath._call (node_modules/babel-traverse/lib/path/context.js:76:18)
        at NodePath.call (node_modules/babel-traverse/lib/path/context.js:48:17)
        at NodePath.visit (node_modules/babel-traverse/lib/path/context.js:105:12)
        ...
        at transpile (node_modules/babel-loader/lib/index.js:50:20)
        at Object.module.exports (node_modules/babel-loader/lib/index.js:173:20)
```

#### Expected Output

I should be able to export my constants without compilation issues

#### Details

If there is another way I should be defining this, or a way I can use the preset and omit the constant folding, that would work as an interim solution.

EDIT: Removed some of the verbosity of the backtrace for easier reading


Pull request:
fix(const-folding): do not evaluate identifier in export specifiers
+ fix #820 

Patch:
diff --git a/packages/babel-plugin-minify-constant-folding/src/index.js b/packages/babel-plugin-minify-constant-folding/src/index.js
index 8deadd3..7276456 100644
--- a/packages/babel-plugin-minify-constant-folding/src/index.js
+++ b/packages/babel-plugin-minify-constant-folding/src/index.js
@@ -102,7 +102,7 @@ module.exports = babel => {
 
       // TODO: look into evaluating binding too (could result in more code, but gzip?)
       Expression(path, { opts: { tdz = false } = {} }) {
-        const { node } = path;
+        const { node, parent } = path;
 
         if (node[seen]) {
           return;
@@ -116,6 +116,11 @@ module.exports = babel => {
           return;
         }
 
+        // Avoid replacing the values for identifiers in exports
+        if (t.isExportSpecifier(parent)) {
+          return;
+        }
+
         // -0 maybe compared via dividing and then checking against -Infinity
         // Also -X will always be -X.
         if (


Test patch:
diff --git a/packages/babel-plugin-minify-constant-folding/__tests__/fixtures/export-specifier/actual.js b/packages/babel-plugin-minify-constant-folding/__tests__/fixtures/export-specifier/actual.js
new file mode 100644
index 0000000..6091bf8
--- /dev/null
+++ b/packages/babel-plugin-minify-constant-folding/__tests__/fixtures/export-specifier/actual.js
@@ -0,0 +1,3 @@
+const a = "a";
+const b = "b";
+export { a, b };
diff --git a/packages/babel-plugin-minify-constant-folding/__tests__/fixtures/export-specifier/babel.json b/packages/babel-plugin-minify-constant-folding/__tests__/fixtures/export-specifier/babel.json
new file mode 100644
index 0000000..2104ca4
--- /dev/null
+++ b/packages/babel-plugin-minify-constant-folding/__tests__/fixtures/export-specifier/babel.json
@@ -0,0 +1,3 @@
+{
+  "sourceType": "module"
+}
diff --git a/packages/babel-plugin-minify-constant-folding/__tests__/fixtures/export-specifier/expected.js b/packages/babel-plugin-minify-constant-folding/__tests__/fixtures/export-specifier/expected.js
new file mode 100644
index 0000000..81fa6ed
--- /dev/null
+++ b/packages/babel-plugin-minify-constant-folding/__tests__/fixtures/export-specifier/expected.js
@@ -0,0 +1,3 @@
+const a = "a";
+const b = "b";
+export { a, b };
\ No newline at end of file