// eslint-disable-next-line @typescript-eslint/naming-convention -- `__esModule` is the flag bundlers set on a transpiled module type ExtractDefault = T extends { __esModule?: boolean; default: infer U } ? // eslint-disable-next-line @typescript-eslint/naming-convention -- `__esModule` is the flag bundlers set on a transpiled module U extends { __esModule?: boolean; default: infer V } ? V : U : T; /** * With ESM it is possible to export both a default value and multiple named exports. * With CJS it is possible to "mock" ESM functionality with a `__esModule=true` flag * but still only export a single "default" export, or multiple named exports. * * This helper method detects when a library tries to do a "default" export with other named values * and returns the "intended" default value. * * It also handles CJS exports that export an entire object via `module.exports = {}` rather than export syntax, * as well as extracting the `default` (if exists) from a dynamic import. * * Note there is not "namedImport" equivalent because it is assumed module loaders successfully parse that * out for both CJS and ESM. * * @param mod - "default" export that might be wrapped in another layer * @returns unwrapped module */ export const defaultImport = (mod: T): ExtractDefault => { if (typeof mod !== 'object' || mod === null) { return mod as ExtractDefault; } // Webpack provides a Module tag to match Node.js' Module module const defaultVal = (mod as unknown as { [Symbol.toStringTag]?: string; default: T })[Symbol.toStringTag] === 'Module' ? (( mod as unknown as { [Symbol.toStringTag]: 'Module'; default?: T; } ).default ?? mod) : mod; if ( typeof defaultVal === 'object' && '__esModule' in defaultVal && // eslint-disable-next-line @typescript-eslint/naming-convention, no-underscore-dangle -- `__esModule` is the flag bundlers set on a transpiled module (defaultVal as unknown as { __esModule?: boolean }).__esModule === true && (defaultVal as unknown as { default?: ExtractDefault | undefined }).default !== undefined ) { return (defaultVal as unknown as { default: ExtractDefault }).default; } return defaultVal as ExtractDefault; };