Skip to main content
hooksSource-backedReview first Safety Privacy

Webpack Bundle Analyzer - Hooks

Analyzes webpack bundle size when webpack config or entry files are modified.

by JSONbored·added 2025-09-19·
HarnessClaude Code
Trigger:PostToolUse
Review first review before installing

Open the source and read safety notes before installing.

Citation facts

Source-backed facts for citing this resource, derived directly from the registry — also available as plain text for AI assistants.

Source URLs
https://github.com/webpack-contrib/webpack-bundle-analyzer, https://github.com/JSONbored/awesome-claude/blob/main/content/hooks/webpack-bundle-analyzer.mdx
Safety notes
Runs automatically on its configured Claude Code hook event and executes shell logic that can read, modify, or delete files in your project (and may run builds, installs, or network calls); review the script and scope it to expected paths before enabling.
Privacy notes
Receives Claude Code hook input (session metadata, file paths, and tool output) and reads local project files; review what the script logs or forwards to external services and keep credentials out of its output.
Author
JSONbored
Claim status
unclaimed
Last verified
2025-09-19

Decision playbook

Review trust signals before you adopt

Signals are present but mixed. Use the checklist below to confirm the source and operational safety for your environment.

Compare context
Selected

0

Current score

78

Baseline

Delta

No baseline selected

No major trust-signal divergence detected in the current selection.

Source and provenance checks

Complete

Confirm ownership and provenance before trusting install instructions.

  • Source link availableRequired

    Open the canonical repository and verify ownership.

    Done
  • Source provenance statusRequired

    Marked as source-backed.

    Done
  • Metadata reviewed

    Registry metadata indicates a reviewed listing.

    Done

Safety and privacy checks

Complete

Validate risk disclosures before installation or API wiring.

  • Safety notes presentRequired

    Review the listed safety guidance before running commands.

    Done
  • Privacy notes presentRequired

    Review data handling notes before connecting accounts or secrets.

    Done
  • Trust level risk gateRequired

    Trust level does not block evaluation.

    Done

Package and install checks

Needs review

Check package metadata and artifact integrity signals.

  • Install payload available

    Install or copy payload is available for review.

    Done
  • Package verification flag

    No package verification flag provided.

    Pending
  • Checksum metadata

    No checksum provided for downloaded artifact.

    Pending

Compare-driven decision checks

Needs review

Use compare context to validate trade-offs before adoption.

  • Compare tray has multiple entries

    Add at least one more entry to compare trust differences.

    Pending
  • Baseline comparison available

    No baseline peer selected yet.

    Pending
  • Diverging trust signals identified

    No major trust-signal divergence found.

    Pending

Setup at a glance

CLI install

Copy-ready — paste the snippet to get started.

Install command

Provided

Config snippet

Provided

Copy snippet

Provided

Prerequisites

None

Platforms

1 listed

Difficulty

0/100

Adoption plan

Balanced adoption plan

Current risk score 16/100. Use staged verification before broader rollout.

Risk 16

Pre-adoption checks

Validate source and review signals before any execution.

  • Confirm source provenanceRequired

    Source URL/provenance metadata is present.

    Done
  • Confirm metadata review state

    Listing has review metadata.

    Done
  • Verify install payload

    Install/config payload exists and can be inspected.

    Done

Security checks

Confirm safety, privacy, and package integrity signals.

  • Review safety notesRequired

    Safety notes are present.

    Done
  • Review privacy notesRequired

    Privacy notes are present.

    Done
  • Verify package integrity metadata

    No package verification/checksum metadata.

    Pending

Rollout

Adopt in controlled steps based on the selected plan.

  • Run in isolated sandbox firstRequired

    Use a constrained sandbox and observe behavior across multiple tasks.

    Pending
  • Roll out graduallyRequired

    Roll out to a small cohort before wider usage.

    Pending
  • Set monitoring and fallback

    Define rollback path and monitor errors after adoption.

    Pending

Evidence readiness

Evidence readiness matrix · balanced

Required evidence gates are covered (5/6 signals complete).

Risk 15

Source provenance

Present

Source repository/provenance is listed.

Required in this preset

Metadata review

Present

Review metadata is present.

Required in this preset

Safety notes

Present

Safety notes are present.

Required in this preset

Privacy notes

Present

Privacy notes are present.

Optional in this preset

Package integrity

Missing

Package integrity metadata is missing.

Optional in this preset

Install payload

Present

Install payload is available.

Required in this preset

Required evidence gates are covered for this preset.

Decision timeline

Decision timeline · balanced

5/6 steps complete with no blocking gaps for this preset.

Risk 14

triage

Confirm source provenanceRequired

Source/provenance metadata is available.

Done

triage

Check metadata review statusRequired

Review metadata is available.

Done

verify

Review safety notesRequired

Safety notes are available.

Done

verify

Review privacy notes

Privacy notes are available.

Done

verify

Validate package integrity metadata

Package integrity metadata is missing.

Pending

rollout

Verify install payload and commandsRequired

Install payload is available.

Done

No required blockers for this timeline preset.

Safety & privacy surface

Safety & privacy surface

1 safety and 1 privacy notes across 2 risk areas. Review closely: credentials & tokens, permissions & scopes.

2 areas
  • SafetyPermissions & scopesRuns automatically on its configured Claude Code hook event and executes shell logic that can read, modify, or delete files in your project (and may run builds, installs, or network calls); review the script and scope it to expected paths before enabling.
  • PrivacyCredentials & tokensReceives Claude Code hook input (session metadata, file paths, and tool output) and reads local project files; review what the script logs or forwards to external services and keep credentials out of its output.

Safety notes

  • Runs automatically on its configured Claude Code hook event and executes shell logic that can read, modify, or delete files in your project (and may run builds, installs, or network calls); review the script and scope it to expected paths before enabling.

Privacy notes

  • Receives Claude Code hook input (session metadata, file paths, and tool output) and reads local project files; review what the script logs or forwards to external services and keep credentials out of its output.

Schema details

Install type
cli
Reading time
5 min
Difficulty score
0
Troubleshooting
Yes
Breaking changes
No
Skill and platform metadata
Retrieval sources
https://github.com/webpack-contrib/webpack-bundle-analyzerhttps://code.claude.com/docs/en/hooks
Runtime and command metadata
Trigger
PostToolUse
Script language
bash
Script body
#!/bin/bash

# Read the tool input from stdin
INPUT=$(cat)
TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name')
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // ""')

if [ -z "$FILE_PATH" ]; then
  exit 0
fi

echo "📊 Webpack Bundle Analyzer - Analyzing bundle performance..."
echo "📄 File: $FILE_PATH"

# Check if this is a relevant file for bundle analysis
RELEVANT_FILE=false

if [[ "$FILE_PATH" == *webpack.config.js ]] || \
   [[ "$FILE_PATH" == *webpack.config.ts ]] || \
   [[ "$FILE_PATH" == *vite.config.js ]] || \
   [[ "$FILE_PATH" == *vite.config.ts ]] || \
   [[ "$FILE_PATH" == *rollup.config.js ]] || \
   [[ "$FILE_PATH" == *src/index.js ]] || \
   [[ "$FILE_PATH" == *src/index.ts ]] || \
   [[ "$FILE_PATH" == *src/main.js ]] || \
   [[ "$FILE_PATH" == *src/main.ts ]] || \
   [[ "$FILE_PATH" == *package.json ]]; then
    RELEVANT_FILE=true
fi

if [ "$RELEVANT_FILE" = false ]; then
    echo "ℹ️ File does not require bundle analysis: $FILE_PATH"
    exit 0
fi

echo "🔍 Detecting build system and configuration..."

# Detect build system
BUILD_SYSTEM="unknown"
if [ -f "webpack.config.js" ] || [ -f "webpack.config.ts" ]; then
    BUILD_SYSTEM="webpack"
    echo "📦 Webpack configuration detected"
elif [ -f "vite.config.js" ] || [ -f "vite.config.ts" ]; then
    BUILD_SYSTEM="vite"
    echo "⚡ Vite configuration detected"
elif [ -f "rollup.config.js" ] || [ -f "rollup.config.ts" ]; then
    BUILD_SYSTEM="rollup"
    echo "🎯 Rollup configuration detected"
elif [ -f "package.json" ] && grep -q '"react-scripts"' package.json 2>/dev/null; then
    BUILD_SYSTEM="cra"
    echo "⚛️ Create React App detected"
elif [ -f "next.config.js" ] || [ -f "next.config.ts" ]; then
    BUILD_SYSTEM="next"
    echo "🔺 Next.js configuration detected"
else
    echo "❓ No recognized build system found"
fi

# Check for bundle analyzer availability
ANALYZER_AVAILABLE=false
if command -v npx >/dev/null 2>&1; then
    if npx webpack-bundle-analyzer --version >/dev/null 2>&1; then
        ANALYZER_AVAILABLE=true
        echo "✅ webpack-bundle-analyzer available"
    else
        echo "⚠️ webpack-bundle-analyzer not available - install with: npm install --save-dev webpack-bundle-analyzer"
    fi
else
    echo "⚠️ npx not available - please install Node.js"
fi

# Perform bundle analysis based on build system
case "$BUILD_SYSTEM" in
    "webpack")
        echo "📊 Analyzing Webpack bundle..."
        
        # Check if dist directory exists
        if [ ! -d "dist" ] && [ ! -d "build" ]; then
            echo "🏗️ Building project to generate bundle..."
            if npm run build 2>/dev/null; then
                echo "✅ Build completed successfully"
            else
                echo "❌ Build failed - cannot analyze bundle"
                exit 1
            fi
        fi
        
        # Find stats file or generate one
        STATS_FILE=""
        if [ -f "dist/stats.json" ]; then
            STATS_FILE="dist/stats.json"
        elif [ -f "build/stats.json" ]; then
            STATS_FILE="build/stats.json"
        else
            echo "📈 Generating webpack stats..."
            if npx webpack --profile --json > webpack-stats.json 2>/dev/null; then
                STATS_FILE="webpack-stats.json"
                echo "✅ Stats file generated: $STATS_FILE"
            else
                echo "❌ Failed to generate webpack stats"
                exit 1
            fi
        fi
        
        # Run bundle analyzer
        if [ "$ANALYZER_AVAILABLE" = true ] && [ -n "$STATS_FILE" ]; then
            echo "🔍 Running bundle analysis..."
            if npx webpack-bundle-analyzer "$STATS_FILE" --mode static --report bundle-report.html --no-open 2>/dev/null; then
                echo "✅ Bundle analysis completed"
                echo "📊 Report saved to: bundle-report.html"
                echo "🌐 View report: file://$(pwd)/bundle-report.html"
            else
                echo "⚠️ Bundle analysis failed"
            fi
        fi
        ;;
    "vite")
        echo "⚡ Analyzing Vite bundle..."
        
        # Check if Vite has bundle analysis plugin
        if grep -q 'vite-bundle-analyzer\|rollup-plugin-analyzer' package.json 2>/dev/null; then
            echo "📊 Running Vite bundle analysis..."
            if npm run build -- --analyze 2>/dev/null; then
                echo "✅ Vite bundle analysis completed"
            else
                echo "⚠️ Vite bundle analysis failed"
            fi
        else
            echo "💡 Install vite-bundle-analyzer for detailed analysis:"
            echo "    npm install --save-dev vite-bundle-analyzer"
            
            # Basic build size analysis
            echo "📏 Running basic build analysis..."
            if npm run build 2>/dev/null; then
                if [ -d "dist" ]; then
                    echo "📊 Build output analysis:"
                    find dist -name "*.js" -exec du -h {} \; | sort -hr | head -10
                fi
            fi
        fi
        ;;
    "next")
        echo "🔺 Analyzing Next.js bundle..."
        
        if grep -q '@next/bundle-analyzer' package.json 2>/dev/null; then
            echo "📊 Running Next.js bundle analysis..."
            if ANALYZE=true npm run build 2>/dev/null; then
                echo "✅ Next.js bundle analysis completed"
            else
                echo "⚠️ Next.js bundle analysis failed"
            fi
        else
            echo "💡 Install @next/bundle-analyzer for detailed analysis:"
            echo "    npm install --save-dev @next/bundle-analyzer"
        fi
        ;;
    "cra")
        echo "⚛️ Analyzing Create React App bundle..."
        
        if npm list --depth=0 | grep -q 'source-map-explorer' 2>/dev/null; then
            echo "📊 Running source-map-explorer..."
            if npm run build && npx source-map-explorer 'build/static/js/*.js' 2>/dev/null; then
                echo "✅ CRA bundle analysis completed"
            else
                echo "⚠️ CRA bundle analysis failed"
            fi
        else
            echo "💡 Install source-map-explorer for detailed analysis:"
            echo "    npm install --save-dev source-map-explorer"
        fi
        ;;
    *)
        echo "❓ Unknown build system - attempting generic analysis"
        
        # Try to analyze any existing build output
        for dir in dist build public; do
            if [ -d "$dir" ]; then
                echo "📊 Analyzing $dir directory:"
                find "$dir" -name "*.js" -o -name "*.css" | head -10 | while read -r file; do
                    size=$(du -h "$file" 2>/dev/null | cut -f1)
                    echo "  • $file: $size"
                done
            fi
        done
        ;;
esac

# General optimization suggestions
echo ""
echo "💡 Bundle Optimization Tips:"
echo "  • Enable tree-shaking to remove unused code"
echo "  • Use dynamic imports for code splitting"
echo "  • Optimize images and static assets"
echo "  • Use compression (gzip/brotli) for production"
echo "  • Analyze and remove large unnecessary dependencies"
echo "  • Use webpack-bundle-analyzer for detailed insights"
echo "  • Consider lazy loading for non-critical components"
echo "  • Review and optimize polyfills"

echo ""
echo "📊 Bundle Analysis Tools:"
echo "  • Webpack: webpack-bundle-analyzer"
echo "  • Vite: vite-bundle-analyzer"
echo "  • Next.js: @next/bundle-analyzer"
echo "  • CRA: source-map-explorer"
echo "  • General: bundlephobia.com for dependency analysis"

echo ""
echo "🎯 Bundle analysis complete!"

exit 0
Full copyable content
{
  "hooks": {
    "postToolUse": {
      "script": "./.claude/hooks/webpack-bundle-analyzer.sh",
      "matchers": [
        "write",
        "edit"
      ]
    }
  }
}

About this resource

Features

  • Interactive bundle size visualization and analysis including bundle visualization (interactive bundle size visualization with treemap visualization, bundle size analysis with size analysis, bundle composition analysis with composition analysis, bundle visualization reporting with visualization status), visualization management (visualization configuration with visualization settings, visualization customization with custom visualization, visualization monitoring with visualization tracking, visualization updates with visualization updates), visualization optimization (visualization performance optimization with performance improvement, visualization accuracy optimization with accuracy improvement, visualization completeness optimization with completeness improvement, visualization analytics with visualization statistics), and visualization reporting (bundle visualization reporting with visualization status, bundle size reporting with size status, bundle composition reporting with composition status, visualization analytics with visualization statistics)
  • Automatic bundle analysis on configuration changes including automatic analysis (automatic bundle analysis on configuration changes with change detection, configuration change detection with change identification, bundle analysis automation with automatic analysis, analysis reporting with analysis status), analysis management (analysis configuration with analysis settings, analysis customization with custom analysis, analysis monitoring with analysis tracking, analysis updates with analysis updates), analysis optimization (analysis performance optimization with performance improvement, analysis accuracy optimization with accuracy improvement, analysis efficiency optimization with efficiency improvement, analysis analytics with analysis statistics), and analysis reporting (automatic analysis reporting with analysis status, configuration change reporting with change status, bundle analysis automation reporting with automation status, analysis analytics with analysis statistics)
  • Support for multiple build tools (Webpack, Vite, Rollup) including build tool support (multiple build tool support with tool detection, Webpack/Vite/Rollup support with tool validation, build tool integration with tool integration, build tool reporting with tool status), support management (support configuration with support settings, support customization with custom support, support monitoring with support tracking, support updates with support updates), support optimization (support performance optimization with performance improvement, support accuracy optimization with accuracy improvement, support completeness optimization with completeness improvement, support analytics with support statistics), and support reporting (build tool support reporting with support status, Webpack/Vite/Rollup reporting with tool status, build tool integration reporting with integration status, support analytics with support statistics)
  • Performance optimization recommendations including optimization recommendations (performance optimization recommendations with recommendation generation, bundle optimization analysis with optimization analysis, performance suggestions with suggestion generation, optimization reporting with optimization status), recommendation management (recommendation configuration with recommendation settings, recommendation customization with custom recommendations, recommendation monitoring with recommendation tracking, recommendation updates with recommendation updates), recommendation optimization (recommendation performance optimization with performance improvement, recommendation accuracy optimization with accuracy improvement, recommendation completeness optimization with completeness improvement, recommendation analytics with recommendation statistics), and recommendation reporting (optimization recommendations reporting with recommendation status, bundle optimization reporting with optimization status, performance suggestions reporting with suggestion status, recommendation analytics with recommendation statistics)
  • Dependency size tracking and optimization including dependency tracking (dependency size tracking with size monitoring, dependency analysis with dependency analysis, dependency optimization with optimization tracking, dependency reporting with dependency status), tracking management (tracking configuration with tracking settings, tracking customization with custom tracking, tracking monitoring with tracking monitoring, tracking updates with tracking updates), tracking optimization (tracking performance optimization with performance improvement, tracking accuracy optimization with accuracy improvement, tracking completeness optimization with completeness improvement, tracking analytics with tracking statistics), and tracking reporting (dependency tracking reporting with tracking status, dependency analysis reporting with analysis status, dependency optimization reporting with optimization status, tracking analytics with tracking statistics)
  • Tree-shaking effectiveness analysis including tree-shaking analysis (tree-shaking effectiveness analysis with effectiveness checking, unused code detection with code detection, tree-shaking optimization with optimization checking, tree-shaking reporting with tree-shaking status), analysis management (analysis configuration with analysis settings, analysis customization with custom analysis, analysis monitoring with analysis tracking, analysis updates with analysis updates), analysis optimization (analysis performance optimization with performance improvement, analysis accuracy optimization with accuracy improvement, analysis completeness optimization with completeness improvement, analysis analytics with analysis statistics), and analysis reporting (tree-shaking analysis reporting with analysis status, unused code reporting with code status, tree-shaking optimization reporting with optimization status, analysis analytics with analysis statistics)
  • Bundle splitting and code splitting analysis including splitting analysis (bundle splitting analysis with splitting checking, code splitting analysis with code splitting checking, splitting optimization with optimization checking, splitting reporting with splitting status), analysis management (analysis configuration with analysis settings, analysis customization with custom analysis, analysis monitoring with analysis tracking, analysis updates with analysis updates), analysis optimization (analysis performance optimization with performance improvement, analysis accuracy optimization with accuracy improvement, analysis completeness optimization with completeness improvement, analysis analytics with analysis statistics), and analysis reporting (bundle splitting reporting with splitting status, code splitting reporting with code splitting status, splitting optimization reporting with optimization status, analysis analytics with analysis statistics)
  • Development workflow integration including continuous bundle analysis (automatic bundle analysis on configuration changes, immediate bundle size feedback on build changes, automatic optimization recommendations on bundle changes, seamless bundle analysis integration with development workflow), workflow automation (automated bundle analysis without manual intervention, bundle analysis automation with automatic analysis, bundle optimization automation with automatic optimization), and workflow optimization (bundle analysis tracking with analysis monitoring, bundle optimization tracking with optimization monitoring, bundle performance maintenance with performance checks)

Use Cases

  • Analyze bundle size after webpack configuration changes automatically analyzing bundle sizes, detecting configuration changes, and providing bundle analysis feedback
  • Identify large dependencies impacting bundle size automatically detecting large dependencies, analyzing dependency sizes, and providing dependency optimization recommendations
  • Optimize bundle splitting and code splitting strategies automatically analyzing bundle splitting, validating code splitting strategies, and providing splitting optimization recommendations
  • Track bundle size over time for performance monitoring automatically tracking bundle sizes, analyzing size trends, and providing performance monitoring feedback
  • Identify unused code and dependencies automatically detecting unused code, analyzing tree-shaking effectiveness, and providing code elimination recommendations
  • Development workflow integration seamlessly integrating bundle analysis into development workflows without manual bundle analysis or performance validation

Installation

  1. Create hooks directory: mkdir -p .claude/hooks
  2. Create hook file: touch .claude/hooks/webpack-bundle-analyzer.sh
  3. Make executable: chmod +x .claude/hooks/webpack-bundle-analyzer.sh
  4. Add configuration from Hook Configuration section above to .claude/settings.json or ~/.claude/settings.json
  5. Alternative: Use the interactive /hooks command in Claude Code

Config paths

  • Local (not committed): .claude/settings.local.json
  • User settings (global): ~/.claude/settings.json
  • Project-wide (committed): .claude/settings.json

Requirements

  • Claude Code CLI installed
  • Project directory initialized
  • Bash shell available
  • Node.js and npm installed
  • Build tool installed (Webpack, Vite, Rollup, Next.js, Create React App)
  • webpack-bundle-analyzer (optional, for Webpack analysis)
  • rollup-plugin-visualizer (optional, for Vite/Rollup analysis)
  • @next/bundle-analyzer (optional, for Next.js analysis)
  • source-map-explorer (optional, for Create React App analysis)
  • jq (optional, for JSON parsing of tool input)

Hook Configuration

{
  "hooks": {
    "postToolUse": {
      "script": "./.claude/hooks/webpack-bundle-analyzer.sh",
      "matchers": ["write", "edit"]
    }
  }
}

Hook Script

#!/bin/bash

# Read the tool input from stdin
INPUT=$(cat)
TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name')
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // ""')

if [ -z "$FILE_PATH" ]; then
  exit 0
fi

echo "📊 Webpack Bundle Analyzer - Analyzing bundle performance..."
echo "📄 File: $FILE_PATH"

# Check if this is a relevant file for bundle analysis
RELEVANT_FILE=false

if [[ "$FILE_PATH" == *webpack.config.js ]] || \
   [[ "$FILE_PATH" == *webpack.config.ts ]] || \
   [[ "$FILE_PATH" == *vite.config.js ]] || \
   [[ "$FILE_PATH" == *vite.config.ts ]] || \
   [[ "$FILE_PATH" == *rollup.config.js ]] || \
   [[ "$FILE_PATH" == *src/index.js ]] || \
   [[ "$FILE_PATH" == *src/index.ts ]] || \
   [[ "$FILE_PATH" == *src/main.js ]] || \
   [[ "$FILE_PATH" == *src/main.ts ]] || \
   [[ "$FILE_PATH" == *package.json ]]; then
    RELEVANT_FILE=true
fi

if [ "$RELEVANT_FILE" = false ]; then
    echo "ℹ️ File does not require bundle analysis: $FILE_PATH"
    exit 0
fi

echo "🔍 Detecting build system and configuration..."

# Detect build system
BUILD_SYSTEM="unknown"
if [ -f "webpack.config.js" ] || [ -f "webpack.config.ts" ]; then
    BUILD_SYSTEM="webpack"
    echo "📦 Webpack configuration detected"
elif [ -f "vite.config.js" ] || [ -f "vite.config.ts" ]; then
    BUILD_SYSTEM="vite"
    echo "⚡ Vite configuration detected"
elif [ -f "rollup.config.js" ] || [ -f "rollup.config.ts" ]; then
    BUILD_SYSTEM="rollup"
    echo "🎯 Rollup configuration detected"
elif [ -f "package.json" ] && grep -q '"react-scripts"' package.json 2>/dev/null; then
    BUILD_SYSTEM="cra"
    echo "⚛️ Create React App detected"
elif [ -f "next.config.js" ] || [ -f "next.config.ts" ]; then
    BUILD_SYSTEM="next"
    echo "🔺 Next.js configuration detected"
else
    echo "❓ No recognized build system found"
fi

# Check for bundle analyzer availability
ANALYZER_AVAILABLE=false
if command -v npx >/dev/null 2>&1; then
    if npx webpack-bundle-analyzer --version >/dev/null 2>&1; then
        ANALYZER_AVAILABLE=true
        echo "✅ webpack-bundle-analyzer available"
    else
        echo "⚠️ webpack-bundle-analyzer not available - install with: npm install --save-dev webpack-bundle-analyzer"
    fi
else
    echo "⚠️ npx not available - please install Node.js"
fi

# Perform bundle analysis based on build system
case "$BUILD_SYSTEM" in
    "webpack")
        echo "📊 Analyzing Webpack bundle..."

        # Check if dist directory exists
        if [ ! -d "dist" ] && [ ! -d "build" ]; then
            echo "🏗️ Building project to generate bundle..."
            if npm run build 2>/dev/null; then
                echo "✅ Build completed successfully"
            else
                echo "❌ Build failed - cannot analyze bundle"
                exit 1
            fi
        fi

        # Find stats file or generate one
        STATS_FILE=""
        if [ -f "dist/stats.json" ]; then
            STATS_FILE="dist/stats.json"
        elif [ -f "build/stats.json" ]; then
            STATS_FILE="build/stats.json"
        else
            echo "📈 Generating webpack stats..."
            if npx webpack --profile --json > webpack-stats.json 2>/dev/null; then
                STATS_FILE="webpack-stats.json"
                echo "✅ Stats file generated: $STATS_FILE"
            else
                echo "❌ Failed to generate webpack stats"
                exit 1
            fi
        fi

        # Run bundle analyzer
        if [ "$ANALYZER_AVAILABLE" = true ] && [ -n "$STATS_FILE" ]; then
            echo "🔍 Running bundle analysis..."
            if npx webpack-bundle-analyzer "$STATS_FILE" --mode static --report bundle-report.html --no-open 2>/dev/null; then
                echo "✅ Bundle analysis completed"
                echo "📊 Report saved to: bundle-report.html"
                echo "🌐 View report: file://$(pwd)/bundle-report.html"
            else
                echo "⚠️ Bundle analysis failed"
            fi
        fi
        ;;
    "vite")
        echo "⚡ Analyzing Vite bundle..."

        # Check if Vite has bundle analysis plugin
        if grep -q 'vite-bundle-analyzer\|rollup-plugin-analyzer' package.json 2>/dev/null; then
            echo "📊 Running Vite bundle analysis..."
            if npm run build -- --analyze 2>/dev/null; then
                echo "✅ Vite bundle analysis completed"
            else
                echo "⚠️ Vite bundle analysis failed"
            fi
        else
            echo "💡 Install vite-bundle-analyzer for detailed analysis:"
            echo "    npm install --save-dev vite-bundle-analyzer"

            # Basic build size analysis
            echo "📏 Running basic build analysis..."
            if npm run build 2>/dev/null; then
                if [ -d "dist" ]; then
                    echo "📊 Build output analysis:"
                    find dist -name "*.js" -exec du -h {} \; | sort -hr | head -10
                fi
            fi
        fi
        ;;
    "next")
        echo "🔺 Analyzing Next.js bundle..."

        if grep -q '@next/bundle-analyzer' package.json 2>/dev/null; then
            echo "📊 Running Next.js bundle analysis..."
            if ANALYZE=true npm run build 2>/dev/null; then
                echo "✅ Next.js bundle analysis completed"
            else
                echo "⚠️ Next.js bundle analysis failed"
            fi
        else
            echo "💡 Install @next/bundle-analyzer for detailed analysis:"
            echo "    npm install --save-dev @next/bundle-analyzer"
        fi
        ;;
    "cra")
        echo "⚛️ Analyzing Create React App bundle..."

        if npm list --depth=0 | grep -q 'source-map-explorer' 2>/dev/null; then
            echo "📊 Running source-map-explorer..."
            if npm run build && npx source-map-explorer 'build/static/js/*.js' 2>/dev/null; then
                echo "✅ CRA bundle analysis completed"
            else
                echo "⚠️ CRA bundle analysis failed"
            fi
        else
            echo "💡 Install source-map-explorer for detailed analysis:"
            echo "    npm install --save-dev source-map-explorer"
        fi
        ;;
    *)
        echo "❓ Unknown build system - attempting generic analysis"

        # Try to analyze any existing build output
        for dir in dist build public; do
            if [ -d "$dir" ]; then
                echo "📊 Analyzing $dir directory:"
                find "$dir" -name "*.js" -o -name "*.css" | head -10 | while read -r file; do
                    size=$(du -h "$file" 2>/dev/null | cut -f1)
                    echo "  • $file: $size"
                done
            fi
        done
        ;;
esac

# General optimization suggestions
echo ""
echo "💡 Bundle Optimization Tips:"
echo "  • Enable tree-shaking to remove unused code"
echo "  • Use dynamic imports for code splitting"
echo "  • Optimize images and static assets"
echo "  • Use compression (gzip/brotli) for production"
echo "  • Analyze and remove large unnecessary dependencies"
echo "  • Use webpack-bundle-analyzer for detailed insights"
echo "  • Consider lazy loading for non-critical components"
echo "  • Review and optimize polyfills"

echo ""
echo "📊 Bundle Analysis Tools:"
echo "  • Webpack: webpack-bundle-analyzer"
echo "  • Vite: vite-bundle-analyzer"
echo "  • Next.js: @next/bundle-analyzer"
echo "  • CRA: source-map-explorer"
echo "  • General: bundlephobia.com for dependency analysis"

echo ""
echo "🎯 Bundle analysis complete!"

exit 0

Examples

Webpack Bundle Analyzer Hook Script

Complete hook script that automatically analyzes webpack bundle size when webpack config or entry files are modified

#!/bin/bash
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // ""')
if [ -z "$FILE_PATH" ]; then
  exit 0
fi
RELEVANT_FILE=false
if [[ "$FILE_PATH" == *webpack.config.js ]] || [[ "$FILE_PATH" == *webpack.config.ts ]] || [[ "$FILE_PATH" == *vite.config.js ]] || [[ "$FILE_PATH" == *vite.config.ts ]] || [[ "$FILE_PATH" == *package.json ]]; then
    RELEVANT_FILE=true
fi
if [ "$RELEVANT_FILE" = false ]; then
    exit 0
fi
echo "📊 Webpack Bundle Analyzer - Analyzing bundle performance..."
BUILD_SYSTEM="unknown"
if [ -f "webpack.config.js" ] || [ -f "webpack.config.ts" ]; then
    BUILD_SYSTEM="webpack"
    echo "📦 Webpack configuration detected"
    if [ ! -d "dist" ] && [ ! -d "build" ]; then
        echo "🏗️ Building project to generate bundle..."
        if npm run build 2>/dev/null; then
            echo "✅ Build completed successfully"
        else
            echo "❌ Build failed - cannot analyze bundle"
            exit 1
        fi
    fi
    STATS_FILE=""
    if [ -f "dist/stats.json" ]; then
        STATS_FILE="dist/stats.json"
    elif [ -f "build/stats.json" ]; then
        STATS_FILE="build/stats.json"
    else
        echo "📈 Generating webpack stats..."
        if npx webpack --profile --json > webpack-stats.json 2>/dev/null; then
            STATS_FILE="webpack-stats.json"
            echo "✅ Stats file generated: $STATS_FILE"
        fi
    fi
    if command -v npx >/dev/null 2>&1 && npx webpack-bundle-analyzer --version >/dev/null 2>&1; then
        if [ -n "$STATS_FILE" ]; then
            echo "🔍 Running bundle analysis..."
            if npx webpack-bundle-analyzer "$STATS_FILE" --mode static --report bundle-report.html --no-open 2>/dev/null; then
                echo "✅ Bundle analysis completed"
                echo "📊 Report saved to: bundle-report.html"
            fi
        fi
    fi
fi
echo "🎯 Bundle analysis complete!"
exit 0

Hook Configuration

Complete hook configuration for .claude/settings.json to enable automatic bundle analysis

{
  "hooks": {
    "postToolUse": {
      "script": "./.claude/hooks/webpack-bundle-analyzer.sh",
      "matchers": ["write", "edit"]
    }
  }
}

Enhanced Multi-Build Tool Bundle Analyzer

Enhanced hook script with support for Webpack, Vite, and Rollup bundle analysis

#!/bin/bash
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // ""')
if [ -z "$FILE_PATH" ]; then
  exit 0
fi
RELEVANT_FILE=false
if [[ "$FILE_PATH" == *webpack.config.js ]] || [[ "$FILE_PATH" == *vite.config.js ]] || [[ "$FILE_PATH" == *rollup.config.js ]] || [[ "$FILE_PATH" == *package.json ]]; then
    RELEVANT_FILE=true
fi
if [ "$RELEVANT_FILE" = false ]; then
    exit 0
fi
echo "📊 Webpack Bundle Analyzer - Analyzing bundle performance..."
BUILD_SYSTEM="unknown"
if [ -f "vite.config.js" ] || [ -f "vite.config.ts" ]; then
    BUILD_SYSTEM="vite"
    echo "⚡ Vite configuration detected"
    if grep -q 'rollup-plugin-visualizer' package.json 2>/dev/null; then
        echo "📊 Running Vite bundle analysis..."
        if npm run build 2>/dev/null; then
            echo "✅ Vite bundle analysis completed"
            if [ -d "dist" ]; then
                echo "📊 Build output analysis:"
                find dist -name "*.js" -exec du -h {} \; | sort -hr | head -10
            fi
        fi
    else
        echo "💡 Install rollup-plugin-visualizer for detailed analysis:"
        echo "    npm install --save-dev rollup-plugin-visualizer"
    fi
elif [ -f "rollup.config.js" ] || [ -f "rollup.config.ts" ]; then
    BUILD_SYSTEM="rollup"
    echo "🎯 Rollup configuration detected"
    if grep -q 'rollup-plugin-visualizer' package.json 2>/dev/null; then
        echo "📊 Running Rollup bundle analysis..."
        if npm run build 2>/dev/null; then
            echo "✅ Rollup bundle analysis completed"
        fi
    fi
fi
echo "🎯 Bundle analysis complete!"
exit 0

Webpack Bundle Analyzer with Build Cache

Enhanced hook script with build cache to skip rebuilds for unchanged configurations

#!/bin/bash
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // ""')
if [ -z "$FILE_PATH" ]; then
  exit 0
fi
if [[ "$FILE_PATH" == *webpack.config.js ]] || [[ "$FILE_PATH" == *package.json ]]; then
    echo "📊 Webpack Bundle Analyzer - Analyzing bundle performance..."
    if [ -f "webpack-stats.json" ]; then
        CONFIG_HASH=$(md5sum webpack.config.js 2>/dev/null | cut -d' ' -f1 || echo "")
        if [ -n "$CONFIG_HASH" ]; then
            CACHE_FILE=".bundle-analyzed-$CONFIG_HASH"
            if [ -f "$CACHE_FILE" ]; then
                echo "✅ Bundle already analyzed for this configuration"
                exit 0
            fi
            touch "$CACHE_FILE"
        fi
    fi
    if [ ! -d "dist" ] && [ ! -d "build" ]; then
        echo "🏗️ Building project to generate bundle..."
        if npm run build 2>/dev/null; then
            echo "✅ Build completed successfully"
        else
            echo "❌ Build failed - cannot analyze bundle"
            exit 1
        fi
    fi
    STATS_FILE=""
    if [ -f "dist/stats.json" ]; then
        STATS_FILE="dist/stats.json"
    elif [ -f "build/stats.json" ]; then
        STATS_FILE="build/stats.json"
    else
        echo "📈 Generating webpack stats..."
        if npx webpack --profile --json > webpack-stats.json 2>/dev/null; then
            STATS_FILE="webpack-stats.json"
        fi
    fi
    if command -v npx >/dev/null 2>&1 && npx webpack-bundle-analyzer --version >/dev/null 2>&1; then
        if [ -n "$STATS_FILE" ]; then
            echo "🔍 Running bundle analysis..."
            if npx webpack-bundle-analyzer "$STATS_FILE" --mode static --report bundle-report.html --no-open 2>/dev/null; then
                echo "✅ Bundle analysis completed"
                echo "📊 Report saved to: bundle-report.html"
            fi
        fi
    fi
fi
echo "🎯 Bundle analysis complete!"
exit 0

Webpack Bundle Analyzer Configuration Example

Example webpack bundle analyzer configuration for customizing bundle analysis behavior

{
  "webpack_bundle_analyzer": {
    "enabled": true,
    "build_systems": ["webpack", "vite", "rollup", "next", "cra"],
    "auto_build": true,
    "cache_analysis": true,
    "tools": {
      "webpack": {
        "analyzer": "webpack-bundle-analyzer",
        "stats_command": "webpack --profile --json",
        "stats_file": "webpack-stats.json"
      },
      "vite": {
        "analyzer": "rollup-plugin-visualizer",
        "build_command": "npm run build"
      },
      "rollup": {
        "analyzer": "rollup-plugin-visualizer",
        "build_command": "npm run build"
      },
      "next": {
        "analyzer": "@next/bundle-analyzer",
        "build_command": "ANALYZE=true npm run build"
      },
      "cra": {
        "analyzer": "source-map-explorer",
        "build_command": "npm run build"
      }
    },
    "file_patterns": [
      "*webpack.config.*",
      "*vite.config.*",
      "*rollup.config.*",
      "package.json",
      "src/index.*",
      "src/main.*"
    ],
    "exclude_patterns": ["**/node_modules/**", "**/dist/**", "**/build/**"]
  }
}

Troubleshooting

Build triggered on every config edit is slow

Add build skip flag or cache check: if [ -f .bundle-analyzed-$(md5sum webpack.config.js | cut -d' ' -f1) ]; then exit 0; fi to track analyzed configs and skip rebuilds for unchanged hashes. Verify cache file creation. Check config hash calculation.

webpack-bundle-analyzer opens browser automatically

Hook uses --no-open flag: npx webpack-bundle-analyzer "$STATS_FILE" --mode static --report bundle-report.html --no-open. Verify flag is present in script to prevent browser launch. Check webpack-bundle-analyzer version.

Stats file generation fails with webpack errors

Check webpack config validity first: npx webpack --config webpack.config.js --json > /dev/null 2>&1 to test. If fails, examine error with npx webpack --profile --json 2>&1 | tee webpack-errors.log. Verify webpack configuration. Check webpack version compatibility.

Vite build doesn't support --analyze flag

Install plugin: npm i -D rollup-plugin-visualizer, add to vite.config: import { visualizer } from 'rollup-plugin-visualizer'; plugins: [visualizer()], rebuild. Verify vite.config.js. Check Vite version compatibility.

Hook detects wrong build system for monorepo

Detection scans root only. For workspace builds, modify check: if [[ "$FILE_PATH" == packages/app/* ]]; then cd packages/app; BUILD_SYSTEM=...; fi. Verify monorepo structure. Check workspace configuration.

webpack-bundle-analyzer not found despite npm installation

Verify webpack-bundle-analyzer installation: 'npm list webpack-bundle-analyzer' or 'npx webpack-bundle-analyzer --version'. Install: 'npm install --save-dev webpack-bundle-analyzer'. Check PATH. Verify Node.js version compatibility.

Bundle report HTML file not generated

Verify stats file exists and is valid JSON. Check webpack-bundle-analyzer version supports --report flag. Ensure write permissions for report directory. Verify --mode static flag. Test webpack-bundle-analyzer command manually.

Next.js bundle analyzer requires ANALYZE environment variable

Set ANALYZE environment variable: 'ANALYZE=true npm run build'. Or configure @next/bundle-analyzer in next.config.js. Verify Next.js configuration. Check @next/bundle-analyzer installation.

Source citations

Add this badge to your README

Show that Webpack Bundle Analyzer - Hooks is listed on HeyClaude. Paste this Markdown into your README — it renders the badge and links back to this page.

Listed on HeyClaude
[![Listed on HeyClaude](https://heyclau.de/badge/hooks/webpack-bundle-analyzer.svg)](https://heyclau.de/entry/hooks/webpack-bundle-analyzer)

How it compares

Webpack Bundle Analyzer - Hooks side by side with 3 alternatives on trust, install, platform support, and disclosed safety notes — all from reviewed registry metadata.

Next steps differ across entries — use the actions in the table below to copy install commands and source links per resource.

Field

Analyzes webpack bundle size when webpack config or entry files are modified.

Open dossier

Analyzes and reports final bundle sizes when the development session ends.

Open dossier

Detects unused CSS selectors when stylesheets are modified to keep CSS lean using PurgeCSS, PostCSS, and content analysis. This hook runs on CSS/SCSS file write/edit operations and analyzes stylesheets to identify unused selectors, generate optimized output, and report before/after size metrics.

Open dossier

A PostToolUse hook that reminds you to enable native query logging for PostgreSQL, Prisma, Sequelize, and TypeORM, and flags possible N+1 patterns when you edit SQL or data-access files.

Open dossier
Next stepsDiffers
Trust
Review statusReviewedMaintainer reviewedReviewedMaintainer reviewedReviewedMaintainer reviewedReviewedMaintainer reviewed
Package trustPackage not verifiedPackage not verifiedPackage not verifiedPackage not verified
Source provenanceSource-backedSource-backedSource-backedSource-backed
Submitter
Install riskReview firstReview firstReview firstReview first
Notes Safety Privacy Safety Privacy Safety Privacy Safety Privacy
Brand
Categoryhookshookshookshooks
Sourcesource-backedsource-backedsource-backedsource-backed
AuthorJSONboredJSONboredJSONboredJSONbored
Added2025-09-192025-09-192025-09-192025-10-19
Platforms
Claude Code
Claude Code
Claude Code
Claude Code
Source repo
Safety notesRuns automatically on its configured Claude Code hook event and executes shell logic that can read, modify, or delete files in your project (and may run builds, installs, or network calls); review the script and scope it to expected paths before enabling.Runs automatically on its configured Claude Code hook event and executes shell logic that can read, modify, or delete files in your project (and may run builds, installs, or network calls); review the script and scope it to expected paths before enabling.Runs automatically on its configured Claude Code hook event and executes shell logic that can read, modify, or delete files in your project (and may run builds, installs, or network calls); review the script and scope it to expected paths before enabling.Runs automatically after bash, write, or edit activity and inspects files that look like query, model, repository, DAO, or SQL files. Creates .claude/logs/query-performance.log and appends database command or file-analysis events. Uses grep-based heuristics for query warnings and should not be treated as proof of a performance defect.
Privacy notesReceives Claude Code hook input (session metadata, file paths, and tool output) and reads local project files; review what the script logs or forwards to external services and keep credentials out of its output.Receives Claude Code hook input (session metadata, file paths, and tool output) and reads local project files; review what the script logs or forwards to external services and keep credentials out of its output.Receives Claude Code hook input (session metadata, file paths, and tool output) and reads local project files; review what the script logs or forwards to external services and keep credentials out of its output.Reads query-related source files and may print file paths, query patterns, and database command strings to local hook output. Stores analyzed file paths and database command text in .claude/logs/query-performance.log. Database command text may include connection names, database names, or other operational details if typed directly into the command.
Prerequisites— none listed— none listed— none listed— none listed
Install
mkdir -p .claude/hooks && touch .claude/hooks/webpack-bundle-analyzer.sh && chmod +x .claude/hooks/webpack-bundle-analyzer.sh
mkdir -p .claude/hooks && touch .claude/hooks/final-bundle-size-reporter.sh && chmod +x .claude/hooks/final-bundle-size-reporter.sh
mkdir -p .claude/hooks && touch .claude/hooks/css-unused-selector-detector.sh && chmod +x .claude/hooks/css-unused-selector-detector.sh
Config
{
  "hooks": {
    "postToolUse": {
      "script": "./.claude/hooks/webpack-bundle-analyzer.sh",
      "matchers": [
        "write",
        "edit"
      ]
    }
  }
}
{
  "hooks": {
    "stop": {
      "script": "./.claude/hooks/final-bundle-size-reporter.sh"
    }
  }
}
{
  "hooks": {
    "postToolUse": {
      "script": "./.claude/hooks/css-unused-selector-detector.sh",
      "matchers": [
        "write",
        "edit"
      ]
    }
  }
}
{
  "hooks": {
    "postToolUse": {
      "script": "./.claude/hooks/database-query-performance-logger.sh",
      "matchers": [
        "bash",
        "write",
        "edit"
      ]
    }
  }
}
Citations
ClaimUnclaimedUnclaimedUnclaimedUnclaimed
Open 4 picks in the interactive comparison tool

Related guides

Signals

Loading live community signals…

More like this, weekly

A short, calm digest of reviewed Claude resources. Unsubscribe any time.