Minify: Exporting constants breaks minification

Created on 14 Apr 2018  路  3Comments  路  Source: babel/minify

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

Most helpful comment

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:

All 3 comments

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:

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).

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

Was this page helpful?
0 / 5 - 0 ratings