{"version":3,"file":"compiler.mjs","names":[],"sources":["../../src/contexts/compiler.ts"],"sourcesContent":["import type { Program } from \"oxc-parser\";\nimport type { Format, Partial } from \"ts-vista\";\n\nimport type { CodegenResult } from \"#/ast/codegen\";\n\nimport { codegen } from \"#/ast/codegen\";\nimport { version } from \"../../package.json\";\n\ntype BundlerName = \"webpack\" | \"rspack\" | \"rollup\" | \"vite\" | \"unknown\";\n\ntype BundlerContext = {\n    /**\n     * Bundler name.\n     */\n    name: BundlerName;\n    /**\n     * Bundler version.\n     */\n    version: string;\n};\n\ntype CreateCompilerContextBaseOptions = {\n    /**\n     * Current bundler.\n     */\n    bundler: BundlerContext;\n    /**\n     * Whether in test mode.\n     */\n    test: boolean;\n    /**\n     * Current file.\n     */\n    file: string;\n};\n\ntype CreateCompilerContextCodeOptions = Format<\n    CreateCompilerContextBaseOptions & {\n        /**\n         * Current file code.\n         */\n        code: string;\n    }\n>;\n\ntype CreateCompilerContextProgramOptions = Format<\n    CreateCompilerContextBaseOptions & {\n        /**\n         * Current file program.\n         */\n        program: Program;\n    }\n>;\n\n/**\n * Options for `createCompilerContext` function.\n */\ntype CompleteCreateCompilerContextOptions =\n    | CreateCompilerContextCodeOptions\n    | CreateCompilerContextProgramOptions;\n\ntype CreateCompilerContextOptions = Format<\n    Partial<CompleteCreateCompilerContextOptions, \"bundler\" | \"test\">\n>;\n\n/**\n * Compiler context.\n */\ntype CompilerContext = {\n    /**\n     * Compiler version.\n     */\n    version: string;\n    /**\n     * Current bundler.\n     */\n    bundler: BundlerContext;\n    /**\n     * Whether in test mode.\n     */\n    isTest: boolean;\n    /**\n     * Current file.\n     */\n    file: string;\n    /**\n     * Current file code.\n     */\n    code: string;\n};\n\nconst createCompilerContext = (\n    options: CreateCompilerContextOptions,\n): CompilerContext => {\n    const bundler: BundlerContext = options.bundler ?? {\n        name: \"unknown\",\n        version: \"unknown\",\n    };\n\n    const isTest: boolean = options.test ?? false;\n\n    if (\"code\" in options) {\n        return {\n            version,\n            bundler,\n            isTest,\n            file: options.file,\n            code: options.code,\n        };\n    }\n\n    const codegenResult: CodegenResult = codegen({\n        file: options.file,\n        program: options.program,\n    });\n\n    return {\n        version,\n        bundler,\n        isTest,\n        file: options.file,\n        code: codegenResult.code,\n    };\n};\n\ntype UpdateCompilerContextCodeOptions = Format<\n    Partial<\n        CreateCompilerContextBaseOptions & {\n            /**\n             * Current file code.\n             */\n            code: string;\n        }\n    >\n>;\n\ntype UpdateCompilerContextProgramOptions = Format<\n    Partial<\n        CreateCompilerContextBaseOptions & {\n            /**\n             * Current file program.\n             */\n            program: Program;\n        }\n    >\n>;\n\ntype UpdateCompilerContextOptions = Format<\n    UpdateCompilerContextCodeOptions | UpdateCompilerContextProgramOptions\n>;\n\nconst updateCompilerContext = (\n    context: CompilerContext,\n    options: UpdateCompilerContextOptions,\n): CompilerContext => {\n    let result: CompilerContext = context;\n\n    if (options.file) {\n        result = {\n            ...result,\n            file: options.file,\n        };\n    }\n\n    if (\"code\" in options && options.code) {\n        result = {\n            ...result,\n            code: options.code,\n        };\n    }\n\n    if (\"program\" in options && options.program) {\n        const codegenResult: CodegenResult = codegen({\n            file: context.file ?? options.file,\n            program: options.program,\n        });\n\n        return {\n            ...context,\n            code: codegenResult.code,\n        };\n    }\n\n    if (typeof options.test === \"boolean\") {\n        result = {\n            ...result,\n            isTest: options.test,\n        };\n    }\n\n    if (options.bundler) {\n        result = {\n            ...result,\n            bundler: options.bundler,\n        };\n    }\n\n    return result;\n};\n\nexport type {\n    BundlerContext,\n    BundlerName,\n    CompilerContext,\n    CreateCompilerContextOptions,\n};\nexport { createCompilerContext, updateCompilerContext };\n"],"mappings":";;;AA2FA,MAAM,yBACF,YACkB;CAClB,MAAM,UAA0B,QAAQ,WAAW;EAC/C,MAAM;EACN,SAAS;CACb;CAEA,MAAM,SAAkB,QAAQ,QAAQ;CAExC,IAAI,UAAU,SACV,OAAO;EACH;EACA;EACA;EACA,MAAM,QAAQ;EACd,MAAM,QAAQ;CAClB;CAGJ,MAAM,gBAA+B,QAAQ;EACzC,MAAM,QAAQ;EACd,SAAS,QAAQ;CACrB,CAAC;CAED,OAAO;EACH;EACA;EACA;EACA,MAAM,QAAQ;EACd,MAAM,cAAc;CACxB;AACJ;AA4BA,MAAM,yBACF,SACA,YACkB;CAClB,IAAI,SAA0B;CAE9B,IAAI,QAAQ,MACR,SAAS;EACL,GAAG;EACH,MAAM,QAAQ;CAClB;CAGJ,IAAI,UAAU,WAAW,QAAQ,MAC7B,SAAS;EACL,GAAG;EACH,MAAM,QAAQ;CAClB;CAGJ,IAAI,aAAa,WAAW,QAAQ,SAAS;EACzC,MAAM,gBAA+B,QAAQ;GACzC,MAAM,QAAQ,QAAQ,QAAQ;GAC9B,SAAS,QAAQ;EACrB,CAAC;EAED,OAAO;GACH,GAAG;GACH,MAAM,cAAc;EACxB;CACJ;CAEA,IAAI,OAAO,QAAQ,SAAS,WACxB,SAAS;EACL,GAAG;EACH,QAAQ,QAAQ;CACpB;CAGJ,IAAI,QAAQ,SACR,SAAS;EACL,GAAG;EACH,SAAS,QAAQ;CACrB;CAGJ,OAAO;AACX"}