rollup-plugin-stats
Version:
Output Rollup stats
85 lines (81 loc) • 2.9 kB
JavaScript
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;
}
function extractRollupStats(bundle, options = {}) {
const { source = 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") {
let assetStats = bundleEntryStats;
// Skip asset source if options source is false
if (!source) {
assetStats = omit(assetStats, ['source']);
}
output[bundleEntryFilepath] = assetStats;
return;
}
if (bundleEntryStats.type === "chunk") {
let chunkStats = bundleEntryStats;
// Skip chunk source if options source is false
if (!source) {
chunkStats = omit(chunkStats, ['code']);
}
// 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;
}
let moduleStats = bundleModuleStats;
// Skip module source if options source is false
if (!source) {
moduleStats = omit(moduleStats, ['code']);
}
chunkModulesStats[bundleModuleFilepath] = moduleStats;
});
chunkStats.modules = chunkModulesStats;
output[bundleEntryFilepath] = chunkStats;
return;
}
});
return output;
}
export { extractRollupStats as default };
//# sourceMappingURL=extract.mjs.map