Skip to content

Does not unwrap default exports in CJS under default property #101

@enisdenjo

Description

@enisdenjo

Problem

Various transpilers (like TypeScript) put default exports under the default property when transpiling ESM to CJS.

Meaning:

// mylib (source es)
export default function foo() {};

becomes (roughly):

// mylib (transpiled cjs)
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = foo;
function foo() {};

When consuming such modules, transpilers add "unwrap" helpers (like tslib.__importDefault in TypeScript) to detect the __esModule property in exports and use the default property.

In essence, importing mylib from above like this:

// (source es)
import foo from 'mylib';

will produce the following output (roughly) in TypeScript:

// (transpiled cjs)
const tslib = require('tslib');
const foo = tslib.__importDefault(require('mylib'));

This is not the case with pkgroll, it'll consume mylib without any helpers or checks during bundling.

Please consult the attached repro repository as well as the failing CI showcasing the issue.

Expected behavior

pkgroll uses helpers or detects such cases during bundling to create outputs that handle such cases.

Minimal reproduction URL

https://github.com/enisdenjo/pkgroll-cjs-default-exports-unwrap

Version

v2.5.1

Node.js version

v22.11.0

Package manager

npm

Operating system

macOS

Bugs are expected to be fixed by those affected by it

  • I'm interested in working on this issue

Compensating engineering work financially will speed up resolution

  • I'm willing to offer financial support

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions