Skip to content

[Bug]: commonjs lazy transform produces invalid output with multiple ns imports for same module #15939

Description

@nicolo-ribaudo

💻

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

import * as a from "mod";
import * as b from "mod";

later(() => [a, b])

Configuration file name

No response

Configuration

const out = transformSync(input, {
  configFile: false,
  plugins: [[await load(`transform-modules-commonjs`), { lazy: true }]],
});

Current and expected behavior

Current:

"use strict";

function a() {
  const data = _interopRequireWildcard(require("mod"));
  a = function () {
    return data;
  };
  return data;
}
var b = a();
function _getRequireWildcardCache(nodeInterop) { ... }
later(() => [a(), b()]);

Expected (note the b declaration):

"use strict";

function a() {
  const data = _interopRequireWildcard(require("mod"));
  a = function () {
    return data;
  };
  return data;
}
var b = a;
function _getRequireWildcardCache(nodeInterop) { ... }
later(() => [a(), b()]);

Environment

Current main

Possible solution

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions