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]: _interopRequireWildcard is not generated for .mjs files #14365

Closed
1 task
sagargurtu opened this issue Mar 17, 2022 · 3 comments 路 Fixed by #14366
Closed
1 task

[Bug]: _interopRequireWildcard is not generated for .mjs files #14365

sagargurtu opened this issue Mar 17, 2022 · 3 comments 路 Fixed by #14366
Labels
i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@sagargurtu
Copy link

馃捇

  • Would you like to work on a fix?

How are you using Babel?

@babel/cli

Input code

// src/index.mjs
import _, { capitalize } from "lodash";

console.log(_);
console.log(capitalize);

Configuration file name

.babelrc.js

Configuration

module.exports = {
  sourceType: 'unambiguous',
  presets: [
    [
      '@babel/preset-env',
      {
        targets: {
          node: 'current',
        },
      },
    ],
  ],
};

Current and expected behavior

Current Behavior

The above .mjs file is compiled to:

"use strict";

var _lodash = _interopRequireDefault(require("lodash"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

console.log(_lodash.default);
console.log(_lodash.capitalize);

Expected Behavior

The above .mjs file should be compiled to:

"use strict";

var _lodash = _interopRequireWildcard(require("lodash"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

console.log(_lodash.default);
console.log(_lodash.capitalize);

Npm Script

"build": "babel src --out-dir dist --extensions '.mjs'"

Environment

System:

  • OS: macOS 12.2.1

Binaries:

  • Node: 16.14.0 - ~/.nvm/versions/node/v16.14.0/bin/node
  • npm: 8.3.1 - ~/.nvm/versions/node/v16.14.0/bin/npm

npmPackages:

  • @babel/cli: ^7.12.0 => 7.17.6
  • @babel/core: ^7.12.0 => 7.17.5
  • @babel/preset-env: ^7.12.0 => 7.16.11

Possible solution

No response

Additional context

No response

@babel-bot
Copy link
Collaborator

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

nicolo-ribaudo commented Mar 17, 2022

I cannot reproduce this 馃 Can you also check which versions of @babel/plugin-transform-modules-commonjs and @babel/helper-module-transforms do you have?

EDIT: I can reproduce this: it does not happen when using a .js extension.

@nicolo-ribaudo
Copy link
Member

Oh this is fun. In Babel 6 we had the same behavior that Node.js currently has, but at the time Node.js was more restrictive and only supported default imports in .mjs files. #7545 aligned our behavior to what Node.js was doing, by removing support for named imports.

Then, Node.js changed their behavior to what Babel 6 was doing, so we can change our again to match Node.js.

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