rollup-plugin-webpack-stats
Version:
Rollup/Vite/Rolldown plugin to generate a stats JSON file with a bundle-stats webpack-compatible structure
1 lines • 4.33 kB
Source Map (JSON)
{"version":3,"file":"plugin.cjs","names":["statsWrite","bundleToWebpackStats","resolveFilepath","getByteSize","formatFileSize"],"sources":["../src/plugin.ts"],"sourcesContent":["import type { OutputBundle } from 'rollup-plugin-stats';\nimport extractStats, { type StatsOptions } from 'rollup-plugin-stats/extract';\n\nimport { type BundleTransformOptions, bundleToWebpackStats } from './transform';\nimport { type StatsWrite, statsWrite } from './write';\nimport { formatFileSize, getByteSize, resolveFilepath } from './utils';\n\nconst PLUGIN_NAME = 'webpackStats';\n\n/**\n * A subset of resolved output options provided to the `generateBundle` hook by Vite/Rolldown/Rollup,\n * containing only the fields this plugin uses to generate a stats file for a specific output.\n */\nexport type OutputOptions = {\n /** Output directory for the generated files. */\n dir?: string | undefined;\n\n /** Output format */\n format?:\n | 'es'\n | 'esm'\n | 'module'\n | 'cjs'\n | 'commonjs'\n | 'iife'\n | 'umd'\n | 'amd'\n | 'system'\n | 'systemjs'\n | undefined;\n};\n\n/**\n * Subset of the Vite/Rolldown/Rollup plugin hook context (`this`) used by this plugin.\n */\ntype PluginContext = {\n /** Log an informational message through Vite/Rolldown/Rollup's logging pipeline. */\n info: (message: string) => void;\n\n /** Log a warning through Vite/Rolldown/Rollup's logging pipeline without stopping the build. */\n warn: (message: string) => void;\n};\n\n/**\n * Minimum plugin interface compatible with Vite/Rolldown/Rollup.\n */\nexport type Plugin = {\n /** Unique identifier for the plugin, used in error messages and logs. */\n name: string;\n\n /**\n * Hook called after the bundle has been fully generated but before it is\n * written to disk. Receives the resolved output options and the complete\n * output bundle map.\n */\n generateBundle?: (\n this: PluginContext,\n outputOptions: OutputOptions,\n bundle: OutputBundle,\n isWrite: boolean\n ) => void | Promise<void>;\n};\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<StatsOptions, 'source' | 'map'> &\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<string, unknown>\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;AA0EpB,MAAa,gBACX,UAAwC,CAAC,OAC7B;CACZ,MAAM;CACN,MAAM,eAAe,eAAe,QAAQ;EAG1C,MAAM,EACJ,UACA,eACA,gBACA,QAAQA,0BACR,GAAG,qBANH,OAAO,YAAY,aAAa,QAAQ,aAAa,IAAI;EAe3D,MAAM,QAAQC,gFANmB,QAAQ;GACvC;GACA;GAEA,QAAQ;EACV,CAC6C,GAAG,gBAAgB;EAChE,MAAM,WAAWC,8BAAgB,UAAU,cAAc,GAAG;EAE5D,IAAI;GACF,MAAM,MAAM,MAAM,MAChB,UACA,KACF;GACA,MAAM,aAAaC,0BAAY,IAAI,OAAO;GAE1C,KAAK,KACH,kBAAkB,IAAI,SAAS,IAAIC,6BAAe,UAAU,EAAE,EAChE;EACF,SAAS,OAAgB;GACvB,MAAM,UACJ,iBAAiB,QAAQ,MAAM,UAAU,KAAK,UAAU,KAAK;GAG/D,KAAK,KAAK,OAAO;EACnB;CACF;AACF"}