Skip to content
Development
Command

/optimize-bundle-size

Reduce and optimize bundle sizes

From plugin
claude-command-suite
1.3k199 skills89 agents199 commands
Install
$ npx -y skills add qdhenry/Claude-Command-Suite --agent claude-code

How it fires

How this command gets triggered: by you, by Claude, or both.

  • Fires itselfClaude auto-loads it when your prompt matches the work.
  • You can call itInvoke it directly when you want it.
  • Slash command/optimize-bundle-size

Context preview

What this command does when you run it.

Reduce and optimize bundle sizes

Command definition

optimize-bundle-size.md

Optimize Bundle Size

Reduce and optimize bundle sizes

Instructions

1. **Bundle Analysis and Assessment**

  • Analyze current bundle size and composition using webpack-bundle-analyzer or similar
  • Identify large dependencies and unused code
  • Assess current build configuration and optimization settings
  • Create baseline measurements for optimization tracking
  • Document current performance metrics and loading times

2. **Build Tool Configuration**

  • Configure build tool optimization settings:

**Webpack Configuration:**

   // webpack.config.js
   const path = require('path');
   const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');

   module.exports = {
     mode: 'production',
     optimization: {
       splitChunks: {
         chunks: 'all',
         cacheGroups: {
           vendor: {
             test: /[\\/]node_modules[\\/]/,
             name: 'vendors',
             priority: 10,
             reuseExistingChunk: true,
           },
           common: {
             name: 'common',
             minChunks: 2,
             priority: 5,
             reuseExistingChunk: true,
           },
         },
       },
       usedExports: true,
       sideEffects: false,
     },
     plugins: [
       new BundleAnalyzerPlugin({
         analyzerMode: 'static',
         openAnalyzer: false,
       }),
     ],
   };

**Vite Configuration:**

   // vite.config.js
   import { defineConfig } from 'vite';
   import { visualizer } from 'rollup-plugin-visualizer';

   export default defineConfig({
     build: {
       rollupOptions: {
         output: {
           manualChunks: {
             vendor: ['react', 'react-dom'],
             ui: ['@mui/material', '@emotion/react'],
           },
         },
       },
     },
     plugins: [
       visualizer({
         filename: 'dist/stats.html',
         open: true,
         gzipSize: true,
       }),
     ],
   });

3. **Code Splitting and Lazy Loading**

  • Implement route-based code splitting:

**React Route Splitting:**

   import { lazy, Suspense } from 'react';
   import { Routes, Route } from 'react-router-dom';

   const Home = lazy(() => import('./pages/Home'));
   const Dashboard = lazy(() => import('./pages/Dashboard'));
   const Profile = lazy(() => import('./pages/Profile'));

   function App() {
     return (
       <Suspense fallback={<div>Loading...</div>}>
         <Routes>
           <Route path="/" element={<Home />} />
           <Route path="/dashboard" element={<Dashboard />} />
           <Route path="/profile" element={<Profile />} />
         </Routes>
       </Suspense>
     );
   }

**Dynamic Imports:**

   // Lazy load heavy components
   const HeavyComponent = lazy(() => 
     import('./HeavyComponent').then(module => ({
       default: module.HeavyComponent
     }))
   );

   // Conditional loading
   async function loadAnalytics() {
     if (process.env.NODE_ENV === 'production') {
       const { analytics } = await import('./analytics');
       return analytics;
     }
   }

4. **Tree Shaking and Dead Code Elimination**

  • Configure tree shaking for optimal dead code elimination:

**Package.json Configuration:**

   {
     "sideEffects": false,
     "exports": {
       ".": {
         "import": "./dist/index.esm.js",
         "require": "./dist/index.cjs.js"
       }
     }
   }

**Import Optimization:**

   // Instead of importing entire library
   // import * as _ from 'lodash';

   // Import only what you need
   import debounce from 'lodash/debounce';
   import throttle from 'lodash/throttle';

   // Use babel-plugin-import for automatic optimization
   // .babelrc
   {
     "plugins": [
       ["import", {
         "libraryName": "lodash",
         "libraryDirectory": "",
         "camel2DashComponentName": false
       }, "lodash"]
     ]
   }

5. **Dependency Optimization**

  • Analyze and optimize dependencies:

**Package Analysis Script:**

   // scripts/analyze-deps.js
   const fs = require('fs');
   const path = require('path');

   function analyzeDependencies() {
     const packageJson = JSON.parse(
       fs.readFileSync('package.json', 'utf8')
     );
     
     const deps = {
       ...packageJson.dependencies,
       ...packageJson.devDependencies
     };

     console.log('Large dependencies to review:');
     Object.keys(deps).forEach(dep => {
       try {
         const depPath = require.resolve(dep);
         const stats = fs.statSync(depPath);
         if (stats.size > 100000) { // > 100KB
           console.log(`${dep}: ${(stats.size / 1024).toFixed(2)}KB`);
         }
       } catch (e) {
         // Skip if can't resolve
       }
     });
   }

   analyzeDependencies();

6. **Asset Optimization**

  • Optimize static assets and media files:

**Image Optimization:**

   // webpack.config.js
   module.exports = {
     module: {
       rules: [
         {
           test: /\.(png|jpe?g|gif|svg)$/i,
           use: [
             {
               loader: 'file-loader',
               options: {
                 outputPath: 'images',
               },
             },
             {
               loader: 'image-webpack-loader',
               options: {
                 mozjpeg: { progressive: true, quality: 80 },
                 optipng: { enabled: false },
                 pngquant: { quality: [0.6, 0.8] },
                 gifsicle: { interlaced: false },
               },
             },
           ],
         },
       ],
     },
   };

7. **Module Federation and Micro-frontends**

  • Implement module federation for large applications:

**Module Federation Setup:**

   // webpack.config.js
   const ModuleFederationPlugin = require('@module-federation/webpack');

   module.exports = {
     plugins: [
       new ModuleFederationPlugin({
Read more
Ships withclaude-command-suite

A comprehensive development toolkit designed following Anthropic's Claude Code Best Practices for AI-assisted software development.

Get the whole plugin