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

Closed
wants to merge 3 commits into from

Commits on Nov 20, 2021

  1. esm: add option to interpret __esModule like Babel

    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
    qnighy committed Nov 20, 2021
    Configuration menu
    Copy the full SHA
    06f06f7 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    09d69a7 View commit details
    Browse the repository at this point in the history
  3. esm: fix grammatical error in --cjs-import-interop

    Co-authored-by: Voltrex <mohammadkeyvanzade94@gmail.com>
    qnighy and VoltrexKeyva committed Nov 20, 2021
    Configuration menu
    Copy the full SHA
    682674f View commit details
    Browse the repository at this point in the history