UNPKG

rollup-plugin-webpack-stats

Version:

Rollup/Vite/Rolldown plugin to generate a stats JSON file with a bundle-stats webpack-compatible structure

31 lines (29 loc) 1.16 kB
import { formatFileSize, getByteSize, resolveFilepath } from "./utils.mjs"; import { bundleToWebpackStats } from "./transform.mjs"; import { statsWrite } from "./write.mjs"; import extractStats from "rollup-plugin-stats/extract"; //#region src/plugin.ts const PLUGIN_NAME = "webpackStats"; const webpackStats = (options = {}) => ({ name: PLUGIN_NAME, async generateBundle(outputOptions, bundle) { const { fileName, excludeAssets, excludeModules, write = statsWrite, ...transformOptions } = typeof options === "function" ? options(outputOptions) : options; const stats = bundleToWebpackStats(extractStats(bundle, { excludeAssets, excludeModules, source: true }), 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) { const message = error instanceof Error ? error.message : JSON.stringify(error); this.warn(message); } } }); //#endregion export { webpackStats }; //# sourceMappingURL=plugin.mjs.map