Next.js configuration wrapper that integrates webpack-bundle-analyzer for bundle analysis
npx @tessl/cli install tessl/npm-next--bundle-analyzer@15.5.0Next.js Bundle Analyzer is a configuration wrapper that integrates webpack-bundle-analyzer into Next.js projects. It provides a higher-order function that conditionally enables bundle analysis based on configuration, generating detailed reports for client-side, server-side (Node.js), and edge runtime bundles to help developers optimize their application bundles.
npm install @next/bundle-analyzerconst withBundleAnalyzer = require('@next/bundle-analyzer');For TypeScript:
import withBundleAnalyzer from '@next/bundle-analyzer';// next.config.js
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
});
module.exports = withBundleAnalyzer({
// Your Next.js config
});Build with analysis enabled:
ANALYZE=true npm run buildThis generates bundle analysis reports in <distDir>/analyze/:
client.html - Client-side bundle analysisnodejs.html - Node.js server bundle analysisedge.html - Edge runtime bundle analysisCreates a Next.js configuration wrapper that conditionally enables webpack-bundle-analyzer.
/**
* Creates a Next.js configuration wrapper with webpack-bundle-analyzer integration
* @param options - Configuration options for bundle analysis
* @returns Function that takes Next.js config and returns enhanced config
*/
function NextBundleAnalyzer(options?: {
/** Whether to enable bundle analysis (default: true) */
enabled?: boolean;
/** Whether to automatically open the report in browser */
openAnalyzer?: boolean;
/** Output format - 'static' for HTML, 'json' for JSON data (default: 'static') */
analyzerMode?: 'json' | 'static';
/** Log level for bundle analyzer (default: 'info') */
logLevel?: 'info' | 'warn' | 'error' | 'silent' | undefined;
}): (config?: NextConfig) => NextConfig;Usage Examples:
// Basic usage with environment variable control
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
});
module.exports = withBundleAnalyzer({
// Your Next.js configuration
experimental: {
appDir: true,
},
});// Advanced configuration
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
openAnalyzer: false,
analyzerMode: 'json',
logLevel: 'warn',
});
module.exports = withBundleAnalyzer({
// Your Next.js configuration
});// Use with next-compose-plugins
const withPlugins = require('next-compose-plugins');
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
});
module.exports = withPlugins([
[withBundleAnalyzer],
// other plugins here
]);// Configuration as a function
module.exports = (phase, defaultConfig) => {
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
});
return withBundleAnalyzer(defaultConfig);
};The bundle analyzer accepts an options object with the following properties:
booleantrueprocess.env.ANALYZE === 'true' for conditional activation.booleanundefined (uses webpack-bundle-analyzer default)'json' | 'static''static''static' generates HTML files, 'json' generates JSON data files.'info' | 'warn' | 'error' | 'silent' | undefined'info'import type { NextConfig } from 'next';
interface BundleAnalyzerOptions {
enabled?: boolean;
openAnalyzer?: boolean;
analyzerMode?: 'json' | 'static';
logLevel?: 'info' | 'warn' | 'error' | 'silent' | undefined;
}
type ConfigEnhancer = (config?: NextConfig) => NextConfig;When enabled, the bundle analyzer generates reports in the following locations within your Next.js build output directory:
Client Bundle: analyze/client.html (or .json)
Node.js Server Bundle: analyze/nodejs.html (or .json)
Edge Runtime Bundle: analyze/edge.html (or .json)
If installing as a devDependency, wrap the require in a process.env check in your next.config.js since the config file is loaded during next start:
const withBundleAnalyzer = process.env.NODE_ENV === 'development'
? require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
})
: (config) => config;
module.exports = withBundleAnalyzer({
// Your Next.js config
});This package depends on webpack-bundle-analyzer@4.10.1 which provides the core bundle analysis functionality.