Skip to content

[Feature Request]: skip __toESM when the default export is not used #6849

@sapphi-red

Description

@sapphi-red

What problem does this feature solve?

__toESM helper brings in many helper functions and makes the bundle size bigger.

__toESM helper exists to add the default export in some condition. While the condition is not known until runtime, we can know whether the default import is used or not. When the default import is not used, we can skip the __toESM helper.

For this input:

// index.js
import { foo } from './foo.cjs'
console.log(foo)
// foo.cjs
exports.foo = 'foo'

Rolldown currently outputs:

// omit helper implementations
//#region foo.cjs
var require_foo = __commonJS({ "foo.cjs"(exports) {
	exports.foo = "foo";
} });

//#endregion
//#region index.js
var import_foo = __toESM(require_foo());
console.log(import_foo.foo);

//#endregion

But since we know that the default import is not used, we can improve this to:

// omit helper implementations
//#region foo.cjs
var require_foo = __commonJS({ "foo.cjs"(exports) {
	exports.foo = "foo";
} });

//#endregion
//#region index.ts
var import_foo = require_foo();
console.log(import_foo.foo);

//#endregion

What does the proposed API look like?

Not an API change

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions