{"version":3,"file":"index.mjs","names":[],"sources":["../../../src/modules/bundler/index.ts"],"sourcesContent":["import type {\n    OutputChunk,\n    RolldownBuild,\n    RolldownOutput,\n    SourceMap,\n} from \"rolldown\";\nimport type { Format } from \"ts-vista\";\n\nimport type { CompilerContext } from \"#/contexts/compiler\";\n\nimport * as Path from \"node:path\";\n\nimport { rolldown } from \"rolldown\";\nimport tsConfigPaths from \"rollup-plugin-tsconfig-paths\";\n\nimport { entryOverrider } from \"#/modules/bundler/plugins/entry\";\nimport { externalResolver } from \"#/modules/bundler/plugins/external\";\nimport { filePreprocessor } from \"#/modules/bundler/plugins/preprocess\";\n\n/**\n * Preset options for `bundle` function.\n */\ntype PresetBundleOptions = {\n    /**\n     * Compiler context.\n     */\n    context: CompilerContext;\n    /**\n     * Name of the CSS-in-JS package.\n     */\n    packageName: string;\n    /**\n     * List of functions to be preprocessed.\n     */\n    includedFunctions: readonly string[];\n};\n\n/**\n * User defined options for `bundle` function.\n */\ntype UserBundleOptions = {\n    /**\n     * Current working directory.\n     */\n    cwd: string;\n    /**\n     * Array of packages/paths to include.\n     *\n     * Packages/paths in `include` option will be\n     * overwritten by `exclude` option.\n     */\n    include: readonly string[];\n    /**\n     * Array of packages/paths to exclude.\n     */\n    exclude: readonly string[];\n    /**\n     * Dedicated tsconfig file to be used.\n     */\n    tsconfigPath?: string;\n};\n\n/**\n * Dynamic options for `bundle` function to bundle file.\n */\ntype DynamicBundleOptions = {\n    /**\n     * File to be bundled.\n     */\n    file: string;\n    /**\n     * preprocessed entry code.\n     */\n    code: string;\n};\n\n/**\n * Options for `bundle` function.\n */\ntype BundleOptions = Format<\n    PresetBundleOptions & UserBundleOptions & DynamicBundleOptions\n>;\n\n/**\n * Result of `bundle` function.\n */\ntype BundleResult = {\n    /**\n     * Bundled code.\n     */\n    code: string;\n    /**\n     * Source map.\n     */\n    map: SourceMap | undefined;\n};\n\ntype ResolveTsconfigPathOptions = {\n    cwd: string;\n    tsconfigPath?: string;\n};\n\nconst resolveTsconfigPath = (\n    options: ResolveTsconfigPathOptions,\n): string | undefined => {\n    if (!options.tsconfigPath) return void 0;\n\n    if (Path.isAbsolute(options.tsconfigPath)) {\n        return options.tsconfigPath;\n    }\n\n    return Path.resolve(options.cwd, options.tsconfigPath);\n};\n\n/**\n * Bundle function to bundle entry file for reference.\n */\nconst bundle = async (options: BundleOptions): Promise<BundleResult> => {\n    const bundlerName: string = options.context.bundler.name;\n\n    const tsconfigPath: string | undefined = resolveTsconfigPath({\n        cwd: options.cwd,\n        tsconfigPath: options.tsconfigPath,\n    });\n\n    const bundled: RolldownBuild = await rolldown({\n        input: options.file,\n        cwd: options.cwd,\n        logLevel: \"silent\",\n        transform: {\n            jsx: \"preserve\",\n        },\n        treeshake: false,\n        tsconfig: tsconfigPath,\n        plugins: [\n            ...(bundlerName === \"webpack\" || bundlerName === \"rspack\"\n                ? [\n                      tsConfigPaths({\n                          tsConfigPath: tsconfigPath,\n                      }),\n                  ]\n                : []),\n            externalResolver({\n                packageName: options.packageName,\n                file: options.file,\n                include: options.include,\n                exclude: options.exclude,\n            }),\n            entryOverrider({\n                file: options.file,\n                code: options.code,\n            }),\n            filePreprocessor({\n                context: options.context,\n                packageName: options.packageName,\n                file: options.file,\n                includedFunctions: options.includedFunctions,\n            }),\n        ],\n        experimental: {\n            attachDebugInfo: \"none\",\n        },\n    });\n\n    const result: RolldownOutput = await bundled.generate({\n        minify: false,\n    });\n\n    const output: OutputChunk = result.output[0];\n\n    return {\n        code: output.code,\n        map: output.map ?? void 0,\n    };\n};\n\nexport type {\n    BundleOptions,\n    BundleResult,\n    DynamicBundleOptions,\n    PresetBundleOptions,\n    UserBundleOptions,\n};\nexport { bundle };\n"],"mappings":";;;;;;;AAsGA,MAAM,uBACF,YACqB;CACrB,IAAI,CAAC,QAAQ,cAAc,OAAO,KAAK;CAEvC,IAAI,KAAK,WAAW,QAAQ,YAAY,GACpC,OAAO,QAAQ;CAGnB,OAAO,KAAK,QAAQ,QAAQ,KAAK,QAAQ,YAAY;AACzD;;;;AAKA,MAAM,SAAS,OAAO,YAAkD;CACpE,MAAM,cAAsB,QAAQ,QAAQ,QAAQ;CAEpD,MAAM,eAAmC,oBAAoB;EACzD,KAAK,QAAQ;EACb,cAAc,QAAQ;CAC1B,CAAC;CA6CD,MAAM,UAAsB,OAJS,MAvCA,SAAS;EAC1C,OAAO,QAAQ;EACf,KAAK,QAAQ;EACb,UAAU;EACV,WAAW,EACP,KAAK,WACT;EACA,WAAW;EACX,UAAU;EACV,SAAS;GACL,GAAI,gBAAgB,aAAa,gBAAgB,WAC3C,CACI,cAAc,EACV,cAAc,aAClB,CAAC,CACL,IACA,CAAC;GACP,iBAAiB;IACb,aAAa,QAAQ;IACrB,MAAM,QAAQ;IACd,SAAS,QAAQ;IACjB,SAAS,QAAQ;GACrB,CAAC;GACD,eAAe;IACX,MAAM,QAAQ;IACd,MAAM,QAAQ;GAClB,CAAC;GACD,iBAAiB;IACb,SAAS,QAAQ;IACjB,aAAa,QAAQ;IACrB,MAAM,QAAQ;IACd,mBAAmB,QAAQ;GAC/B,CAAC;EACL;EACA,cAAc,EACV,iBAAiB,OACrB;CACJ,CAAC,EAE2C,CAAC,SAAS,EAClD,QAAQ,MACZ,CAAC,EAEiC,CAAC,OAAO;CAE1C,OAAO;EACH,MAAM,OAAO;EACb,KAAK,OAAO,OAAO,KAAK;CAC5B;AACJ"}