The Wayback Machine - https://web.archive.org/web/20211120043828/https://github.com/nodejs/node/pull/40892
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

esm: add option to interpret __esModule like Babel #40892

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

@qnighy
Copy link

@qnighy qnighy commented Nov 20, 2021

This PR adds an option --cjs-import-interop. When enabled, a CJS module will be translated to ESM slightly differently with respect to default exports.

  • When the module defines module.exports.__esModule as a truthy value, the value of module.exports.default is used as the default export.
  • Otherwise, module.exports is used as the default export. (existing behavior)

It allows better interoperation between full ES modules and CJS modules transformed from ES modules by Babel or tsc. Consider the following example:

// Transformed from:
// export default "Hello";
Object.defineProperty(module.exports, "__esModule", { value: true });
module.exports.default = "Hello";

When imported from the following module:

import greeting from "./hello.cjs";
console.log(greeting);

With --cjs-import-interop, it will print "Hello".

Fixes: #40891

Adds an option `--cjs-import-interop`. When enabled, a CJS module will
be translated to ESM slightly differently with respect to default
exports.

- When the module defines `module.exports.__esModule` as a truthy value,
  the value of `module.exports.default` is used as the default export.
- Otherwise, `module.exports` is used as the default export.
  (existing behavior)

It allows better interoperation between full ES modules and CJS modules
transformed from ES modules by Babel or tsc. Consider the following
example:

```javascript
// Transformed from:
// export default "Hello";
Object.defineProperty(module.exports, "__esModule", { value: true });
module.exports.default = "Hello";
```

When imported from the following module:

```javascript
import greeting from "./hello.cjs";
console.log(greeting);
```

With `--cjs-import-interop`, it will print "Hello".

Fixes: nodejs#40891
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants