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/runtime 7.13.3 unexpected cjs modules in esm modules #12861

Closed
1 task
afc163 opened this issue Feb 23, 2021 · 16 comments · Fixed by #12883
Closed
1 task

@babel/runtime 7.13.3 unexpected cjs modules in esm modules #12861

afc163 opened this issue Feb 23, 2021 · 16 comments · Fixed by #12883
Labels
i: bug i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@afc163
Copy link

afc163 commented Feb 23, 2021

Bug Report

  • I would like to work on a fix!

Current behavior

https://unpkg.com/@babel/runtime@7.13.2/helpers/createSuper/_index.mjs

import getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import isNativeReflectConstruct from "@babel/runtime/helpers/isNativeReflectConstruct";
import possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
export default function _createSuper(Derived) {
  var hasNativeReflectConstruct = isNativeReflectConstruct();
  return function _createSuperInternal() {
    var Super = getPrototypeOf(Derived),
        result;

    if (hasNativeReflectConstruct) {
      var NewTarget = getPrototypeOf(this).constructor;
      result = Reflect.construct(Super, arguments, NewTarget);
    } else {
      result = Super.apply(this, arguments);
    }

    return possibleConstructorReturn(this, result);
  };
}

Expected behavior

Should it be?

```js
- import getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
- import isNativeReflectConstruct from "@babel/runtime/helpers/isNativeReflectConstruct";
- import possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
+ import getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
+ import isNativeReflectConstruct from "@babel/runtime/helpers/esm/isNativeReflectConstruct";
+ import possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
export default function _createSuper(Derived) {
  var hasNativeReflectConstruct = isNativeReflectConstruct();
  return function _createSuperInternal() {
    var Super = getPrototypeOf(Derived),
        result;

    if (hasNativeReflectConstruct) {
      var NewTarget = getPrototypeOf(this).constructor;
      result = Reflect.construct(Super, arguments, NewTarget);
    } else {
      result = Super.apply(this, arguments);
    }

    return possibleConstructorReturn(this, result);
  };
}

like: https://unpkg.com/browse/@babel/runtime@7.12.0/helpers/esm/createSuper.js

Environment

  System:
    OS: macOS 10.15.7
  Binaries:
    Node: 15.3.0 - ~/.nvm/versions/node/v15.3.0/bin/node
    Yarn: 1.22.10 - ~/.nvm/versions/node/v15.3.0/bin/yarn
    npm: 7.0.14 - ~/.nvm/versions/node/v15.3.0/bin/npm
  npmPackages:
    @babel/runtime: ^7.12.5 => 7.13.2 
    babel-plugin-add-react-displayname: ^0.0.5 => 0.0.5 
    eslint: ^7.9.0 => 7.20.0 
    eslint-plugin-babel: ^5.3.0 => 5.3.1 
    jest: ^26.0.0 => 26.6.3 
  • Babel version(s): 7.13.3
  • Node/npm version: 15.3.0
  • OS: [e.g. macOS 10.15.4, Windows 10]
  • Monorepo: [e.g. yes/no/Lerna]
  • How you are using Babel: [e.g. webpack, rollup, parcel, babel-register]

Possible Solution

Additional context

@afc163 afc163 changed the title 7.13.3 unexpected cjs modules in esm modules @babel/runtime 7.13.3 unexpected cjs modules in esm modules Feb 23, 2021
@babel-bot
Copy link
Collaborator

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

Is this causing a problem? @babel/runtime/helpers/getPrototypeOf should resolve to _index.mjs in every tool that supports Node.js's resolution algorithm, and in the worst case the CJS version should still work.

@372623460jh
Copy link

The esm module unable to import the cjs module.
Error: 'default' is not exported by node_modules/@babel_runtime@7.13.2@@babel/runtime/helpers/defineProperty/index.js, imported by node_modules/@babel_runtime@7.13.2@@babel/runtime/helpers/objectSpread2/_index.mjs.

@afc163
Copy link
Author

afc163 commented Feb 23, 2021

Package exports is not supported in webpack 4

"./helpers/createSuper": {
"module": "./helpers/createSuper/_index.mjs",
"node": "./helpers/createSuper/index.js",
"default": "./helpers/createSuper/_index.mjs"
},

@KACAH
Copy link

KACAH commented Feb 23, 2021

It is causing a problem. I am using Babel with Webpack and resuting source is like

image

As you see, I got module instead of function in _toConsumableArray variable. The usage of it (generated by transform-runtime) obviously fails.

image

_toConsumableArray is not a function

Maybe there is a setting somewhere to fix this, but it's definetly a breaking change for me, not just a minor update. Babel 7.12 works perfectly fine.

@dollysingh3192
Copy link

dollysingh3192 commented Feb 23, 2021

I have babel "7.12.17" with below configuration and everything was working fine till yesterday.

{
    "presets": [
        [
            "@babel/env"
        ],
        [
            "@babel/typescript",
            {
                "allowDeclareFields": true
            }
        ]
    ],
    "plugins": [
        "lodash",
        [
            "@babel/plugin-proposal-decorators",
            {
                "legacy": true
            }
        ],
        "@babel/proposal-class-properties",
        "@babel/proposal-object-rest-spread",
        [
            "@babel/plugin-transform-runtime", //target environment are not supported
            {
                "corejs": 3,
                "helpers": true,
                "regenerator": true,
                "useESModules": true
            }
        ]
    ]
}

After "7.13.0" release I am continuously getting error, where I am trying to extend a Class but not able to do so.

Uncaught ReferenceError: _Reflect$construct is not defined at _isNativeReflectConstruct

In previous version's I can see ./node_modules/@babel/runtime-corejs3/core-js-stable/reflect/construct.js was injected on top but now it doesn't

Any suggestions? Please

@KACAH
Copy link

KACAH commented Feb 23, 2021

I have babel "7.12.17" with below configuration and everything was working fine till yesterday.
After "7.13.0" release I am continuously getting error, where I am trying to extend a Class but not able to do so.
Any suggestions? Please

As temporary fix you can rollback to 7.12.x. Replace "^7.12.17" with "~7.12.17" for all @babel packages in your package.json and reinstall dependencies.

@dollysingh3192
Copy link

dollysingh3192 commented Feb 23, 2021

Yes Thanks @KACAH Just did that. But Why is this happening, I mean what causing polyfill not injecting?

@nicolo-ribaudo
Copy link
Member

@dollysingh3192 We unified our polyfill implementations for corejs2 and corejs3, and it caused problems.

@nicolo-ribaudo
Copy link
Member

For people with the corejs problem

Please go to #12863, this is a different issue!

@dollysingh3192
Copy link

@nicolo-ribaudo Thanks for your response and the link in above comment.

Is there any helpful link where we can read more on detail on this unification implementation?

Thanks again!

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Feb 24, 2021

@KACAH Your problem is the opposite: your bundle is requiring an ESM file in CJS. Could you create a small repo that shows the error?

@372623460jh your error will be fixed by #12883

@KACAH
Copy link

KACAH commented Feb 25, 2021

@KACAH Your problem is the opposite: your bundle is requiring an ESM file in CJS. Could you create a small repo that shows the error?

My problem seems to be fixed after this commit. So the build works fine again after @babel/runtime >= 7.13.4. Thank you for a fast reaction!

Here is a small repo with pre-fix problem demo, just for a history I guess. BTW problem does not appear without sourceType: 'unambiguous' in a babel config.

@nicolo-ribaudo
Copy link
Member

I'm glad that it was solved, I'm sorry it took us a few attempts to finally get the "exports" definitions correct 😅

@KACAH
Copy link

KACAH commented Feb 25, 2021

Just found out documentation on exports setup in "package.json". There is a note about node export condition.

"node" - matches for any Node.js environment. Can be a CommonJS or ES module file. This condition should always come after "import" or "require"

Not sure if this is or will be important, but your node conditions come BEFORE import. Just letting you know.

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Feb 25, 2021

Yes that was a deliberate decision: when running in Node, we always want to load the CommonJS file. Otherwise you could end up running two different "copies" of the same helper: the ESM one when using import and the CJS one when using require.

Unfortunately in webpack there could still be duplication between ESM and CJS, but it's not something that we can fix until Babel 8 (and it was already happening with older @babel/runtime versions).

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

6 participants