UNPKG

autotel

Version:
1 lines 2.71 kB
{"version":3,"sources":["../src/node-require.ts"],"names":["__require","createRequire"],"mappings":";;;;;;AAgBA,IAAM,cACJ,OAAOA,2BAAA,KAAY,cAAcC,sBAAA,CAAc,oQAAe,CAAA,GAAID,2BAAA;AAmB7D,SAAS,YAAyB,EAAA,EAA2B;AAClE,EAAA,IAAI;AACF,IAAA,OAAO,YAAY,EAAE,CAAA;AAAA,EACvB,SAAS,KAAA,EAAO;AACd,IAAA,IAAI,KAAA,IAAU,KAAA,CAAgC,IAAA,KAAS,kBAAA,EAAoB;AAEzE,MAAA,OAAO,MAAA;AAAA,IACT;AAEA,IAAA,MAAM,KAAA;AAAA,EACR;AACF;AAmBO,SAAS,cAA2B,EAAA,EAAe;AACxD,EAAA,OAAO,YAAY,EAAE,CAAA;AACvB","file":"chunk-YS6C2YJE.cjs","sourcesContent":["/**\n * Cross-format require() helper for CJS and ESM compatibility\n *\n * Provides a synchronous `require()` function that works in both:\n * - CJS builds: Uses native `require`\n * - ESM builds: Uses `createRequire(import.meta.url)`\n *\n * This allows optional peer dependencies and dynamic module loading\n * to work synchronously in both module formats.\n */\n\nimport { createRequire } from 'node:module';\n\n// Use native `require` in CJS, `createRequire` in ESM.\n// tsup will compile this appropriately for each format.\n// Type is ReturnType<typeof createRequire> which is a function that loads modules.\nconst nodeRequire =\n typeof require === 'undefined' ? createRequire(import.meta.url) : require;\n\n/**\n * Synchronously require a module (works in both CJS and ESM)\n *\n * @param id - Module ID to require\n * @returns The required module\n * @throws Error if module cannot be loaded\n *\n * @example\n * ```typescript\n * import { safeRequire } from './node-require';\n *\n * const traceloop = safeRequire('@traceloop/node-server-sdk');\n * if (traceloop) {\n * traceloop.initialize({ ... });\n * }\n * ```\n */\nexport function safeRequire<T = unknown>(id: string): T | undefined {\n try {\n return nodeRequire(id) as T;\n } catch (error) {\n if (error && (error as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {\n // Optional dependency missing – return undefined\n return undefined;\n }\n // Any other error is a real bug: rethrow\n throw error;\n }\n}\n\n/**\n * Synchronously require a module (throws if not found)\n *\n * Use this when the module is required (not optional).\n *\n * @param id - Module ID to require\n * @returns The required module\n * @throws Error if module cannot be loaded\n *\n * @example\n * ```typescript\n * import { requireModule } from './node-require';\n *\n * const fs = requireModule<typeof import('node:fs')>('node:fs');\n * const content = fs.readFileSync('file.txt', 'utf8');\n * ```\n */\nexport function requireModule<T = unknown>(id: string): T {\n return nodeRequire(id) as T;\n}\n\n/**\n * Direct access to the nodeRequire function (for advanced use cases)\n */\nexport { nodeRequire };\n"]}