Skip to content

[Bug]: topLevelVar option removes class name, causing a TypeError in static block #5884

@EvenTorset

Description

@EvenTorset

Reproduction link or steps

package.json:

{
  "type": "module",
  "dependencies": {
    "rolldown": "^1.0.0-beta.33"
  },
  "scripts": {
    "build": "rolldown -c"
  }
}

rolldown.config.js:

import { defineConfig } from 'rolldown'

export default defineConfig({
  input: 'index.js',
  output: {
    file: 'bundle.js',
    format: 'es',
    topLevelVar: true
  }
})

index.js:

class Example {
  static {
    this.prop = new Example()
  }
}

Run npm run build to produce the bundle with the error.

What is expected?

For the Example constructor to be available in the class' static block, it must still be named, like this:

var Example = class Example {
	static {
		this.prop = new Example();
	}
};

This is also the same output as when topLevelVar is disabled.

What is actually happening?

The repro steps produce this bundle:

//#region index.js
var Example = class {
	static {
		this.prop = new Example();
	}
};

//#endregion

Here, Example is not available in the static block, causing a TypeError when running the bundle module.

System Info

System:
    OS: Windows 11 10.0.26100
    CPU: (12) x64 AMD Ryzen 5 PRO 4650G with Radeon Graphics
    Memory: 2.06 GB / 15.39 GB
  Binaries:
    Node: 22.14.0 - C:\Program Files\nodejs\node.EXE
    npm: 11.4.1 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (139.0.3405.111)
  npmPackages:
    rolldown: ^1.0.0-beta.33 => 1.0.0-beta.33

Any additional comments?

No response

Metadata

Metadata

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions