UNPKG

rollup-plugin-stats

Version:
99 lines (95 loc) 3.43 kB
function omit(data, keys) { const result = {}; const objectKeys = Object.keys(data); objectKeys.forEach((key) => { if (!keys.includes(key)) { result[key] = data[key]; } }); return result; } /** * Check if filepath should be excluded based on patterns */ function checkExcludeFilepath(filepath, patterns) { if (!patterns) { return false; } if (Array.isArray(patterns)) { let res = false; for (let i = 0; i <= patterns.length - 1 && res === false; i++) { res = checkExcludeFilepath(filepath, patterns[i]); } return res; } if (typeof patterns === 'function') { return patterns(filepath); } if (typeof patterns === 'string') { return Boolean(filepath.match(patterns)); } if ('test' in patterns) { return patterns.test(filepath); } return false; } /** * Extract bundler stats * * Shallow clone stats object before any processing using `omit` to * 1. resolve getters * 2. prevent changes to the stats object * * @NOTE structuredClone is not supported by rolldown-vite: https://github.com/vitejs/rolldown-vite/issues/128 */ function extractRollupStats(bundle, options = {}) { const { source = false, map = false, excludeAssets, excludeModules } = options; const output = {}; Object.entries(bundle).forEach(([bundleEntryFilepath, bundleEntryStats]) => { // Skip extraction if the entry filepath matches the exclude assets pattern if (checkExcludeFilepath(bundleEntryFilepath, excludeAssets)) { return; } if (bundleEntryStats.type === "asset") { const assetStatsOmitKeys = []; // Skip asset source if options.source is false if (!source) { assetStatsOmitKeys.push('source'); } output[bundleEntryFilepath] = omit(bundleEntryStats, assetStatsOmitKeys); return; } if (bundleEntryStats.type === "chunk") { const chunkStatsOmitKeys = []; // Skip chunk source if options.source is false if (!source) { chunkStatsOmitKeys.push('code'); } // Skip chunk map if options.map is false if (!map) { chunkStatsOmitKeys.push('map'); } const chunkStats = omit(bundleEntryStats, chunkStatsOmitKeys); // Extract chunk modules stats const chunkModulesStats = {}; Object.entries(chunkStats.modules).forEach(([bundleModuleFilepath, bundleModuleStats]) => { // Skip module extraction if the filepath matches the exclude modules pattern if (checkExcludeFilepath(bundleModuleFilepath, excludeModules)) { return; } const moduleStatsOmitKeys = []; // Skip module source if options.source is false if (!source) { moduleStatsOmitKeys.push('code'); } chunkModulesStats[bundleModuleFilepath] = omit(bundleModuleStats, moduleStatsOmitKeys); }); chunkStats.modules = chunkModulesStats; output[bundleEntryFilepath] = chunkStats; return; } }); return output; } export { extractRollupStats as default }; //# sourceMappingURL=extract.mjs.map