{"version":3,"file":"plugin.mjs","names":[],"sources":["../../src/plugin.ts"],"sourcesContent":["import extractStats, { type StatsOptions } from 'rollup-plugin-stats/extract';\n\nimport type { Plugin, OutputOptions } from './types';\nimport { type BundleTransformOptions, bundleToWebpackStats } from './transform';\nimport { type StatsWrite, statsWrite } from './write';\nimport { formatFileSize, getByteSize, resolveFilepath } from './utils';\n\nconst PLUGIN_NAME = 'webpackStats';\n\ntype WebpackStatsOptions = {\n /**\n * JSON file output fileName\n * default: webpack-stats.json\n */\n fileName?: string;\n /**\n * Custom file writer\n * @default - fs.write(FILENAME, JSON.stringify(STATS, null, 2));\n */\n write?: StatsWrite;\n} & Omit &\n BundleTransformOptions;\n\ntype WebpackStatsOptionsOrBuilder =\n | WebpackStatsOptions\n | ((outputOptions: OutputOptions) => WebpackStatsOptions);\n\nexport const webpackStats = (\n options: WebpackStatsOptionsOrBuilder = {}\n): Plugin => ({\n name: PLUGIN_NAME,\n async generateBundle(outputOptions, bundle) {\n const resolvedOptions =\n typeof options === 'function' ? options(outputOptions) : options;\n const {\n fileName,\n excludeAssets,\n excludeModules,\n write = statsWrite,\n ...transformOptions\n } = resolvedOptions;\n\n const rollupStats = extractStats(bundle, {\n excludeAssets,\n excludeModules,\n // Extract stats source to compute size\n source: true,\n });\n const stats = bundleToWebpackStats(rollupStats, transformOptions);\n const filepath = resolveFilepath(fileName, outputOptions.dir);\n\n try {\n const res = await write(\n filepath,\n stats as unknown as Record\n );\n const outputSize = getByteSize(res.content);\n\n this.info(\n `Stats saved to ${res.filepath} (${formatFileSize(outputSize)})`\n );\n } catch (error: unknown) {\n const message =\n error instanceof Error ? error.message : JSON.stringify(error);\n\n // Log error, but do not throw to allow the compilation to continue\n this.warn(message);\n }\n },\n});\n"],"mappings":";;;;;;AAOA,MAAM,cAAc;AAoBpB,MAAa,gBACX,UAAwC,EAAE,MAC9B;CACZ,MAAM;CACN,MAAM,eAAe,eAAe,QAAQ;EAG1C,MAAM,EACJ,UACA,eACA,gBACA,QAAQ,YACR,GAAG,qBANH,OAAO,YAAY,aAAa,QAAQ,cAAc,GAAG;EAe3D,MAAM,QAAQ,qBANM,aAAa,QAAQ;GACvC;GACA;GAEA,QAAQ;GACT,CAAC,EAC8C,iBAAiB;EACjE,MAAM,WAAW,gBAAgB,UAAU,cAAc,IAAI;AAE7D,MAAI;GACF,MAAM,MAAM,MAAM,MAChB,UACA,MACD;GACD,MAAM,aAAa,YAAY,IAAI,QAAQ;AAE3C,QAAK,KACH,kBAAkB,IAAI,SAAS,IAAI,eAAe,WAAW,CAAC,GAC/D;WACM,OAAgB;GACvB,MAAM,UACJ,iBAAiB,QAAQ,MAAM,UAAU,KAAK,UAAU,MAAM;AAGhE,QAAK,KAAK,QAAQ;;;CAGvB"}