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

Babel targets don't recognize Node.js 16 #13913

Closed
nicolo-ribaudo opened this issue Nov 1, 2021 Discussed in #13912 · 2 comments · Fixed by #13914
Closed

Babel targets don't recognize Node.js 16 #13913

nicolo-ribaudo opened this issue Nov 1, 2021 Discussed in #13912 · 2 comments · Fixed by #13914
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@nicolo-ribaudo
Copy link
Member

Discussed in #13912

Why does Babel transpile async functions to generators via _asyncToGenerator if target supports async/await?

Originally posted by nlucero November 1, 2021
I'm trying to understand why my transpiled code transforms regular async functions to generators via _asyncToGenerator if the compilation target already supports async functions.

I'm using @babel/preset-env and targeting node >=16.0.0 via targets. This is my config file:

module.exports = {
  presets: [
    [
      '@babel/preset-env',
      {
        targets: {
          browsers: `node >=16.0.0`,
        },
      },
    ],
  ],
  plugins: [
    ['@babel/plugin-proposal-decorators', { legacy: true }],
    ['@babel/plugin-proposal-private-methods', { loose: true }],
    ['@babel/plugin-proposal-class-properties', { loose: true }],
    [
      'module-resolver',
      {
        root: ['./src'],
      },
    ],
  ],
}

If I inspect my transpiled code, I see my async functions are transpiled to something like this:

var login = /*#__PURE__*/function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(req, res) {
    var _req$body, username, password, token, name, cookieConfig;

    return regeneratorRuntime.wrap(function _callee$(_context) {
      // Implementation here
    }, _callee);
  }));

  return function login(_x, _x2) {
    return _ref.apply(this, arguments);
  };
}();

This seems unnecessary to me since my transpilation target (node v16.x.x) fully supports async functions. Is there a good reason why this is being done this way? Could it be a misconfiguration on my end?

For more context, the transpiled code uses native async functions (no _asyncToGenerator whatsoever) if I set my target to node>=14.0.0, so this might be a regression introduced recently.

@nicolo-ribaudo
Copy link
Member Author

When targeting node 16.0.0, Babel complains that it's not a known Node.js version. When targeting node >=14.0.0, Babel doesn't transpile async functions.

@nicolo-ribaudo
Copy link
Member Author

Oh thanks @JLHwung; the reason is #13912 (comment).

@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 Feb 2, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
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.

1 participant