Skip to content

Incorrect compilation of imported bindings inside static initialization blocks #16054

@gebsh

Description

@gebsh

Bug report

What is the current behavior?

Running files outputted by webpack containing a static initialization block that uses an imported binding throws ReferenceError. Running source files results in no error at runtime.

// foo.mjs
export default class Foo {}

// bar.mjs
import Foo from './foo.mjs';

export default class Bar {
    static foo;

    static {
        // The following line will throw at runtime when running a file produced
        // by webpack from this one.
        this.foo = new Foo();
    }
}

If the current behavior is a bug, please provide the steps to reproduce.

I created a repository with reproduction of this bug: https://github.com/gebsh/webpack-static-blocks. It already contains files produced by webpack in the dist directory. If needed, they can be created by running start npm script.

What is the expected behavior?

ReferenceError should not be thrown when running files outputted by webpack.

Other relevant information:
webpack version: 5.73.0
Node.js version: 18.6.0
Operating System: macOS Monterey 12.4
Additional tools: -

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions