{"version":3,"file":"parseFileContent.cjs","names":["getProjectRequire","loadEnvFile"],"sources":["../../../src/loadExternalFile/parseFileContent.ts"],"sourcesContent":["import { type Context, runInNewContext } from 'node:vm';\nimport * as esbuild from 'esbuild';\nimport { type LoadEnvFileOptions, loadEnvFile } from '../loadEnvFile';\nimport { getProjectRequire } from '../utils/ESMxCJSHelpers';\n\nexport type SandBoxContextOptions = {\n  envVarOptions?: LoadEnvFileOptions;\n  projectRequire?: NodeJS.Require;\n  additionalEnvVars?: Record<string, string>;\n  /**\n   * Map of specifier -> mocked export to be returned when code in the VM calls require(specifier).\n   * Example:\n   *   mocks: {\n   *     '@intlayer/config/built': { getConfig: () => ({}), Locales: {} }\n   *   }\n   */\n  mocks?: Record<string, any>;\n  /**\n   * Optional alias map if you want to redirect specifiers.\n   * Useful when user code imports a subpath you want to collapse.\n   * Example:\n   *   aliases: { '@intlayer/config/built': '@intlayer/config' }\n   */\n  aliases?: Record<string, string>;\n};\n\nexport const getSandBoxContext = (options?: SandBoxContextOptions): Context => {\n  const { envVarOptions, projectRequire, additionalEnvVars, mocks, aliases } =\n    options ?? {};\n\n  let additionalGlobalVar = {};\n\n  const baseRequire: NodeJS.Require =\n    typeof projectRequire === 'function' ? projectRequire : getProjectRequire();\n\n  // Wrap require to honor mocks and aliases inside the VM\n  const mockedRequire: NodeJS.Require = (() => {\n    const mockTable = Object.assign(\n      {\n        esbuild,\n      },\n      mocks\n    );\n    const aliasTable = Object.assign({}, aliases);\n\n    const wrappedRequire = function mockableRequire(id: string) {\n      const target = aliasTable?.[id] ? aliasTable[id] : id;\n\n      if (mockTable && Object.hasOwn(mockTable, target)) {\n        return mockTable[target];\n      }\n\n      // If the original id was aliased, allow mocks to be defined on either key.\n      if (target !== id && mockTable && Object.hasOwn(mockTable, id)) {\n        return mockTable[id];\n      }\n\n      return baseRequire(target);\n    } as NodeJS.Require;\n\n    // Mirror NodeJS.Require properties\n    wrappedRequire.resolve = baseRequire.resolve.bind(baseRequire);\n    wrappedRequire.main = baseRequire.main;\n    wrappedRequire.extensions = baseRequire.extensions;\n    wrappedRequire.cache = baseRequire.cache;\n\n    return wrappedRequire;\n  })();\n\n  try {\n    // Dynamically try to require React if it's installed in the project\n    additionalGlobalVar = {\n      React: baseRequire('react'),\n    };\n  } catch (_err) {\n    // React is not installed, so we inject a dummy React object to capture JSX elements\n    // This allows using JSX in content declarations even if React is not installed (e.g. in Solid.js or Vue projects)\n    // because esbuild's tsx loader defaults to React.createElement.\n    additionalGlobalVar = {\n      React: {\n        createElement: (type: any, props: any, ...children: any[]) => ({\n          type,\n          props: {\n            ...props,\n            children: children.length <= 1 ? children[0] : children,\n          },\n        }),\n        Fragment: Symbol.for('react.fragment'),\n      },\n    };\n  }\n\n  const sandboxContext: Context = {\n    exports: {\n      default: {},\n    },\n    module: {\n      exports: {},\n    },\n    process: {\n      ...process,\n      env: {\n        ...process.env,\n        ...loadEnvFile(envVarOptions),\n        ...additionalEnvVars,\n      },\n    },\n    console,\n    require: mockedRequire,\n    ...additionalGlobalVar,\n  };\n\n  // Dynamically inject all global variables\n  Object.getOwnPropertyNames(globalThis).forEach((key) => {\n    if (!(key in sandboxContext)) {\n      sandboxContext[key] = globalThis[key as keyof typeof globalThis];\n    }\n  });\n\n  return sandboxContext;\n};\n\nexport const parseFileContent = <T>(\n  fileContentString: string,\n  options?: SandBoxContextOptions\n): T | undefined => {\n  const sandboxContext = getSandBoxContext(options);\n\n  // Force strict mode so illegal writes throw instead of silently failing.\n  runInNewContext(`\"use strict\";\\n${fileContentString}`, sandboxContext);\n\n  const candidates: unknown[] = [\n    sandboxContext.exports?.default,\n    sandboxContext.module?.exports?.defaults,\n    sandboxContext.module?.exports?.default,\n    sandboxContext.module?.exports,\n  ];\n\n  for (const candidate of candidates) {\n    if (\n      candidate &&\n      typeof candidate === 'object' &&\n      Object.keys(candidate as object).length > 0\n    ) {\n      return candidate as T;\n    }\n  }\n};\n"],"mappings":";;;;;;;;;AA0BA,MAAa,qBAAqB,YAA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