react-code-smell-detector 1.4.1 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +347 -22
- package/dist/__tests__/parser.test.d.ts +2 -0
- package/dist/__tests__/parser.test.d.ts.map +1 -0
- package/dist/__tests__/parser.test.js +56 -0
- package/dist/__tests__/performanceBudget.test.d.ts +2 -0
- package/dist/__tests__/performanceBudget.test.d.ts.map +1 -0
- package/dist/__tests__/performanceBudget.test.js +91 -0
- package/dist/__tests__/prComments.test.d.ts +2 -0
- package/dist/__tests__/prComments.test.d.ts.map +1 -0
- package/dist/__tests__/prComments.test.js +118 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/analyzer.js +34 -1
- package/dist/bundleAnalyzer.d.ts +25 -0
- package/dist/bundleAnalyzer.d.ts.map +1 -0
- package/dist/bundleAnalyzer.js +375 -0
- package/dist/cli.js +148 -1
- package/dist/customRules.d.ts +31 -0
- package/dist/customRules.d.ts.map +1 -0
- package/dist/customRules.js +289 -0
- package/dist/detectors/complexity.d.ts +0 -4
- package/dist/detectors/complexity.d.ts.map +1 -1
- package/dist/detectors/complexity.js +1 -1
- package/dist/detectors/deadCode.d.ts +0 -7
- package/dist/detectors/deadCode.d.ts.map +1 -1
- package/dist/detectors/deadCode.js +0 -24
- package/dist/detectors/index.d.ts +3 -2
- package/dist/detectors/index.d.ts.map +1 -1
- package/dist/detectors/index.js +4 -2
- package/dist/detectors/serverComponents.d.ts +11 -0
- package/dist/detectors/serverComponents.d.ts.map +1 -0
- package/dist/detectors/serverComponents.js +222 -0
- package/dist/docGenerator.d.ts +37 -0
- package/dist/docGenerator.d.ts.map +1 -0
- package/dist/docGenerator.js +306 -0
- package/dist/git.d.ts.map +1 -1
- package/dist/git.js +0 -7
- package/dist/graphGenerator.d.ts +34 -0
- package/dist/graphGenerator.d.ts.map +1 -0
- package/dist/graphGenerator.js +320 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/interactiveFixer.d.ts +20 -0
- package/dist/interactiveFixer.d.ts.map +1 -0
- package/dist/interactiveFixer.js +178 -0
- package/dist/performanceBudget.d.ts +54 -0
- package/dist/performanceBudget.d.ts.map +1 -0
- package/dist/performanceBudget.js +218 -0
- package/dist/prComments.d.ts +47 -0
- package/dist/prComments.d.ts.map +1 -0
- package/dist/prComments.js +233 -0
- package/dist/reporter.js +2 -0
- package/dist/types/index.d.ts +7 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +10 -0
- package/package.json +10 -4
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { generatePRComment, generateInlineComments } from '../prComments.js';
|
|
3
|
+
describe('PR Comments', () => {
|
|
4
|
+
const createMockResult = () => ({
|
|
5
|
+
files: [
|
|
6
|
+
{
|
|
7
|
+
file: '/src/components/Button.tsx',
|
|
8
|
+
components: [
|
|
9
|
+
{
|
|
10
|
+
name: 'Button',
|
|
11
|
+
file: '/src/components/Button.tsx',
|
|
12
|
+
startLine: 1,
|
|
13
|
+
endLine: 50,
|
|
14
|
+
lineCount: 50,
|
|
15
|
+
useEffectCount: 1,
|
|
16
|
+
useStateCount: 2,
|
|
17
|
+
useMemoCount: 0,
|
|
18
|
+
useCallbackCount: 0,
|
|
19
|
+
propsCount: 3,
|
|
20
|
+
propsDrillingDepth: 0,
|
|
21
|
+
hasExpensiveCalculation: false,
|
|
22
|
+
},
|
|
23
|
+
],
|
|
24
|
+
smells: [
|
|
25
|
+
{
|
|
26
|
+
type: 'useEffect-overuse',
|
|
27
|
+
severity: 'warning',
|
|
28
|
+
message: 'Component has multiple useEffect hooks',
|
|
29
|
+
file: '/src/components/Button.tsx',
|
|
30
|
+
line: 10,
|
|
31
|
+
column: 0,
|
|
32
|
+
suggestion: 'Consider consolidating effects',
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
imports: [],
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
summary: {
|
|
39
|
+
totalFiles: 1,
|
|
40
|
+
totalComponents: 1,
|
|
41
|
+
totalSmells: 1,
|
|
42
|
+
smellsByType: {
|
|
43
|
+
'useEffect-overuse': 1,
|
|
44
|
+
},
|
|
45
|
+
smellsBySeverity: { error: 0, warning: 1, info: 0 },
|
|
46
|
+
},
|
|
47
|
+
debtScore: {
|
|
48
|
+
score: 85,
|
|
49
|
+
grade: 'B',
|
|
50
|
+
breakdown: {
|
|
51
|
+
useEffectScore: 85,
|
|
52
|
+
propDrillingScore: 100,
|
|
53
|
+
componentSizeScore: 100,
|
|
54
|
+
memoizationScore: 100,
|
|
55
|
+
},
|
|
56
|
+
estimatedRefactorTime: '< 30 minutes',
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
it('should generate a markdown PR comment', () => {
|
|
60
|
+
const result = createMockResult();
|
|
61
|
+
const comment = generatePRComment(result, '/');
|
|
62
|
+
expect(comment).toContain('## 🔍 Code Smell Analysis Report');
|
|
63
|
+
expect(comment).toContain('Grade: **B**');
|
|
64
|
+
expect(comment).toContain('Total Issues | 1');
|
|
65
|
+
expect(comment).toContain('useEffect-overuse');
|
|
66
|
+
});
|
|
67
|
+
it('should include score breakdown', () => {
|
|
68
|
+
const result = createMockResult();
|
|
69
|
+
const comment = generatePRComment(result, '/');
|
|
70
|
+
expect(comment).toContain('Score Breakdown');
|
|
71
|
+
expect(comment).toContain('useEffect Usage');
|
|
72
|
+
expect(comment).toContain('85/100');
|
|
73
|
+
});
|
|
74
|
+
it('should generate inline comments for smells', () => {
|
|
75
|
+
const smells = [
|
|
76
|
+
{
|
|
77
|
+
type: 'debug-statement',
|
|
78
|
+
severity: 'warning',
|
|
79
|
+
message: 'console.log found',
|
|
80
|
+
file: '/src/utils/helper.ts',
|
|
81
|
+
line: 15,
|
|
82
|
+
column: 0,
|
|
83
|
+
suggestion: 'Remove debug statements',
|
|
84
|
+
},
|
|
85
|
+
];
|
|
86
|
+
const comments = generateInlineComments(smells, '/');
|
|
87
|
+
expect(comments).toHaveLength(1);
|
|
88
|
+
expect(comments[0].path).toBe('src/utils/helper.ts');
|
|
89
|
+
expect(comments[0].line).toBe(15);
|
|
90
|
+
expect(comments[0].body).toContain('debug-statement');
|
|
91
|
+
});
|
|
92
|
+
it('should group multiple smells on same line', () => {
|
|
93
|
+
const smells = [
|
|
94
|
+
{
|
|
95
|
+
type: 'debug-statement',
|
|
96
|
+
severity: 'warning',
|
|
97
|
+
message: 'console.log found',
|
|
98
|
+
file: '/src/utils/helper.ts',
|
|
99
|
+
line: 15,
|
|
100
|
+
column: 0,
|
|
101
|
+
suggestion: 'Remove debug statements',
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
type: 'js-var-usage',
|
|
105
|
+
severity: 'warning',
|
|
106
|
+
message: 'var usage detected',
|
|
107
|
+
file: '/src/utils/helper.ts',
|
|
108
|
+
line: 15,
|
|
109
|
+
column: 0,
|
|
110
|
+
suggestion: 'Use let or const',
|
|
111
|
+
},
|
|
112
|
+
];
|
|
113
|
+
const comments = generateInlineComments(smells, '/');
|
|
114
|
+
expect(comments).toHaveLength(1);
|
|
115
|
+
expect(comments[0].body).toContain('debug-statement');
|
|
116
|
+
expect(comments[0].body).toContain('js-var-usage');
|
|
117
|
+
});
|
|
118
|
+
});
|
package/dist/analyzer.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"analyzer.d.ts","sourceRoot":"","sources":["../src/analyzer.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"analyzer.d.ts","sourceRoot":"","sources":["../src/analyzer.ts"],"names":[],"mappings":"AAiCA,OAAO,EACL,cAAc,EAMd,cAAc,EAIf,MAAM,kBAAkB,CAAC;AAE1B,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,MAAM,CAAC,EAAE,OAAO,CAAC,cAAc,CAAC,CAAC;CAClC;AAED,wBAAsB,cAAc,CAAC,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,cAAc,CAAC,CA8DtF;AAmRD,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC"}
|
package/dist/analyzer.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import fg from 'fast-glob';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import { parseFile } from './parser/index.js';
|
|
4
|
-
import { detectUseEffectOveruse, detectPropDrilling, analyzePropDrillingDepth, detectLargeComponent, detectUnmemoizedCalculations, detectMissingKeys, detectHooksRulesViolations, detectDependencyArrayIssues, detectNestedTernaries, detectDeadCode, detectMagicValues, detectNextjsIssues, detectReactNativeIssues, detectNodejsIssues, detectJavascriptIssues, detectTypescriptIssues, detectDebugStatements, detectSecurityIssues, detectAccessibilityIssues, detectComplexity, detectMemoryLeaks, detectImportIssues, detectUnusedCode, } from './detectors/index.js';
|
|
4
|
+
import { detectUseEffectOveruse, detectPropDrilling, analyzePropDrillingDepth, detectLargeComponent, detectUnmemoizedCalculations, detectMissingKeys, detectHooksRulesViolations, detectDependencyArrayIssues, detectNestedTernaries, detectDeadCode, detectMagicValues, detectNextjsIssues, detectReactNativeIssues, detectNodejsIssues, detectJavascriptIssues, detectTypescriptIssues, detectDebugStatements, detectSecurityIssues, detectAccessibilityIssues, detectComplexity, detectMemoryLeaks, detectImportIssues, detectUnusedCode, detectServerComponentIssues, detectAsyncComponentIssues, } from './detectors/index.js';
|
|
5
|
+
import { parseCustomRules, detectCustomRuleViolations } from './customRules.js';
|
|
6
|
+
import { buildDependencyGraph } from './graphGenerator.js';
|
|
7
|
+
import { analyzeBundleImpact } from './bundleAnalyzer.js';
|
|
5
8
|
import { DEFAULT_CONFIG, } from './types/index.js';
|
|
6
9
|
export async function analyzeProject(options) {
|
|
7
10
|
const { rootDir, include = ['**/*.tsx', '**/*.jsx'], exclude = ['**/node_modules/**', '**/dist/**', '**/build/**', '**/*.test.*', '**/*.spec.*'], config: userConfig = {}, } = options;
|
|
@@ -30,6 +33,20 @@ export async function analyzeProject(options) {
|
|
|
30
33
|
// Calculate summary and score
|
|
31
34
|
const summary = calculateSummary(fileAnalyses);
|
|
32
35
|
const debtScore = calculateTechnicalDebtScore(fileAnalyses, summary);
|
|
36
|
+
// Generate dependency graph if requested
|
|
37
|
+
if (config.generateDependencyGraph) {
|
|
38
|
+
const importsData = fileAnalyses.map(f => ({ file: f.file, imports: f.imports }));
|
|
39
|
+
const graph = buildDependencyGraph(importsData, rootDir);
|
|
40
|
+
// Graph is available via extension (not in standard result yet)
|
|
41
|
+
global._dependencyGraph = graph;
|
|
42
|
+
}
|
|
43
|
+
// Analyze bundle size if requested
|
|
44
|
+
if (config.analyzeBundleSize) {
|
|
45
|
+
const allComponents = fileAnalyses.flatMap(f => f.components);
|
|
46
|
+
const bundleAnalysis = analyzeBundleImpact(fileAnalyses.map(f => ({ components: f.components, file: f.file })), fileAnalyses, '');
|
|
47
|
+
// Bundle analysis is available via extension
|
|
48
|
+
global._bundleAnalysis = bundleAnalysis;
|
|
49
|
+
}
|
|
33
50
|
return {
|
|
34
51
|
files: fileAnalyses,
|
|
35
52
|
summary,
|
|
@@ -83,6 +100,14 @@ function analyzeFile(parseResult, filePath, config) {
|
|
|
83
100
|
smells.push(...detectMemoryLeaks(component, filePath, sourceCode, config));
|
|
84
101
|
smells.push(...detectImportIssues(component, filePath, sourceCode, config));
|
|
85
102
|
smells.push(...detectUnusedCode(component, filePath, sourceCode, config));
|
|
103
|
+
// Server Components (React 19)
|
|
104
|
+
smells.push(...detectServerComponentIssues(component, filePath, sourceCode, config, imports));
|
|
105
|
+
smells.push(...detectAsyncComponentIssues(component, filePath, sourceCode, config));
|
|
106
|
+
// Custom rules
|
|
107
|
+
const customRules = parseCustomRules(config);
|
|
108
|
+
if (customRules.length > 0) {
|
|
109
|
+
smells.push(...detectCustomRuleViolations(component, filePath, sourceCode, customRules));
|
|
110
|
+
}
|
|
86
111
|
});
|
|
87
112
|
// Run cross-component analysis
|
|
88
113
|
smells.push(...analyzePropDrillingDepth(components, filePath, sourceCode, config));
|
|
@@ -191,6 +216,14 @@ function calculateSummary(files) {
|
|
|
191
216
|
// Unused code
|
|
192
217
|
'unused-export': 0,
|
|
193
218
|
'dead-import': 0,
|
|
219
|
+
// Server Components (React 19)
|
|
220
|
+
'server-component-hooks': 0,
|
|
221
|
+
'server-component-events': 0,
|
|
222
|
+
'server-component-browser-api': 0,
|
|
223
|
+
'async-client-component': 0,
|
|
224
|
+
'mixed-directives': 0,
|
|
225
|
+
// Custom rules
|
|
226
|
+
'custom-rule': 0,
|
|
194
227
|
};
|
|
195
228
|
const smellsBySeverity = {
|
|
196
229
|
error: 0,
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface ComponentBundleInfo {
|
|
2
|
+
name: string;
|
|
3
|
+
file: string;
|
|
4
|
+
estimatedSize: number;
|
|
5
|
+
dependencies: number;
|
|
6
|
+
exports: number;
|
|
7
|
+
complexity: number;
|
|
8
|
+
impact: 'low' | 'medium' | 'high' | 'critical';
|
|
9
|
+
}
|
|
10
|
+
export interface BundleAnalysisResult {
|
|
11
|
+
components: ComponentBundleInfo[];
|
|
12
|
+
totalEstimatedSize: number;
|
|
13
|
+
averageComponentSize: number;
|
|
14
|
+
largestComponents: ComponentBundleInfo[];
|
|
15
|
+
recommendations: string[];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Estimate bundle size impact per component
|
|
19
|
+
*/
|
|
20
|
+
export declare function analyzeBundleImpact(components: any[], files: any[], sourceCode: string): BundleAnalysisResult;
|
|
21
|
+
/**
|
|
22
|
+
* Generate HTML bundle analysis report
|
|
23
|
+
*/
|
|
24
|
+
export declare function generateBundleReport(analysis: BundleAnalysisResult, projectName: string): string;
|
|
25
|
+
//# sourceMappingURL=bundleAnalyzer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bundleAnalyzer.d.ts","sourceRoot":"","sources":["../src/bundleAnalyzer.ts"],"names":[],"mappings":"AAMA,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,UAAU,CAAC;CAChD;AAED,MAAM,WAAW,oBAAoB;IACnC,UAAU,EAAE,mBAAmB,EAAE,CAAC;IAClC,kBAAkB,EAAE,MAAM,CAAC;IAC3B,oBAAoB,EAAE,MAAM,CAAC;IAC7B,iBAAiB,EAAE,mBAAmB,EAAE,CAAC;IACzC,eAAe,EAAE,MAAM,EAAE,CAAC;CAC3B;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CACjC,UAAU,EAAE,GAAG,EAAE,EACjB,KAAK,EAAE,GAAG,EAAE,EACZ,UAAU,EAAE,MAAM,GACjB,oBAAoB,CAuBtB;AAuJD;;GAEG;AACH,wBAAgB,oBAAoB,CAClC,QAAQ,EAAE,oBAAoB,EAC9B,WAAW,EAAE,MAAM,GAClB,MAAM,CAmOR"}
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import _traverse from '@babel/traverse';
|
|
2
|
+
const traverse = typeof _traverse === 'function' ? _traverse : _traverse.default;
|
|
3
|
+
/**
|
|
4
|
+
* Estimate bundle size impact per component
|
|
5
|
+
*/
|
|
6
|
+
export function analyzeBundleImpact(components, files, sourceCode) {
|
|
7
|
+
const componentInfo = [];
|
|
8
|
+
for (const component of components) {
|
|
9
|
+
const info = estimateComponentSize(component, sourceCode);
|
|
10
|
+
componentInfo.push(info);
|
|
11
|
+
}
|
|
12
|
+
// Sort by size
|
|
13
|
+
componentInfo.sort((a, b) => b.estimatedSize - a.estimatedSize);
|
|
14
|
+
const totalSize = componentInfo.reduce((sum, c) => sum + c.estimatedSize, 0);
|
|
15
|
+
const avgSize = totalSize / componentInfo.length || 0;
|
|
16
|
+
const recommendations = generateBundleRecommendations(componentInfo, totalSize);
|
|
17
|
+
return {
|
|
18
|
+
components: componentInfo,
|
|
19
|
+
totalEstimatedSize: totalSize,
|
|
20
|
+
averageComponentSize: avgSize,
|
|
21
|
+
largestComponents: componentInfo.slice(0, 5),
|
|
22
|
+
recommendations,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Estimate size of a single component in bytes
|
|
27
|
+
*/
|
|
28
|
+
function estimateComponentSize(component, sourceCode) {
|
|
29
|
+
let size = 0;
|
|
30
|
+
let lineCount = 0;
|
|
31
|
+
let exportCount = 0;
|
|
32
|
+
let dependencyCount = 0;
|
|
33
|
+
let complexity = 0;
|
|
34
|
+
// Count lines
|
|
35
|
+
if (component.code) {
|
|
36
|
+
lineCount = component.code.split('\n').length;
|
|
37
|
+
size += lineCount * 4; // ~4 bytes per line average
|
|
38
|
+
}
|
|
39
|
+
// Count exports
|
|
40
|
+
component.path.traverse({
|
|
41
|
+
ExportNamedDeclaration() {
|
|
42
|
+
exportCount++;
|
|
43
|
+
size += 100; // export overhead
|
|
44
|
+
},
|
|
45
|
+
ExportDefaultDeclaration() {
|
|
46
|
+
exportCount++;
|
|
47
|
+
size += 100;
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
// Count dependencies
|
|
51
|
+
component.path.traverse({
|
|
52
|
+
ImportDeclaration() {
|
|
53
|
+
dependencyCount++;
|
|
54
|
+
size += 80;
|
|
55
|
+
},
|
|
56
|
+
CallExpression(path) {
|
|
57
|
+
if (path.node.callee.type === 'Import') {
|
|
58
|
+
dependencyCount++;
|
|
59
|
+
size += 100; // dynamic import is heavier
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
// Estimate complexity
|
|
64
|
+
component.path.traverse({
|
|
65
|
+
IfStatement() {
|
|
66
|
+
complexity++;
|
|
67
|
+
size += 30;
|
|
68
|
+
},
|
|
69
|
+
FunctionDeclaration() {
|
|
70
|
+
complexity++;
|
|
71
|
+
size += 50;
|
|
72
|
+
},
|
|
73
|
+
ArrowFunctionExpression() {
|
|
74
|
+
complexity++;
|
|
75
|
+
size += 40;
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
// Add base overhead
|
|
79
|
+
size += 200;
|
|
80
|
+
const impact = determineImpact(size, lineCount, complexity);
|
|
81
|
+
return {
|
|
82
|
+
name: component.name || 'Anonymous',
|
|
83
|
+
file: component.file || 'unknown',
|
|
84
|
+
estimatedSize: size,
|
|
85
|
+
dependencies: dependencyCount,
|
|
86
|
+
exports: exportCount,
|
|
87
|
+
complexity: lineCount,
|
|
88
|
+
impact,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Determine bundle impact level
|
|
93
|
+
*/
|
|
94
|
+
function determineImpact(size, lines, complexity) {
|
|
95
|
+
let score = 0;
|
|
96
|
+
// Size factor
|
|
97
|
+
if (size > 10000)
|
|
98
|
+
score += 3; // 10KB+
|
|
99
|
+
else if (size > 5000)
|
|
100
|
+
score += 2; // 5KB+
|
|
101
|
+
else if (size > 2000)
|
|
102
|
+
score += 1; // 2KB+
|
|
103
|
+
// Lines factor
|
|
104
|
+
if (lines > 500)
|
|
105
|
+
score += 3;
|
|
106
|
+
else if (lines > 300)
|
|
107
|
+
score += 2;
|
|
108
|
+
else if (lines > 150)
|
|
109
|
+
score += 1;
|
|
110
|
+
// Complexity factor
|
|
111
|
+
if (complexity > 50)
|
|
112
|
+
score += 2;
|
|
113
|
+
else if (complexity > 30)
|
|
114
|
+
score += 1;
|
|
115
|
+
if (score >= 7)
|
|
116
|
+
return 'critical';
|
|
117
|
+
if (score >= 5)
|
|
118
|
+
return 'high';
|
|
119
|
+
if (score >= 2)
|
|
120
|
+
return 'medium';
|
|
121
|
+
return 'low';
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Generate recommendations to reduce bundle size
|
|
125
|
+
*/
|
|
126
|
+
function generateBundleRecommendations(components, totalSize) {
|
|
127
|
+
const recommendations = [];
|
|
128
|
+
// Check for large components
|
|
129
|
+
const largeComps = components.filter(c => c.impact === 'critical' || c.impact === 'high');
|
|
130
|
+
if (largeComps.length > 0) {
|
|
131
|
+
recommendations.push(`Consider splitting large components: ${largeComps.slice(0, 3).map(c => c.name).join(', ')}`);
|
|
132
|
+
}
|
|
133
|
+
// Check for high dependency count
|
|
134
|
+
const highDepComps = components.filter(c => c.dependencies > 10);
|
|
135
|
+
if (highDepComps.length > 0) {
|
|
136
|
+
recommendations.push(`These components have many dependencies (${highDepComps.length}). Consider lazy loading or code splitting.`);
|
|
137
|
+
}
|
|
138
|
+
// Check total size
|
|
139
|
+
if (totalSize > 100000) {
|
|
140
|
+
recommendations.push(`Total estimated bundle size is ${(totalSize / 1024).toFixed(1)}KB. Consider code-splitting or tree-shaking unused imports.`);
|
|
141
|
+
}
|
|
142
|
+
// Check for high complexity
|
|
143
|
+
const complexComps = components.filter(c => c.complexity > 300);
|
|
144
|
+
if (complexComps.length > 0) {
|
|
145
|
+
recommendations.push(`${complexComps.length} component(s) have high complexity (>300 LOC). Extract logic to custom hooks or utilities.`);
|
|
146
|
+
}
|
|
147
|
+
return recommendations.length > 0
|
|
148
|
+
? recommendations
|
|
149
|
+
: ['✅ Bundle size is well-optimized. Keep monitoring as code grows.'];
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Generate HTML bundle analysis report
|
|
153
|
+
*/
|
|
154
|
+
export function generateBundleReport(analysis, projectName) {
|
|
155
|
+
const formatSize = (bytes) => {
|
|
156
|
+
if (bytes > 1024 * 1024)
|
|
157
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
|
|
158
|
+
if (bytes > 1024)
|
|
159
|
+
return `${(bytes / 1024).toFixed(1)}KB`;
|
|
160
|
+
return `${bytes}B`;
|
|
161
|
+
};
|
|
162
|
+
const impactColor = {
|
|
163
|
+
low: '#4caf50',
|
|
164
|
+
medium: '#ff9800',
|
|
165
|
+
high: '#f44336',
|
|
166
|
+
critical: '#b71c1c',
|
|
167
|
+
};
|
|
168
|
+
return `<!DOCTYPE html>
|
|
169
|
+
<html lang="en">
|
|
170
|
+
<head>
|
|
171
|
+
<meta charset="UTF-8">
|
|
172
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
173
|
+
<title>Bundle Analysis - ${projectName}</title>
|
|
174
|
+
<style>
|
|
175
|
+
body {
|
|
176
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
|
|
177
|
+
background: #f5f5f5;
|
|
178
|
+
margin: 0;
|
|
179
|
+
padding: 20px;
|
|
180
|
+
}
|
|
181
|
+
.container {
|
|
182
|
+
max-width: 1200px;
|
|
183
|
+
margin: 0 auto;
|
|
184
|
+
background: white;
|
|
185
|
+
border-radius: 8px;
|
|
186
|
+
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
|
187
|
+
padding: 30px;
|
|
188
|
+
}
|
|
189
|
+
h1 {
|
|
190
|
+
color: #1976d2;
|
|
191
|
+
margin: 0 0 10px 0;
|
|
192
|
+
}
|
|
193
|
+
.stats-grid {
|
|
194
|
+
display: grid;
|
|
195
|
+
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
|
196
|
+
gap: 20px;
|
|
197
|
+
margin: 30px 0;
|
|
198
|
+
}
|
|
199
|
+
.stat-card {
|
|
200
|
+
background: #f8f9fa;
|
|
201
|
+
border-left: 4px solid #1976d2;
|
|
202
|
+
padding: 20px;
|
|
203
|
+
border-radius: 4px;
|
|
204
|
+
}
|
|
205
|
+
.stat-label {
|
|
206
|
+
font-size: 12px;
|
|
207
|
+
color: #666;
|
|
208
|
+
text-transform: uppercase;
|
|
209
|
+
letter-spacing: 0.5px;
|
|
210
|
+
}
|
|
211
|
+
.stat-value {
|
|
212
|
+
font-size: 28px;
|
|
213
|
+
font-weight: bold;
|
|
214
|
+
color: #1976d2;
|
|
215
|
+
margin-top: 8px;
|
|
216
|
+
}
|
|
217
|
+
.recommendations {
|
|
218
|
+
background: #e3f2fd;
|
|
219
|
+
border-left: 4px solid #1976d2;
|
|
220
|
+
padding: 20px;
|
|
221
|
+
border-radius: 4px;
|
|
222
|
+
margin: 30px 0;
|
|
223
|
+
}
|
|
224
|
+
.recommendations h3 {
|
|
225
|
+
margin: 0 0 10px 0;
|
|
226
|
+
color: #1976d2;
|
|
227
|
+
}
|
|
228
|
+
.recommendations ul {
|
|
229
|
+
margin: 0;
|
|
230
|
+
padding-left: 20px;
|
|
231
|
+
}
|
|
232
|
+
.recommendations li {
|
|
233
|
+
margin: 8px 0;
|
|
234
|
+
color: #333;
|
|
235
|
+
}
|
|
236
|
+
table {
|
|
237
|
+
width: 100%;
|
|
238
|
+
border-collapse: collapse;
|
|
239
|
+
margin-top: 30px;
|
|
240
|
+
}
|
|
241
|
+
th {
|
|
242
|
+
background: #f5f5f5;
|
|
243
|
+
padding: 12px;
|
|
244
|
+
text-align: left;
|
|
245
|
+
font-weight: 600;
|
|
246
|
+
border-bottom: 2px solid #e0e0e0;
|
|
247
|
+
}
|
|
248
|
+
td {
|
|
249
|
+
padding: 12px;
|
|
250
|
+
border-bottom: 1px solid #e0e0e0;
|
|
251
|
+
}
|
|
252
|
+
tr:hover {
|
|
253
|
+
background: #fafafa;
|
|
254
|
+
}
|
|
255
|
+
.impact-badge {
|
|
256
|
+
display: inline-block;
|
|
257
|
+
padding: 4px 12px;
|
|
258
|
+
border-radius: 12px;
|
|
259
|
+
font-size: 12px;
|
|
260
|
+
font-weight: 600;
|
|
261
|
+
color: white;
|
|
262
|
+
}
|
|
263
|
+
.impact-low {
|
|
264
|
+
background: #4caf50;
|
|
265
|
+
}
|
|
266
|
+
.impact-medium {
|
|
267
|
+
background: #ff9800;
|
|
268
|
+
}
|
|
269
|
+
.impact-high {
|
|
270
|
+
background: #f44336;
|
|
271
|
+
}
|
|
272
|
+
.impact-critical {
|
|
273
|
+
background: #b71c1c;
|
|
274
|
+
}
|
|
275
|
+
.size-bar {
|
|
276
|
+
display: inline-block;
|
|
277
|
+
height: 8px;
|
|
278
|
+
background: #1976d2;
|
|
279
|
+
border-radius: 4px;
|
|
280
|
+
margin-right: 8px;
|
|
281
|
+
vertical-align: middle;
|
|
282
|
+
}
|
|
283
|
+
</style>
|
|
284
|
+
</head>
|
|
285
|
+
<body>
|
|
286
|
+
<div class="container">
|
|
287
|
+
<h1>📦 Bundle Size Analysis - ${projectName}</h1>
|
|
288
|
+
<p style="color: #666; margin-top: 5px;">Estimated size impact per component</p>
|
|
289
|
+
|
|
290
|
+
<div class="stats-grid">
|
|
291
|
+
<div class="stat-card">
|
|
292
|
+
<div class="stat-label">Total Size</div>
|
|
293
|
+
<div class="stat-value">${formatSize(analysis.totalEstimatedSize)}</div>
|
|
294
|
+
</div>
|
|
295
|
+
<div class="stat-card">
|
|
296
|
+
<div class="stat-label">Components</div>
|
|
297
|
+
<div class="stat-value">${analysis.components.length}</div>
|
|
298
|
+
</div>
|
|
299
|
+
<div class="stat-card">
|
|
300
|
+
<div class="stat-label">Avg Component</div>
|
|
301
|
+
<div class="stat-value">${formatSize(analysis.averageComponentSize)}</div>
|
|
302
|
+
</div>
|
|
303
|
+
</div>
|
|
304
|
+
|
|
305
|
+
${analysis.recommendations.length > 0
|
|
306
|
+
? `
|
|
307
|
+
<div class="recommendations">
|
|
308
|
+
<h3>💡 Recommendations</h3>
|
|
309
|
+
<ul>
|
|
310
|
+
${analysis.recommendations.map(rec => `<li>${rec}</li>`).join('')}
|
|
311
|
+
</ul>
|
|
312
|
+
</div>
|
|
313
|
+
`
|
|
314
|
+
: ''}
|
|
315
|
+
|
|
316
|
+
<h2 style="margin-top: 40px; color: #333;">📊 Component Breakdown</h2>
|
|
317
|
+
<table>
|
|
318
|
+
<thead>
|
|
319
|
+
<tr>
|
|
320
|
+
<th>Component</th>
|
|
321
|
+
<th>Est. Size</th>
|
|
322
|
+
<th>LOC</th>
|
|
323
|
+
<th>Dependencies</th>
|
|
324
|
+
<th>Exports</th>
|
|
325
|
+
<th>Impact</th>
|
|
326
|
+
</tr>
|
|
327
|
+
</thead>
|
|
328
|
+
<tbody>
|
|
329
|
+
${analysis.components
|
|
330
|
+
.map(c => `
|
|
331
|
+
<tr>
|
|
332
|
+
<td><strong>${c.name}</strong></td>
|
|
333
|
+
<td>${formatSize(c.estimatedSize)}</td>
|
|
334
|
+
<td>${c.complexity}</td>
|
|
335
|
+
<td>${c.dependencies}</td>
|
|
336
|
+
<td>${c.exports}</td>
|
|
337
|
+
<td>
|
|
338
|
+
<span class="impact-badge impact-${c.impact}">${c.impact.toUpperCase()}</span>
|
|
339
|
+
</td>
|
|
340
|
+
</tr>
|
|
341
|
+
`)
|
|
342
|
+
.join('')}
|
|
343
|
+
</tbody>
|
|
344
|
+
</table>
|
|
345
|
+
|
|
346
|
+
<h2 style="margin-top: 40px; color: #333;">🏆 Largest Components</h2>
|
|
347
|
+
<table>
|
|
348
|
+
<thead>
|
|
349
|
+
<tr>
|
|
350
|
+
<th>Rank</th>
|
|
351
|
+
<th>Component</th>
|
|
352
|
+
<th>Size</th>
|
|
353
|
+
<th>% of Total</th>
|
|
354
|
+
</tr>
|
|
355
|
+
</thead>
|
|
356
|
+
<tbody>
|
|
357
|
+
${analysis.largestComponents
|
|
358
|
+
.map((c, i) => `
|
|
359
|
+
<tr>
|
|
360
|
+
<td><strong>#${i + 1}</strong></td>
|
|
361
|
+
<td>${c.name}</td>
|
|
362
|
+
<td>${formatSize(c.estimatedSize)}</td>
|
|
363
|
+
<td>
|
|
364
|
+
${((c.estimatedSize / analysis.totalEstimatedSize) * 100).toFixed(1)}%
|
|
365
|
+
<div class="size-bar" style="width: ${((c.estimatedSize / analysis.totalEstimatedSize) * 200).toFixed(0)}px;"></div>
|
|
366
|
+
</td>
|
|
367
|
+
</tr>
|
|
368
|
+
`)
|
|
369
|
+
.join('')}
|
|
370
|
+
</tbody>
|
|
371
|
+
</table>
|
|
372
|
+
</div>
|
|
373
|
+
</body>
|
|
374
|
+
</html>`;
|
|
375
|
+
}
|