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

module: add __esModule to require()'d ESM #52166

Closed
wants to merge 4 commits into from

Commits on Jul 3, 2024

  1. Configuration menu
    Copy the full SHA
    d4a8a49 View commit details
    Browse the repository at this point in the history
  2. module: add __esModule to require()'d ESM

    Tooling in the ecosystem have been using the __esModule property to
    recognize transpiled ESM in consuming code. For example, a 'log'
    package written in ESM:
    
    export function log(val) { console.log(val); }
    
    Can be transpiled as:
    
    exports.__esModule = true;
    exports.default = function log(val) { console.log(val); }
    
    The consuming code may be written like this in ESM:
    
    import log from 'log'
    
    Which gets transpiled to:
    
    const _mod = require('log');
    const log = _mod.__esModule ? _mod.default : _mod;
    
    So to allow transpiled consuming code to recognize require()'d real ESM
    as ESM and pick up the default exports, we add a __esModule property by
    building a source text module facade for any module that has a default
    export and add .__esModule = true to the exports. We don't do this to
    modules that don't have default exports to avoid the unnecessary
    overhead. This maintains the enumerability of the re-exported names
    and the live binding of the exports.
    
    The source of the facade is defined as a constant per-isolate property
    required_module_facade_source_string, which looks like this
    
    export * from 'original';
    export { default } from 'original';
    export const __esModule = true;
    
    And the 'original' module request is always resolved by
    createRequiredModuleFacade() to wrap which is a ModuleWrap wrapping
    over the original module.
    joyeecheung committed Jul 3, 2024
    Configuration menu
    Copy the full SHA
    1035f58 View commit details
    Browse the repository at this point in the history

Commits on Jul 9, 2024

  1. Configuration menu
    Copy the full SHA
    911bbb3 View commit details
    Browse the repository at this point in the history

Commits on Jul 10, 2024

  1. Configuration menu
    Copy the full SHA
    ae54d52 View commit details
    Browse the repository at this point in the history