{"version":3,"file":"getIntlayerBundle.mjs","names":[],"sources":["../../../src/loadDictionaries/getIntlayerBundle.ts"],"sourcesContent":["import { readFile } from 'node:fs/promises';\nimport { builtinModules, createRequire } from 'node:module';\nimport { join } from 'node:path';\nimport { bundleFile, type ESBuildPlugin } from '@intlayer/config/file';\nimport { getProjectRequire } from '@intlayer/config/utils';\nimport type { IntlayerConfig } from '@intlayer/types/config';\n\n/**\n * Rewrites bare specifiers to absolute paths on the user's disk and externalizes them\n * to preserve directory context (__dirname/import.meta.url).\n */\nconst localResolvePlugin = (\n  aliases: Record<string, string>,\n  rootRequire: NodeJS.Require\n): ESBuildPlugin => {\n  return {\n    name: 'local-resolve',\n    setup(build) {\n      build.onResolve({ filter: /.*/ }, (args) => {\n        // Direct alias match\n        if (aliases[args.path]) {\n          return {\n            path: aliases[args.path],\n            external: true, // Prevents inlining and context loss\n          };\n        }\n\n        // Dynamic resolution, importer first then user workspace.\n        //\n        // The importer takes priority so the bundle always keeps the\n        // `@intlayer/*` copy the importing file itself depends on. Resolving\n        // only from the project root lets Node walk past the project when the\n        // package is not installed there, and pick up an unrelated\n        // `node_modules` in a parent directory — which silently mixes two\n        // different Intlayer installations into the same bundle.\n        if (args.path === 'defu' || args.path.startsWith('@intlayer/')) {\n          const importerRequire = args.importer\n            ? createRequire(args.importer)\n            : undefined;\n\n          for (const resolver of [importerRequire, rootRequire]) {\n            if (!resolver) continue;\n\n            try {\n              return {\n                path: resolver.resolve(args.path),\n                external: true, // Injects `require('/absolute/path')`\n              };\n            } catch {\n              // Try the next resolver, then let esbuild resolve it itself.\n            }\n          }\n        }\n\n        return null;\n      });\n    },\n  };\n};\n\nexport const getIntlayerBundle = async (configuration: IntlayerConfig) => {\n  const rootRequire = getProjectRequire(configuration.system.baseDir);\n\n  const configurationPath = join(\n    configuration.system.configDir,\n    `configuration.cjs`\n  );\n\n  const aliases = {\n    '@intlayer/config/built': configurationPath,\n  };\n\n  const filePath = rootRequire.resolve('intlayer');\n  const code = await readFile(filePath, 'utf-8');\n\n  const output = await bundleFile(code, filePath, {\n    bundle: true,\n    platform: 'node',\n    external: [\n      ...builtinModules,\n      ...builtinModules.map((mod) => `node:${mod}`),\n      'vscode',\n      'esbuild',\n    ],\n    minify: true,\n    plugins: [localResolvePlugin(aliases, rootRequire)],\n  });\n\n  return output ?? '';\n};\n"],"mappings":";;;;;;;;;;;AAWA,MAAM,sBACJ,SACA,gBACkB;CAClB,OAAO;EACL,MAAM;EACN,MAAM,OAAO;GACX,MAAM,UAAU,EAAE,QAAQ,KAAK,IAAI,SAAS;IAE1C,IAAI,QAAQ,KAAK,OACf,OAAO;KACL,MAAM,QAAQ,KAAK;KACnB,UAAU;IACZ;IAWF,IAAI,KAAK,SAAS,UAAU,KAAK,KAAK,WAAW,YAAY,GAAG;KAC9D,MAAM,kBAAkB,KAAK,WACzB,cAAc,KAAK,QAAQ,IAC3B;KAEJ,KAAK,MAAM,YAAY,CAAC,iBAAiB,WAAW,GAAG;MACrD,IAAI,CAAC,UAAU;MAEf,IAAI;OACF,OAAO;QACL,MAAM,SAAS,QAAQ,KAAK,IAAI;QAChC,UAAU;OACZ;MACF,QAAQ,CAER;KACF;IACF;IAEA,OAAO;GACT,CAAC;EACH;CACF;AACF;AAEA,MAAa,oBAAoB,OAAO,kBAAkC;CACxE,MAAM,cAAc,kBAAkB,cAAc,OAAO,OAAO;CAOlE,MAAM,UAAU,EACd,0BANwB,KACxB,cAAc,OAAO,WACrB,mBAI0C,EAC5C;CAEA,MAAM,WAAW,YAAY,QAAQ,UAAU;CAC/C,MAAM,OAAO,MAAM,SAAS,UAAU,OAAO;CAe7C,OAAO,MAbc,WAAW,MAAM,UAAU;EAC9C,QAAQ;EACR,UAAU;EACV,UAAU;GACR,GAAG;GACH,GAAG,eAAe,KAAK,QAAQ,QAAQ,KAAK;GAC5C;GACA;EACF;EACA,QAAQ;EACR,SAAS,CAAC,mBAAmB,SAAS,WAAW,CAAC;CACpD,CAAC,KAEgB;AACnB"}