Last active
November 7, 2024 13:59
-
-
Save pi0/c3d8fb97c035cc4f2929b55ccb072a1a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reportRollupTiming(build) { | |
if (!build.getTimings) { | |
console.log( | |
"Rollup timing information is not available. Make sure `perf: true` config is set." | |
); | |
return; | |
} | |
const formatTime = (ms) => | |
ms > 1000 ? `${(ms / 1000).toLocaleString()}s` : `${ms.toLocaleString()}ms`; | |
const timings = build.getTimings(); | |
const timingGroups = []; | |
let currentGroup; | |
for (const key in timings) { | |
const time = timings[key][0]; | |
const label = key.replace(/^[#-\s]+/, ""); | |
if (key.startsWith("#")) { | |
currentGroup = { label, time, items: [] }; | |
timingGroups.push(currentGroup); | |
} else { | |
currentGroup.items.push({ label, time }); | |
} | |
} | |
let log = []; | |
for (const group of timingGroups) { | |
const totalMeasured = group.items.reduce((t, i) => t + i.time, 0); | |
if (group.time !== totalMeasured && group.items.length) { | |
group.items.push({ | |
label: "(unknown)", | |
time: group.time - totalMeasured, | |
}); | |
} | |
group.items.sort((a, b) => b.time - a.time); | |
log.push(`\n${group.label} (${formatTime(group.time)})`); | |
for (const item of group.items) { | |
log.push(` ${item.label.padEnd(40, " ")} ${formatTime(item.time)}`); | |
} | |
} | |
console.log(`\n${log.join("\n")}\n`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment