Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks
enisdenjo opened this issue Nov 6, 2024 · 0 comments
Open
2 tasks

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

enisdenjo opened this issue Nov 6, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@enisdenjo
Copy link

enisdenjo commented Nov 6, 2024

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
@enisdenjo enisdenjo added the bug Something isn't working label Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant