Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[Bug]: the order of exports is incorrect when converting ESM to CJS #13785

Closed
1 task
evanw opened this issue Sep 23, 2021 · 1 comment · Fixed by #13788
Closed
1 task

[Bug]: the order of exports is incorrect when converting ESM to CJS #13785

evanw opened this issue Sep 23, 2021 · 1 comment · Fixed by #13788
Assignees
Labels
area: modules i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@evanw
Copy link

evanw commented Sep 23, 2021

💻

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

export let a = 1
export let c = 3
export let b = 2

Configuration file name

No response

Configuration

import * as babel from '@babel/core';
import esm2cjs from '@babel/plugin-transform-modules-commonjs';
output = babel.transformSync(input, { plugins: [esm2cjs] }).code;

Current and expected behavior

Babel generates this code when converting ESM to CommonJS:

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.b = exports.c = exports.a = void 0;
let a = 1;
exports.a = a;
let c = 3;
exports.c = c;
let b = 2;
exports.b = b;

However, the order of exports is incorrect and does not match what native ESM does. For example, ESM code that uses Object.keys() on this file will behave differently when converted to CommonJS. The order should be ['a', 'b', 'c'] but Babel gives an order of ['a', 'c', 'b'] instead. According to the specification, exports must be sorted:

Table 29 — Internal Slots of Module Namespace Exotic Objects

Internal Slot Type Description
[[Module]] Module Record The Module Record whose exports this namespace exposes.
[[Exports]] List of String A List containing the String values of the exported names exposed as own properties of this object. The list is ordered as if an Array of those String values had been sorted using Array.prototype.sort using SortCompare as comparefn.

Environment

My package.json file:

{
  "dependencies": {
    "@babel/core": "7.15.5",
    "@babel/plugin-transform-modules-commonjs": "7.15.4"
  }
}

Possible solution

I believe Babel should generate exports.c = exports.b = exports.a = void 0; instead of exports.b = exports.c = exports.a = void 0; here.

Additional context

No response

@babel-bot
Copy link
Collaborator

Hey @evanw! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@JLHwung JLHwung self-assigned this Sep 23, 2021
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Dec 27, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: modules i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants