{"version":3,"file":"preprocess.mjs","names":[],"sources":["../../../../src/modules/bundler/plugins/preprocess.ts"],"sourcesContent":["import type { Plugin, TransformResult } from \"rolldown\";\n\nimport type { CodegenResult } from \"#/ast/codegen\";\nimport type { ParseResult } from \"#/ast/parse\";\nimport type { CompilerContext } from \"#/contexts/compiler\";\nimport type { PreprocessResult } from \"#/modules/preprocessor\";\n\nimport * as Path from \"node:path\";\n\nimport { codegen } from \"#/ast/codegen\";\nimport { parse } from \"#/ast/parse\";\nimport { createCompilerContext } from \"#/contexts/compiler\";\nimport { collect } from \"#/modules/collector\";\nimport { preprocess } from \"#/modules/preprocessor\";\n\ntype FilePreprocessorOptions = {\n    context: CompilerContext;\n    packageName: string;\n    file: string;\n    includedFunctions: string[] | readonly string[];\n};\n\nconst filePreprocessor = (options: FilePreprocessorOptions): Plugin => {\n    return {\n        name: \"@ammolite/transpiler/file-preprocessor\",\n        transform: (code: string, file: string): TransformResult => {\n            // avoid duplicated preprocess\n            if (Path.resolve(file) === Path.resolve(options.file))\n                return void 0;\n\n            const parseResult: ParseResult = parse({\n                file,\n                code,\n            });\n\n            const context: CompilerContext = createCompilerContext({\n                bundler: options.context.bundler,\n                file,\n                program: parseResult.program,\n            });\n\n            const { isImported, namespaces, specifiers } = collect({\n                context,\n                program: parseResult.program,\n                packageName: options.packageName,\n                includedFunctions: options.includedFunctions,\n            });\n\n            if (!isImported) return void 0;\n\n            const preprocessed: PreprocessResult = preprocess({\n                context,\n                program: parseResult.program,\n                namespaces,\n                includedFunctions: options.includedFunctions,\n                specifiers,\n            });\n\n            const codegenResult: CodegenResult = codegen({\n                file,\n                program: preprocessed.program,\n            });\n\n            return {\n                code: codegenResult.code,\n                map: codegenResult.map,\n            };\n        },\n    };\n};\n\nexport type { FilePreprocessorOptions };\nexport { filePreprocessor };\n"],"mappings":";;;;;;;AAsBA,MAAM,oBAAoB,YAA6C;CACnE,OAAO;EACH,MAAM;EACN,YAAY,MAAc,SAAkC;GAExD,IAAI,KAAK,QAAQ,IAAI,MAAM,KAAK,QAAQ,QAAQ,IAAI,GAChD,OAAO,KAAK;GAEhB,MAAM,cAA2B,MAAM;IACnC;IACA;GACJ,CAAC;GAED,MAAM,UAA2B,sBAAsB;IACnD,SAAS,QAAQ,QAAQ;IACzB;IACA,SAAS,YAAY;GACzB,CAAC;GAED,MAAM,EAAE,YAAY,YAAY,eAAe,QAAQ;IACnD;IACA,SAAS,YAAY;IACrB,aAAa,QAAQ;IACrB,mBAAmB,QAAQ;GAC/B,CAAC;GAED,IAAI,CAAC,YAAY,OAAO,KAAK;GAU7B,MAAM,gBAA+B,QAAQ;IACzC;IACA,SAVmC,WAAW;KAC9C;KACA,SAAS,YAAY;KACrB;KACA,mBAAmB,QAAQ;KAC3B;IACJ,CAIwB,CAAC,CAAC;GAC1B,CAAC;GAED,OAAO;IACH,MAAM,cAAc;IACpB,KAAK,cAAc;GACvB;EACJ;CACJ;AACJ"}