{"version":3,"file":"extract.mjs","names":[],"sources":["../src/extract.ts"],"sourcesContent":["import type {\n OutputAsset,\n OutputBundle,\n OutputChunk,\n RenderedModule,\n} from 'rollup';\nimport { omit } from './utils/omit';\nimport {\n type ExcludeFilepathPatterns,\n checkExcludeFilepath,\n} from './utils/check-exclude-filepath';\n\nexport type AssetStatsOptionalProperties = {\n source?: OutputAsset['source'];\n};\n\nexport type AssetStats = Omit &\n AssetStatsOptionalProperties;\n\nexport type ModuleStatsOptionalProperties = {\n code?: RenderedModule['code'] | null;\n};\n\nexport type ModuleStats = Omit<\n RenderedModule,\n keyof ModuleStatsOptionalProperties\n> &\n ModuleStatsOptionalProperties;\n\nexport type ChunkStatsOptionalProperties = {\n code?: OutputChunk['code'];\n map?: OutputChunk['map'];\n};\n\nexport type ChunkStats = Omit<\n OutputChunk,\n keyof ChunkStatsOptionalProperties | 'modules'\n> & {\n modules: Record;\n} & ChunkStatsOptionalProperties;\n\nexport type Stats = Record;\n\nexport type StatsOptions = {\n /**\n * Output asset/module sources\n * @default false\n */\n source?: boolean;\n /**\n * Output chunk map\n * @default false\n */\n map?: boolean;\n /**\n * Exclude matching assets\n */\n excludeAssets?: ExcludeFilepathPatterns;\n /**\n * Exclude matching modules\n */\n excludeModules?: ExcludeFilepathPatterns;\n};\n\n/**\n * Extract bundler stats\n *\n * Shallow clone stats object before any processing using `omit` to\n * 1. resolve getters\n * 2. prevent changes to the stats object\n *\n * @NOTE structuredClone is not supported by rolldown-vite: https://github.com/vitejs/rolldown-vite/issues/128\n */\nexport default function extractRollupStats(\n bundle: OutputBundle,\n options: StatsOptions = {}\n): Stats {\n const {\n source = false,\n map = false,\n excludeAssets,\n excludeModules,\n } = options;\n\n const output: Stats = {};\n\n Object.entries(bundle).forEach(([bundleEntryFilepath, bundleEntryStats]) => {\n // Skip extraction if the entry filepath matches the exclude assets pattern\n if (checkExcludeFilepath(bundleEntryFilepath, excludeAssets)) {\n return;\n }\n\n if (bundleEntryStats.type === 'asset') {\n const assetStatsOmitKeys: Array = [];\n\n // Skip asset source if options.source is false\n if (!source) {\n assetStatsOmitKeys.push('source');\n }\n\n output[bundleEntryFilepath] = omit(\n bundleEntryStats,\n assetStatsOmitKeys\n ) as AssetStats;\n\n return;\n }\n\n if (bundleEntryStats.type === 'chunk') {\n const chunkStatsOmitKeys: Array = [];\n\n // Skip chunk source if options.source is false\n if (!source) {\n chunkStatsOmitKeys.push('code');\n }\n\n // Skip chunk map if options.map is false\n if (!map) {\n chunkStatsOmitKeys.push('map');\n }\n\n const chunkStats = omit(\n bundleEntryStats,\n chunkStatsOmitKeys\n ) as ChunkStats;\n\n // Extract chunk modules stats\n const chunkModulesStats: ChunkStats['modules'] = {};\n\n Object.entries(chunkStats.modules).forEach(\n ([bundleModuleFilepath, bundleModuleStats]) => {\n // Skip module extraction if the filepath matches the exclude modules pattern\n if (checkExcludeFilepath(bundleModuleFilepath, excludeModules)) {\n return;\n }\n\n const moduleStatsOmitKeys: Array<\n keyof ModuleStatsOptionalProperties\n > = [];\n\n // Skip module source if options.source is false\n if (!source) {\n moduleStatsOmitKeys.push('code');\n }\n\n chunkModulesStats[bundleModuleFilepath] = omit(\n bundleModuleStats,\n moduleStatsOmitKeys\n ) as ModuleStats;\n }\n );\n\n chunkStats.modules = chunkModulesStats;\n\n output[bundleEntryFilepath] = chunkStats;\n\n return;\n }\n });\n\n return output;\n}\n"],"mappings":";;;;;;;;;;;;;AAyEA,SAAwB,mBACtB,QACA,UAAwB,EAAE,EACnB;CACP,MAAM,EACJ,SAAS,OACT,MAAM,OACN,eACA,mBACE;CAEJ,MAAM,SAAgB,EAAE;AAExB,QAAO,QAAQ,OAAO,CAAC,SAAS,CAAC,qBAAqB,sBAAsB;AAE1E,MAAI,qBAAqB,qBAAqB,cAAc,CAC1D;AAGF,MAAI,iBAAiB,SAAS,SAAS;GACrC,MAAM,qBAAgE,EAAE;AAGxE,OAAI,CAAC,OACH,oBAAmB,KAAK,SAAS;AAGnC,UAAO,uBAAuB,KAC5B,kBACA,mBACD;AAED;;AAGF,MAAI,iBAAiB,SAAS,SAAS;GACrC,MAAM,qBAAgE,EAAE;AAGxE,OAAI,CAAC,OACH,oBAAmB,KAAK,OAAO;AAIjC,OAAI,CAAC,IACH,oBAAmB,KAAK,MAAM;GAGhC,MAAM,aAAa,KACjB,kBACA,mBACD;GAGD,MAAM,oBAA2C,EAAE;AAEnD,UAAO,QAAQ,WAAW,QAAQ,CAAC,SAChC,CAAC,sBAAsB,uBAAuB;AAE7C,QAAI,qBAAqB,sBAAsB,eAAe,CAC5D;IAGF,MAAM,sBAEF,EAAE;AAGN,QAAI,CAAC,OACH,qBAAoB,KAAK,OAAO;AAGlC,sBAAkB,wBAAwB,KACxC,mBACA,oBACD;KAEJ;AAED,cAAW,UAAU;AAErB,UAAO,uBAAuB;AAE9B;;GAEF;AAEF,QAAO"}