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]: When ESM is converted to CJS, the execution timing of self-references is incorrect #16834

Open
1 task
magic-akari opened this issue Sep 13, 2024 · 1 comment
Labels

Comments

@magic-akari
Copy link
Contributor

magic-akari commented Sep 13, 2024

💻

  • Would you like to work on a fix?

How are you using Babel?

Other (Next.js, Gatsby, vue-cli, ...)

Input code

// self.mjs
import { a } from "./self.mjs";

export const [b, c = a] = [2];

export { b as a }

console.log(a, b, c);

Configuration file name

No response

Configuration

N/A

Current and expected behavior

output code:

For ease of direct execution, the filename has been renamed to self.cjs.

// self.cjs
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.c = exports.b = exports.a = void 0;
var _self = require("./self.cjs");
const [b, c = _self.a] = [2];
exports.c = c;
exports.a = exports.b = b;
console.log(_self.a, b, c);

Expected behavior: print 2 2 2
Current behavior: print 2 2 undefined

Environment

N/A

Possible solution

Two optional solutions:

  1. Downgrade destructuring syntax before converting ESM to CJS
  2. Use the define getter approach like SWC/ESBuild (click the link to see playground)

Additional context

No response

@babel-bot
Copy link
Collaborator

Hey @magic-akari! 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants