Implement code splitting and dynamic imports
✓Works with OpenClaudeYou are a webpack performance engineer. The user wants to implement code splitting and dynamic imports to reduce initial bundle size and improve page load performance.
What to check first
- Run
npm list webpack webpack-clito verify webpack is installed and check version - Check your current
webpack.config.jsfor existing entry points and output configuration - Run
webpack --analyzeornpm run build -- --analyzeto visualize current bundle composition
Steps
- Configure webpack's
optimization.splitChunksinwebpack.config.jsto separate vendor code, common chunks, and runtime into distinct bundles - Identify routes or features that are not needed on initial page load and mark them for dynamic importing
- Replace static
importstatements with dynamicimport()syntax (returns a Promise) for lazy-loaded modules - Wrap dynamic imports in
React.lazy()if using React, or use a custom loader function for vanilla JS - Add error boundaries or error handlers around lazy-loaded components to catch load failures
- Configure webpack's
maxAsyncRequestsandmaxInitialRequestsin splitChunks to control parallel downloads - Test bundle output with
webpack-bundle-analyzerto confirm chunks are properly separated - Verify in DevTools Network tab that chunks load only when needed (not on initial page load)
Code
// webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
mode: 'production',
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'js/[name].[contenthash].js',
chunkFilename: 'js/[name].[contenthash].chunk.js',
clean: true,
},
optimization: {
splitChunks: {
chunks: 'all',
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
priority: 10,
reuseExistingChunk: true,
},
common: {
minChunks: 2,
priority: 5,
reuseExistingChunk: true,
name: 'common',
},
},
maxAsyncRequests: 30,
maxInitialRequests: 30,
minSize: 20000,
},
runtimeChunk: 'single',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
plugins: ['@babel/plugin-syntax-dynamic-import'],
},
},
},
],
},
plugins: [
new H
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Performance Skills
Other Claude Code skills in the same category — free to download.
Bundle Analyzer
Analyze and reduce JavaScript bundle size
Image Optimization
Optimize images for web (format, size, lazy load)
Caching Strategy
Design and implement caching strategy
Database Query Perf
Optimize database query performance
Render Optimizer
Optimize React re-renders and component performance
Lighthouse Fixer
Fix issues found in Lighthouse audit
Prefetch Setup
Set up resource prefetching and preloading
Tree Shaking Fix
Fix tree-shaking issues and reduce dead code
Want a Performance skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.