Skip to content

Unused function in ESM code winds up incorrectly referencing a global #20151

@anomiex

Description

@anomiex

Bug Description

An unused function in ESM code winds up being incorrectly transformed, referencing a global variable rather than the imported variable.

This causes an unrelated variable in the entry module that happens to have the same name to be renamed.

Link to Minimal Reproduction and step to reproduce

Reproduction: test.zip

  1. Unzip the reproduction project.
  2. Run npm install && npm exec webpack.
  3. Examine dist/main.js.

Expected Behavior

If function unused() is not tree-shaken out, it looks something like this:

function unused() {
    return (0,_i18n_js__WEBPACK_IMPORTED_MODULE_0__.__)( "wtf" );
}

Also, in the entry module, the const __ retains that name.

Actual Behavior

Function unused() is present, but incorrectly appears like this:

function unused() {
    return __( "wtf" );
}

Also the const __ has been renamed to const src_, presumably to avoid interfering with that global variable reference.

Environment

System:
    OS: Linux 6.16 Debian GNU/Linux forky/sid
    CPU: (16) x64 AMD Ryzen 7 7840U w/ Radeon  780M Graphics
    Memory: 22.74 GB / 30.66 GB
  Binaries:
    Node: 22.21.0 - /usr/bin/node
    Yarn: 1.22.22 - /usr/local/bin/yarn
    npm: 10.9.4 - /usr/bin/npm
    pnpm: 10.23.0 - /usr/local/bin/pnpm
  Browsers:
    Firefox: 145.0.1
    Firefox Developer Edition: 145.0.1
  Packages:
    webpack: 5.103.0 => 5.103.0 
    webpack-cli: ^6.0.1 => 6.0.1

Is this a regression?

None

Last Working Version

No response

Additional Context

v5.92.0 introduced the "const __ has been renamed to const src_" part, presumably due #18772. Turning off optimization.avoidEntryIife works around that part.

Versions as far back as v5.61.0 generate the incorrect code in the unused function. I couldn't check earlier versions due to errors like "error:0308010C:digital envelope routines::unsupported".

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions