* Populate the exports of a CJS module entry from an ESM module's namespace object for * require(esm). * @param {Module} mod CJS module instance * @param {ModuleWrap} wrap ESM ModuleWrap instance. * @param {object} namespace The ESM namespace object.
(mod, wrap, namespace)
| 1773 | * @param {object} namespace The ESM namespace object. |
| 1774 | */ |
| 1775 | function populateCJSExportsFromESM(mod, wrap, namespace) { |
| 1776 | // Tooling in the ecosystem have been using the __esModule property to recognize |
| 1777 | // transpiled ESM in consuming code. For example, a 'log' package written in ESM: |
| 1778 | // |
| 1779 | // export default function log(val) { console.log(val); } |
| 1780 | // |
| 1781 | // Can be transpiled as: |
| 1782 | // |
| 1783 | // exports.__esModule = true; |
| 1784 | // exports.default = function log(val) { console.log(val); } |
| 1785 | // |
| 1786 | // The consuming code may be written like this in ESM: |
| 1787 | // |
| 1788 | // import log from 'log' |
| 1789 | // |
| 1790 | // Which gets transpiled to: |
| 1791 | // |
| 1792 | // const _mod = require('log'); |
| 1793 | // const log = _mod.__esModule ? _mod.default : _mod; |
| 1794 | // |
| 1795 | // So to allow transpiled consuming code to recognize require()'d real ESM |
| 1796 | // as ESM and pick up the default exports, we add a __esModule property by |
| 1797 | // building a source text module facade for any module that has a default |
| 1798 | // export and add .__esModule = true to the exports. This maintains the |
| 1799 | // enumerability of the re-exported names and the live binding of the exports, |
| 1800 | // without incurring a non-trivial per-access overhead on the exports. |
| 1801 | // |
| 1802 | // The source of the facade is defined as a constant per-isolate property |
| 1803 | // required_module_default_facade_source_string, which looks like this |
| 1804 | // |
| 1805 | // export * from 'original'; |
| 1806 | // export { default } from 'original'; |
| 1807 | // export const __esModule = true; |
| 1808 | // |
| 1809 | // And the 'original' module request is always resolved by |
| 1810 | // createRequiredModuleFacade() to `wrap` which is a ModuleWrap wrapping |
| 1811 | // over the original module. |
| 1812 | |
| 1813 | // We don't do this to modules that are marked as CJS ESM or that |
| 1814 | // don't have default exports to avoid the unnecessary overhead. |
| 1815 | // If __esModule is already defined, we will also skip the extension |
| 1816 | // to allow users to override it. |
| 1817 | if (ObjectHasOwn(namespace, 'module.exports')) { |
| 1818 | mod.exports = namespace['module.exports']; |
| 1819 | } else if (!ObjectHasOwn(namespace, 'default') || ObjectHasOwn(namespace, '__esModule')) { |
| 1820 | mod.exports = namespace; |
| 1821 | } else { |
| 1822 | mod.exports = createRequiredModuleFacade(wrap); |
| 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | /** |
| 1827 | * Wraps the given content in a script and runs it in a new context. |
no outgoing calls
no test coverage detected