.cursor/skills/bundle-optimizer/SKILL.md
打包优化技能。当用户需要优化打包体积、分析Bundle大小、实现代码分割、或询问如何减小构建产物时使用此skill。
npx skillsauth add xiaoniuge36/codegen-engine-mcp bundle-optimizerInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
打包优化指导,帮助 AI 完成 Bundle 分析、代码分割、Tree Shaking、依赖优化等任务。
| 能力 | 说明 | |------|------| | 📊 Bundle 分析 | 分析打包产物构成 | | ✂️ 代码分割 | 路由/组件级别分割 | | 🌳 Tree Shaking | 移除未使用代码 | | 📦 依赖优化 | 减少第三方库体积 | | 🗜️ 压缩优化 | Gzip/Brotli 压缩 |
# webpack-bundle-analyzer
npm install --save-dev webpack-bundle-analyzer
# 在 webpack 配置中
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
plugins: [new BundleAnalyzerPlugin()]
# Vite 使用 rollup-plugin-visualizer
npm install --save-dev rollup-plugin-visualizer
| 指标 | 说明 | 目标 | |------|------|------| | 总大小 | 所有 chunks 总和 | < 500KB (gzip) | | 主包大小 | main chunk | < 150KB (gzip) | | 最大 chunk | 单个最大包 | < 200KB (gzip) | | 重复依赖 | 同一库多个版本 | 0 |
// React 路由懒加载
import { lazy, Suspense } from 'react';
const Dashboard = lazy(() => import('./pages/Dashboard'));
const Settings = lazy(() => import('./pages/Settings'));
function App() {
return (
<Suspense fallback={<Loading />}>
<Routes>
<Route path="/dashboard" element={<Dashboard />} />
<Route path="/settings" element={<Settings />} />
</Routes>
</Suspense>
);
}
// Vue 路由懒加载
const routes = [
{
path: '/dashboard',
component: () => import('./pages/Dashboard.vue'),
},
{
path: '/settings',
component: () => import('./pages/Settings.vue'),
},
];
// 重组件动态导入
const HeavyChart = lazy(() => import('./HeavyChart'));
const RichEditor = lazy(() => import('./RichEditor'));
// 条件加载
function Editor({ type }) {
const EditorComponent = useMemo(() => {
if (type === 'rich') {
return lazy(() => import('./RichEditor'));
}
return lazy(() => import('./SimpleEditor'));
}, [type]);
return (
<Suspense fallback={<Skeleton />}>
<EditorComponent />
</Suspense>
);
}
// 命名 chunk
const Dashboard = lazy(() =>
import(/* webpackChunkName: "dashboard" */ './Dashboard')
);
// 预加载
const Settings = lazy(() =>
import(/* webpackPrefetch: true */ './Settings')
);
// 预获取
const Reports = lazy(() =>
import(/* webpackPreload: true */ './Reports')
);
// ❌ 全量导入
import _ from 'lodash';
import * as antd from 'antd';
// ✅ 按需导入
import debounce from 'lodash/debounce';
import { Button, Table } from 'antd';
// ✅ 使用 babel-plugin-import
// .babelrc
{
"plugins": [
["import", { "libraryName": "antd", "style": true }]
]
}
| 原依赖 | 替代方案 | 体积减少 | |--------|----------|----------| | moment | dayjs | ~95% | | lodash | lodash-es + 按需 | ~80% | | axios | ky / redaxios | ~70% | | uuid | nanoid | ~90% |
// moment -> dayjs
// ❌ import moment from 'moment';
// ✅ import dayjs from 'dayjs';
// lodash -> 按需
// ❌ import _ from 'lodash';
// ✅ import debounce from 'lodash-es/debounce';
// webpack externals
module.exports = {
externals: {
react: 'React',
'react-dom': 'ReactDOM',
},
};
// vite rollupOptions
export default {
build: {
rollupOptions: {
external: ['react', 'react-dom'],
output: {
globals: {
react: 'React',
'react-dom': 'ReactDOM',
},
},
},
},
};
// package.json 设置 sideEffects
{
"sideEffects": false
}
// 或指定有副作用的文件
{
"sideEffects": [
"*.css",
"*.scss"
]
}
// ❌ 阻碍 Tree Shaking
export default {
add: (a, b) => a + b,
subtract: (a, b) => a - b,
};
// ✅ 支持 Tree Shaking
export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;
// webpack
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true,
},
},
}),
],
}
// vite
import viteCompression from 'vite-plugin-compression';
export default {
plugins: [
viteCompression({ algorithm: 'gzip' }),
viteCompression({ algorithm: 'brotliCompress' }),
],
};
| 类型 | 关键词示例 | |------|-----------| | 打包优化 | "包体积"、"Bundle大"、"打包优化" | | 代码分割 | "代码分割"、"懒加载"、"动态导入" | | 依赖优化 | "依赖太大"、"替换lodash"、"按需导入" |
## Bundle 优化清单
- [ ] 运行 Bundle 分析
- [ ] 实现路由级代码分割
- [ ] 按需导入 UI 库
- [ ] 替换大型依赖
- [ ] 启用 Tree Shaking
- [ ] 配置 Gzip/Brotli
- [ ] 设置性能预算
development
Vue Composables技能。当用户需要编写Vue组合式函数、理解Composition API、封装composables逻辑、或询问Vue3状态管理时使用此skill。
testing
单元测试技能。当用户需要编写单元测试、创建测试用例、使用测试框架、或询问如何进行前端测试时使用此skill。
tools
表格生成技能。当用户需要生成表格、创建列表页、做数据展示、实现分页排序筛选、或询问如何处理表格逻辑时使用此skill。
development
样式编写技能。当用户需要编写CSS样式、使用Tailwind CSS、实现响应式布局、或询问如何组织样式代码时使用此skill。