UNPKG

rollup-plugin-webpack-stats

Version:

[![](https://img.shields.io/npm/v/rollup-plugin-webpack-stats.svg)](https://www.npmjs.com/package/rollup-plugin-webpack-stats) [![](https://img.shields.io/npm/dm/rollup-plugin-webpack-stats.svg)](https://www.npmjs.com/package/rollup-plugin-webpack-stats)

47 lines (43 loc) 1.78 kB
import extractStats from 'rollup-plugin-stats/extract'; import { b as bundleToWebpackStats, r as resolveFilepath, g as getByteSize, f as formatFileSize } from './chunks/transform.mjs'; import path from 'node:path'; import fs from 'node:fs/promises'; import 'path'; import 'node:crypto'; async function statsWrite(filepath, stats) { const content = JSON.stringify(stats, null, 2); // Create base directory if it does not exist await fs.mkdir(path.dirname(filepath), { recursive: true }); await fs.writeFile(filepath, content); return { filepath, content, }; } const PLUGIN_NAME = 'webpackStats'; const webpackStats = (options = {}) => ({ name: PLUGIN_NAME, async generateBundle(outputOptions, bundle) { const resolvedOptions = typeof options === 'function' ? options(outputOptions) : options; const { fileName, excludeAssets, excludeModules, write = statsWrite, ...transformOptions } = resolvedOptions; const rollupStats = extractStats(bundle, { excludeAssets, excludeModules, // Extract stats source to compute size source: true, }); const stats = bundleToWebpackStats(rollupStats, transformOptions); const filepath = resolveFilepath(fileName, outputOptions.dir); try { const res = await write(filepath, stats); const outputSize = getByteSize(res.content); this.info(`Stats saved to ${res.filepath} (${formatFileSize(outputSize)})`); } catch (error) { // eslint-disable-line // Log error, but do not throw to allow the compilation to continue this.warn(error); } }, }); export { webpackStats as default }; //# sourceMappingURL=index.mjs.map