Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Exporting constants breaks minification #820

Closed
bashlyss opened this issue Apr 13, 2018 · 3 comments · Fixed by #828
Closed

Exporting constants breaks minification #820

bashlyss opened this issue Apr 13, 2018 · 3 comments · Fixed by #828

Comments

@bashlyss
Copy link

bashlyss commented Apr 13, 2018

Input Code

 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

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

@aomarks
Copy link

aomarks commented Apr 19, 2018

I am also encountering this problem. I believe the issue may be that the minify-constant-folding plugin is substituting the constant value directly into the export specifier. This is invalid syntax, because ExportSpecifiers must be IdentifierNames: https://tc39.github.io/ecma262/#sec-exports.

To reproduce, run:

const babel = require('@babel/core');
const foldingPlugin = require('babel-plugin-minify-constant-folding')

const js = `const foo = "foo"; export { foo };`;
babel.transformSync(js, {plugins: [foldingPlugin]});

Which throws:

TypeError: Property local of ExportSpecifier expected node to be of a type ["Identifier"] but instead got "StringLiteral"

The correct behavior would be not to perform identifier substitutions inside ExportSpecifiers.

Versions:

  • @babel/core@7.0.0-beta.44
  • babel-plugin-minify-constant-folding@0.4.0

@aomarks
Copy link

aomarks commented Apr 19, 2018

FYI if you're using babel-preset-minify you can turn off this plugin by setting evaluate: false (https://github.com/babel/minify/tree/master/packages/babel-preset-minify#options).

@bashlyss
Copy link
Author

@aomarks Thanks! Not sure how I completely missed that documentation originally.

aomarks added a commit to Polymer/tools that referenced this issue Apr 19, 2018
Disable Babel `minify-constant-folding` plugin when minifying. This
plugin has a bug that breaks when a constant is exported from a module
(babel/minify#820).

Fixes https://github.com/Polymer/polymer-cli/issues/998
aomarks added a commit to Polymer/tools that referenced this issue Apr 20, 2018
Disable Babel `minify-constant-folding` plugin when minifying. This
plugin has a bug that breaks when a constant is exported from a module
(babel/minify#820).

Fixes https://github.com/Polymer/polymer-cli/issues/998
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants