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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: @babel/plugin-transform-runtime not transform Object.hasOwn correctly #15572

Closed
1 task done
Ashiria7 opened this issue Apr 17, 2023 · 4 comments 路 Fixed by #15531
Closed
1 task done

[Bug]: @babel/plugin-transform-runtime not transform Object.hasOwn correctly #15572

Ashiria7 opened this issue Apr 17, 2023 · 4 comments 路 Fixed by #15531
Labels
i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@Ashiria7
Copy link

Ashiria7 commented Apr 17, 2023

馃捇

  • Would you like to work on a fix?

How are you using Babel?

@babel/cli

Input code

const _ = function () {
    const obj = { a: 1}
    console.log(Object.hasOwn(obj, "b"));
    [].includes(1)
      
    const a = new Promise()
    return a
}
export default  _

console.log(_())

Configuration file name

babel.config.json

Configuration

{
  "plugins": [
    [
      "@babel/plugin-transform-runtime",
      {
        "corejs": 3
      }
    ]
  ]
}

Current and expected behavior

I used @babel/cli directly with the command ./node_modules/.bin/babel index.js --out-dir lib. This is the output:

import _includesInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/includes";
import _Promise from "@babel/runtime-corejs3/core-js-stable/promise";
const _ = function () {
  var _context;
  const obj = {
    a: 1
  };
  console.log(Object.hasOwn(obj, "b"));
  _includesInstanceProperty(_context = []).call(_context, 1);
  const a = new _Promise();
  return a;
};
export default _;
console.log(_());

The Promise and Array.includes are replace with @babel/runtime-corejs3 correctly. But Object.hasOwn not. And I find the @babel/runtime-corejs3/core-js-stable didn't exist the polyfill of Object.hasOwn which is realized in corejs. I have read the doc about @babel/plugin-transform-runtime, but I haven't found anything about this. So this behavior is designed so? Or I have missed some informations?

Environment

  System:
    OS: macOS 13.2
  Binaries:
    Node: 16.19.1 - ~/.nvm/versions/node/v16.19.1/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.19.1/bin/yarn
    npm: 8.19.3 - ~/.nvm/versions/node/v16.19.1/bin/npm
  npmPackages:
    @babel/cli: ^7.21.0 => 7.21.0
    @babel/core: ^7.21.4 => 7.21.4
    @babel/plugin-transform-runtime: ^7.21.4 => 7.21.4
    @babel/preset-env: ^7.21.4 => 7.21.4
    @babel/runtime: ^7.21.0 => 7.21.0
    @babel/runtime-corejs3: ^7.21.0 => 7.21.0

Possible solution

No response

Additional context

No response

@babel-bot
Copy link
Collaborator

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

@nicolo-ribaudo
Copy link
Member

"corejs": 3 means "3.0.0", and Object.hasOwn is only supported in more recent versions.

Unfortunately there is no good way yet to specify an higher version. You could update your config to this:

{
  "plugins": [
    "@babel/plugin-transform-runtime",
    ["babel-plugin-polyfill-corejs3", { "method": "usage-pure", "version": "3.30" }]
  ]
}

and add "core-js" to your package.json, so that it injects polyfills supoprted by core-js 3.30.

However, with that config Babel would inject imports to core-js and to @babel/runtime, and not @babel/runtime-corejs3, so the runtime helpers are not polyfilled and might not work in old browsers.

Once we have #15531 or something similar, the above config should properly injects imports to core-js and @babel/runtime-corejs3.

@Ashiria7
Copy link
Author

Ashiria7 commented Apr 17, 2023

Thanks a lot for your answer. But I still have a question. I find you said

However, with that config Babel would inject imports to core-js and to @babel/runtime, and not @babel/runtime-corejs3, so the runtime helpers are not polyfilled and might not work in old browsers.

Is this means the entry I input will polyfilled correctly, but code in @balbel/runtime will not be polyfilled, and some of them may not work in old browsers?

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Apr 17, 2023

Yes exactly. Most of them are ES5-compatible, but some of them use Symbol or Map.

@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 Aug 24, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: needs triage 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