Issue 1:
NOTE: this seems to be fixed in 7.x either intentionally or from refactorings. I don't know the maintenance status of 6.x, so I'm opening this anyhow.

Choose one: is this a bug report or feature request? Bug

Input Code

Repro repo: https://github.com/FLGMwt/babel-cjs-bug

export const withExport = () => null;
const withoutExport = () => null;

console.log({
withExport: withExport.name, // should be 'withExport', is ''
withoutExport: withoutExport.name, // should be 'withoutExport', *is* 'withoutExport'
});

Babel/Babylon Configuration (.babelrc, package.json, cli command)

{
"plugins": ["transform-es2015-modules-commonjs"]
}

{
"name": "babel-export-bug",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"babel-core": "^6.26.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0"
}
}

babel test.js | node

Expected Behavior

Output of babel test.js | node should be

{ withExport: 'withExport', withoutExport: 'withoutExport' }

Arrow functions should have their .name property defined by declarations, as demonstrated with the withoutExport function name.

Current Behavior

{ withExport: '', withoutExport: 'withoutExport' }

The arrow function that is also exported at the time of declaration is not given its name.

This seems to be because the output of the transform disassociates the arrow function from the variable declaration by injecting a cascading assignment to the module-wrapping code:

Object.defineProperty(exports, "__esModule", {
value: true
});
const withExport = exports.withExport = () => null;
const withoutExport = () => null;

and I guess this turns the arrow function into an anonymous one.

Possible Solution

Possible solutions:

Separate variable declaration from the exports.withExport assignment

Explicitly assign the .name property to the function in a separate statement

Don't fix and wait for babel 7.x

If we're capable of fixing this in 6.x, I'm happy to give it a shot : )

Context

I was writing a tutorial for testing React apps using Enzyme. Building on create-react-app, I made some simple, stateless functional components and immediately exported them link. Enzyme uses a jQuery-like selector syntax for traversing a DOM-like structure which accepts string keys for components. React derives these keys/component names from function names, which in this case was missing. When I try to find these components in the test like this, I can't. (shows up as <Component /> instead of <SpyListItem />)

It's worth pointing out that this is only a problem for node-based unit tests. Dev tools use the same names to identify components, but those work in the browser usually since the env preset transforms arrow functions into named functions. The create-react-app config uses node: 'current', which, unless current is <=5.12, leaves arrows alone. After opening this, I'm gonna poke around that repo to see about pinning down the node env target.

I can work around this by explicitly naming the component, naming the function, querying by object reference, or exporting differently, but since I'm trying to reach a beginner audience, I don't want to have to do anything exceptional.

Your Environment

software
version(s)

Babel
^6.26.0

node
9.3.0

npm
5.6.0

Operating System
OSX

Issue 2:
Choose one: is this a bug report or feature request?

Bug

Input Code

export const f1 = function () {};
export const f2 = () => {};

Babel Configuration (.babelrc, package.json, cli command)

--plugins transform-es2015-modules-commonjs

Expected Behavior

To preserve inferred names of anonymous functions, I would expect:

// ...
const f1 = function () {};
exports.f1 = f1;
const f2 = () => {};
exports.f2 = f2;
// f1.name === 'f1'
// f2.name === 'f2'

Current Behavior

// ...
const f1 = exports.f1 = function () {};
const f2 = exports.f2 = () => {};
// f1.name === ''
// f2.name === ''

Context

Especially for arrow functions (since there is no explicitly named alternative), it would be very helpful to preserve the inferred function name.

Your Environment

software
version(s)

Babel
6.24.1

node
7.3.0

npm
3.8.9

Operating System
Windows