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 attempts to use wrong runtime helpers in version 7.22 #15663

Open
1 task
fire332 opened this issue May 26, 2023 · 6 comments
Open
1 task

[Bug]: Babel attempts to use wrong runtime helpers in version 7.22 #15663

fire332 opened this issue May 26, 2023 · 6 comments
Labels

Comments

@fire332
Copy link

fire332 commented May 26, 2023

馃捇

  • Would you like to work on a fix?

How are you using Babel?

babel-loader (webpack)

Input code

export async function x() {
return 1
}

Configuration file name

babel.config.js

Configuration

module.exports = (api) => {
  api.cache.never();

  return {
    // Fixes "TypeError: __webpack_require__(...) is not a function"
    // https://github.com/webpack/webpack/issues/9379#issuecomment-509628205
    // https://babeljs.io/docs/en/options#sourcetype
    sourceType: 'unambiguous',
    plugins: [
      [
        '@babel/plugin-transform-runtime',
        {
          regenerator: false,
          version: require('./package.json').dependencies['@babel/runtime']
        }
      ],
      [
        'polyfill-corejs3',
        {
          method: 'usage-pure'
        }
      ],
      [
        'polyfill-regenerator',
        {
          method: 'usage-pure'
        }
      ]
    ],
    presets: [
      [
        '@babel/preset-env',
        {
          bugfixes: true
        }
      ]
    ]
  };
};

Current and expected behavior

Current behavior: log spam of Can't resolve '@babel/runtime-corejs3/helpers/xxx' in 'yyy'

Expected: helpers are resolved into @babel/runtime instead of @babel/runtime-corejs3

Environment

Last known good babel versions:

  • System:
    • OS: Windows 10 10.0.19045
  • Binaries:
    • Node: 18.16.0 - C:\Program Files\nodejs\node.EXE
    • npm: 9.5.1 - C:\Program Files\nodejs\npm.CMD
      npmPackages:
    • @babel/core: 7.21.8 => 7.21.8
    • @babel/plugin-transform-runtime: 7.21.4 => 7.21.4
    • @babel/preset-env: 7.21.5 => 7.21.5
    • @babel/runtime: 7.21.5 => 7.21.5
    • babel-loader: ^9.1.2 => 9.1.2
    • babel-plugin-polyfill-corejs3: ^0.8.1 => 0.8.1
    • babel-plugin-polyfill-regenerator: ^0.5.0 => 0.5.0
    • webpack: ^5.84.1 => 5.84.1

Possible solution

No response

Additional context

This wasn't an issue in 7.21

@babel-bot
Copy link
Collaborator

Hey @fire332! 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

Can you share your package.json?

@fire332
Copy link
Author

fire332 commented May 26, 2023

Working babel versions from minimal repo:

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "7.21.8",
    "@babel/plugin-transform-runtime": "7.21.4",
    "@babel/preset-env": "7.21.5",
    "babel-loader": "^9.1.2",
    "babel-plugin-polyfill-corejs3": "^0.8.1",
    "babel-plugin-polyfill-regenerator": "^0.5.0",
    "webpack": "^5.84.1",
    "webpack-cli": "^5.1.1"
  },
  "dependencies": {
    "@babel/runtime": "7.21.5",
    "core-js-pure": "^3.30.2",
    "regenerator-runtime": "^0.13.11"
  }
}

@nicolo-ribaudo
Copy link
Member

Thanks!

This behavior is expected, and it's a breaking change introduced in babel-plugin-polyfill-corejs3 0.8.0 (according to semver, version starting with 0. should bump the second number when there is a breaking change).

Your configuration is asking Babel to use inject "pure" imports to polyfills for the features that you are using. However, Babel previously had a problem: it was injecting imports to helpers (@babel/runtime) that were using modern features without being polyfilled.

babel-polyfill-* packages can now "hint" at Babel runtime from which package to load the helpers, providing a package that is internally polyfilled. This behavior is similar to the old polyfilling strategy, using @babel/plugin-transform-runtime's corejs option instead of babel-plugin-polyfill-corejs3.

We need to improve the docs around this behavior, specifically in https://github.com/babel/babel-polyfills/tree/main/packages/babel-plugin-polyfill-corejs3 and https://github.com/babel/website/blob/main/docs/plugin-transform-runtime.md.

@fire332
Copy link
Author

fire332 commented May 26, 2023

Could this be related? And I'm now expected to install @babel/runtime-corejs3 as well as @babel/runtime?

@nicolo-ribaudo
Copy link
Member

You should only need @babel/runtime-corejs3 now. It has been introduced in babel/babel-polyfills#160

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