{"version":3,"file":"babelParser.mjs","names":[],"sources":["../../../src/utils/babelParser.ts"],"sourcesContent":["import { createRequire } from 'node:module';\nimport type {\n  babelTsParser as babelTsParserImpl,\n  RecastParser,\n  typescriptParser as typescriptParserImpl,\n} from '@intlayer/babel/babelParser';\n\n/**\n * Recast-compatible parsers, backed by `@intlayer/babel`.\n *\n * All Babel operations live in `@intlayer/babel`, so this package never depends\n * on `@babel/parser` directly. `@intlayer/babel/babelParser` is loaded lazily on\n * the first `parse` call: `recast.parse` invokes `parser.parse` synchronously,\n * so a `require` (rather than a dynamic `import`) is used to keep the API\n * synchronous while deferring the load — `@babel/parser` stays off the hot path\n * until an AST transform actually runs.\n *\n * `@intlayer/babel` is an optional peer dependency of `@intlayer/engine`. The\n * flows that use these parsers (`intlayer init`, content-declaration writes) are\n * driven by `@intlayer/cli`, which ships `@intlayer/babel`, so it is present\n * whenever these parsers are needed.\n */\n\n/** Works in both the ESM and CJS builds (see `@intlayer/config`'s helper). */\nconst isESModule = typeof import.meta.url === 'string';\nconst engineRequire: NodeJS.Require = isESModule\n  ? createRequire(import.meta.url)\n  : require;\n\nconst BABEL_PARSER_MODULE = '@intlayer/babel/babelParser';\n\ntype BabelParserModule = {\n  typescriptParser: typeof typescriptParserImpl;\n  babelTsParser: typeof babelTsParserImpl;\n};\n\nlet cachedModule: BabelParserModule | undefined;\n\n/**\n * Lazily resolves `@intlayer/babel/babelParser`, caching the result.\n *\n * @throws If `@intlayer/babel` is not installed.\n */\nconst loadBabelParser = (): BabelParserModule => {\n  if (cachedModule) return cachedModule;\n\n  try {\n    cachedModule = engineRequire(BABEL_PARSER_MODULE) as BabelParserModule;\n  } catch (error) {\n    throw new Error(\n      `Failed to load \"${BABEL_PARSER_MODULE}\". Install \"@intlayer/babel\" to enable AST-based transforms.`,\n      { cause: error }\n    );\n  }\n\n  return cachedModule;\n};\n\nexport type { RecastParser };\n\n/** Drop-in replacement for `recast/parsers/typescript` (TS without JSX). */\nexport const typescriptParser: RecastParser = {\n  parse: (source: string) => loadBabelParser().typescriptParser.parse(source),\n};\n\n/** Drop-in replacement for `recast/parsers/babel-ts` (TS *and* JSX). */\nexport const babelTsParser: RecastParser = {\n  parse: (source: string) => loadBabelParser().babelTsParser.parse(source),\n};\n"],"mappings":";;;;AAyBA,MAAM,gBADa,OAAO,YAAY,QAAQ,WAE1C,cAAc,YAAY,GAAG;AAGjC,MAAM,sBAAsB;AAO5B,IAAI;;;;;;AAOJ,MAAM,wBAA2C;CAC/C,IAAI,cAAc,OAAO;CAEzB,IAAI;EACF,eAAe,cAAc,mBAAmB;CAClD,SAAS,OAAO;EACd,MAAM,IAAI,MACR,mBAAmB,oBAAoB,+DACvC,EAAE,OAAO,MAAM,CACjB;CACF;CAEA,OAAO;AACT;;AAKA,MAAa,mBAAiC,EAC5C,QAAQ,WAAmB,gBAAgB,CAAC,CAAC,iBAAiB,MAAM,MAAM,EAC5E;;AAGA,MAAa,gBAA8B,EACzC,QAAQ,WAAmB,gBAAgB,CAAC,CAAC,cAAc,MAAM,MAAM,EACzE"}