Unplugin Build Time Statistics
unplugin-time-stat is a utility that provides unified build time stat reporting across various JavaScript bundlers, including Vite, Webpack, Rspack, Rollup, and esbuild, by leveraging the `unplugin` ecosystem. It addresses the common challenge of inconsistent build performance reporting mechanisms across different build tools, offering a single interface to monitor and report build durations. The current stable version is `0.3.0`. The project has seen regular, feature-driven releases, indicating active development. A key differentiator is its ability to integrate custom hooks, allowing developers to programmatically capture build metrics and integrate them with external services or custom reporting tools, beyond just console output. This makes it a versatile tool for performance monitoring in diverse build environments.
Common errors
-
Cannot find module 'unplugin-time-stat/vite'
cause The specific bundler integration module could not be found, often due to a typo in the import path or a missing `unplugin-time-stat` package installation.fixVerify the package is installed (`npm install unplugin-time-stat`) and that the import path matches your bundler (e.g., `unplugin-time-stat/vite`, `unplugin-time-stat/webpack`). -
TypeError: TimeStat is not a function
cause This error occurs when trying to invoke `TimeStat` as a function, but the imported value is not a function. This can happen with incorrect CommonJS `require` usage for an ESM default export, or vice-versa.fixEnsure you are using the correct import syntax for your environment: `import TimeStat from '...'` for ESM, and `const TimeStat = require('...')` for CommonJS, especially for Webpack/Rspack integrations.
Warnings
- gotcha This package acts as an `unplugin` wrapper, requiring you to import the specific bundler integration (e.g., `/vite`, `/webpack`). Importing the wrong path for your bundler will lead to runtime errors or the plugin not functioning.
- gotcha Using `unplugin-time-stat` requires the corresponding bundler package (e.g., `vite`, `webpack`, `rollup`, `esbuild`, `@nuxt/kit`) as a peer dependency. If the peer dependency is not installed, the plugin might fail to initialize or cause resolution errors.
- gotcha As a `0.x.x` version package, the API of `unplugin-time-stat` might undergo minor changes in future releases. While recent updates have been additive, always review release notes for potential adjustments.
Install
-
npm install unplugin-time-stat -
yarn add unplugin-time-stat -
pnpm add unplugin-time-stat
Imports
- TimeStat (Vite)
const TimeStat = require('unplugin-time-stat/vite')import TimeStat from 'unplugin-time-stat/vite'
- TimeStat (Webpack)
import TimeStat from 'unplugin-time-stat/webpack'
const TimeStat = require('unplugin-time-stat/webpack') - TimeStat (Nuxt module)
import TimeStat from 'unplugin-time-stat/nuxt'
export default defineNuxtConfig({ modules: [['unplugin-time-stat/nuxt', {}]] }) - TimeStatHook
import type { TimeStatHook } from 'unplugin-time-stat'
Quickstart
import { defineConfig } from 'vite'
import TimeStat from 'unplugin-time-stat/vite'
function metrics(buildTime, raw) {
// raw contains { start: Date, end: Date, buildTime: string }
const duration = raw.end.getTime() - raw.start.getTime();
console.log(`[Custom Metrics] Build finished in ${duration}ms`);
// Example: send data to an external monitoring service
// sendToDatadog('build_time', duration);
// Optionally return a string to be displayed in the console
return `Custom Report: ${duration}ms`;
}
export default defineConfig({
plugins: [
TimeStat({
hook: metrics,
// Optional: disable default console output if you only want custom handling
// outputConsole: false
})
]
})