{
  "version": 3,
  "sources": ["../../src/index.ts"],
  "sourcesContent": ["import { dirname } from 'path';\nimport { readFile } from 'fs/promises';\nimport { createRequire } from 'module';\n\nfunction reloadGenerator(requireModule: any, name: string) {\n  const path = requireModule.resolve(name);\n  delete requireModule.cache[path];\n  return requireModule(name);\n}\n\nfunction noop() {}\n\n// This is right now needed as a .codegen file will be\n// reported as having an invalid file extension; so\n// Node.js will refuse to load / evaluate it\nasync function dynamicCodegenImport(file: string) {\n  const dir = dirname(file);\n  const text = await readFile(file, 'utf8');\n  const code = replaceRelativeImports(text, dir);\n  const content = Buffer.from(code).toString('base64');\n  const url = `data:text/javascript;base64,${content}`;\n  return await import(url);\n}\n\n// Converts relative paths to absolute paths - only needed for (some) imports\nfunction replaceRelativeImports(content: string, absoluteBase: string) {\n  const regex = /import\\s+([^'\"]+)\\s+from\\s+(['\"])\\.\\/([^'\"]+)\\2/g;\n\n  // Replace relative imports with absolute imports\n  return content.replace(regex, (_, imports, quote, relativePath) => {\n    const absolutePath = `${absoluteBase}/${relativePath}`;\n    return `import ${imports} from ${quote}file://${absolutePath}${quote}`;\n  });\n}\n\nexport interface CodegenDetailsOptions {\n  outDir?: string;\n  rootDir?: string;\n}\n\nexport type CodegenType = 'auto' | 'esm' | 'cjs';\n\nexport interface CodegenDetails {\n  name: string;\n  type?: CodegenType;\n  options: CodegenDetailsOptions;\n  addDependency?(file: string, options: CodegenDetailsOptions): void;\n}\n\nexport interface CodegenContext {\n  name: string;\n  options: CodegenDetailsOptions;\n  addDependency(file: string, options: CodegenDetailsOptions): void;\n}\n\nexport interface CodegenResult {\n  value: string;\n  type: string;\n}\n\nexport interface CodegenFile {\n  (this: CodegenContext): string | CodegenResult | Promise<string | CodegenResult>;\n  type?: string;\n}\n\nexport interface CodegenHost {\n  load(name: string, type: CodegenType): Promise<CodegenFile>;\n  generate(details: CodegenDetails): Promise<CodegenResult>;\n}\n\nexport function createCodegenHost(fileOrDirName: string): CodegenHost {\n  const requireModule = createRequire(fileOrDirName);\n  const host: CodegenHost = {\n    load(name, type) {\n      switch (type) {\n        case 'cjs':\n          return Promise.resolve(reloadGenerator(requireModule, name));\n        case 'esm':\n          return dynamicCodegenImport(name).then((result) => result.default || result);\n        case 'auto':\n        default:\n          if (\n            name.endsWith('.mjs.codegen') ||\n            name.endsWith('.esm.codegen') ||\n            name.endsWith('.module.codegen') ||\n            name.endsWith('.m.codegen')\n          ) {\n            return host.load(name, 'esm');\n          }\n\n          return host.load(name, 'cjs');\n      }\n    },\n    async generate(details) {\n      const { name, type = 'auto', options = {}, addDependency = noop } = details;\n\n      if (typeof name !== 'string') {\n        throw new Error(`You need to pass in a string for \"name\". Received: \"${name}\".`);\n      }\n\n      const generator = await host.load(name, type);\n\n      if (typeof generator !== 'function') {\n        throw new Error(`The codegen module \"${name}\" does not export a function and is invalid.`);\n      }\n\n      const result = await (generator.call({\n        name,\n        options,\n        addDependency,\n      }) as ReturnType<CodegenFile>);\n\n      if (typeof result === 'string') {\n        const type = typeof generator.type === 'string' ? generator.type : 'js';\n\n        return {\n          type,\n          value: result,\n        };\n      }\n\n      if (result && typeof result === 'object' && typeof result.value === 'string' && typeof result.type === 'string') {\n        return result;\n      }\n\n      throw new Error(\n        `The codegen module \"${name}\" did not generate a valid result (string or object). It returned: \"${result}\".`,\n      );\n    },\n  };\n  return host;\n}\n"],
  "mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAAwB;AACxB,sBAAyB;AACzB,oBAA8B;AAE9B,SAAS,gBAAgB,eAAoB,MAAc;AACzD,QAAM,OAAO,cAAc,QAAQ,IAAI;AACvC,SAAO,cAAc,MAAM,IAAI;AAC/B,SAAO,cAAc,IAAI;AAC3B;AAEA,SAAS,OAAO;AAAC;AAKjB,eAAe,qBAAqB,MAAc;AAChD,QAAM,UAAM,qBAAQ,IAAI;AACxB,QAAM,OAAO,UAAM,0BAAS,MAAM,MAAM;AACxC,QAAM,OAAO,uBAAuB,MAAM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