The babel compiler exports undefined and actual export together

Can someone explain why Babel compiles the following:

import {resolve} from "path";
export const exportedConst = "value";

to:

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.exportedConst = undefined;

var _path = require("path");

var exportedConst = exports.exportedConst = "value";

and if I exported exportedConstwithout importing any other modules:

export const exportedConst = "value";

he compiles it:

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
var exportedConst = exports.exportedConst = "value";

Why is he creating this line? export.exportedConst = undefined;

and export it as follows

var exportedConst = exports.exportedConst = "value";

this makes the IDE see two exports

enter image description here

This may be an intelliJ problem, but it made me wonder why babel does it this way.

I use:

  • nodejs: v8.5.0
  • babel-cli: 6.26.0 (babel-core 6.26.0)
  • babel-preset-env: 1.6.1

Try here

or if you want to play it locally, run a quick one-line command

mkdir stackoverflow-questin && cd stackoverflow-questin && npm init -y && npm install babel-cli babel-preset-env && echo 'import {resolve} from "path";\nexport const exportedConst = "value";' > index.js && npx babel index.js --out-file index.compiled.js --presets=env
+4
source share

Source: https://habr.com/ru/post/1689334/


All Articles