-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest-tree-shaking.mjs
More file actions
216 lines (180 loc) · 7.73 KB
/
test-tree-shaking.mjs
File metadata and controls
216 lines (180 loc) · 7.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { optimize } from './crates/swc_macro_wasm/pkg/swc_macro_wasm.js';
import fs from 'fs';
// Feature to module dependency mapping
const FEATURE_MODULE_DEPENDENCIES = {
'features.enableFeatureA': ['153', '418', '78'], // featureA + dataProcessor + heavyMathUtils
'features.enableFeatureB': ['722', '803', '812'], // featureB + expensiveUIUtils + networkUtils
'features.enableDebugMode': ['422'] // debugUtils
};
// All module IDs and their descriptions
const ALL_MODULES = {
'418': 'dataProcessor',
'422': 'debugUtils',
'803': 'expensiveUIUtils',
'153': 'featureA',
'722': 'featureB',
'78': 'heavyMathUtils',
'812': 'networkUtils'
};
function getExpectedModulesForConfig(config) {
const expectedModules = new Set();
// Add modules for enabled features
Object.entries(config).forEach(([feature, enabled]) => {
if (enabled) {
const featureKey = `features.${feature}`;
const dependencies = FEATURE_MODULE_DEPENDENCIES[featureKey] || [];
dependencies.forEach(moduleId => expectedModules.add(moduleId));
}
});
return expectedModules;
}
function analyzeBundle(code, description, config = null) {
// Basic stats
const totalSize = code.length;
const totalLines = code.split('\n').length;
// Check which modules are present
const moduleStatuses = {};
if (config) {
const expectedModules = getExpectedModulesForConfig(config);
Object.entries(ALL_MODULES).forEach(([id, name]) => {
const isPresent = code.includes(`${id}:`);
const shouldBePresent = expectedModules.has(id);
if (shouldBePresent && isPresent) {
moduleStatuses[id] = `✅ ${name} (correctly included)`;
} else if (!shouldBePresent && !isPresent) {
moduleStatuses[id] = `✅ ${name} (correctly removed)`;
} else if (!shouldBePresent && isPresent) {
moduleStatuses[id] = `❌ ${name} (should be removed but kept)`;
} else if (shouldBePresent && !isPresent) {
moduleStatuses[id] = `❌ ${name} (should be present but missing)`;
}
});
} else {
// For original bundle, just show present/absent without judgment
Object.entries(ALL_MODULES).forEach(([id, name]) => {
const isPresent = code.includes(`${id}:`);
moduleStatuses[id] = isPresent ? `✅ ${name}` : `❌ ${name} (not present)`;
});
}
console.log(`📊 ${description}:`);
console.log(` Size: ${totalSize} chars (${totalLines} lines)`);
if (config) {
const expectedModules = getExpectedModulesForConfig(config);
const actualModules = new Set();
Object.entries(ALL_MODULES).forEach(([id, name]) => {
if (code.includes(`${id}:`)) {
actualModules.add(id);
}
});
console.log(` Expected modules for this config: [${Array.from(expectedModules).join(', ')}]`);
console.log(` Actually present modules: [${Array.from(actualModules).join(', ')}]`);
// Check for correctly removed modules
const shouldBeRemoved = new Set();
Object.keys(ALL_MODULES).forEach(id => {
if (!expectedModules.has(id)) {
shouldBeRemoved.add(id);
}
});
const correctlyRemoved = [];
const incorrectlyKept = [];
shouldBeRemoved.forEach(id => {
if (!actualModules.has(id)) {
correctlyRemoved.push(id);
} else {
incorrectlyKept.push(id);
}
});
if (correctlyRemoved.length > 0) {
console.log(` ✅ Correctly removed: [${correctlyRemoved.map(id => ALL_MODULES[id]).join(', ')}]`);
}
if (incorrectlyKept.length > 0) {
console.log(` ❌ Should be removed but kept: [${incorrectlyKept.map(id => ALL_MODULES[id]).join(', ')}]`);
}
}
console.log(` Module-by-module status:`);
Object.entries(moduleStatuses).forEach(([id, status]) => {
console.log(` ${id}: ${status}`);
});
// Count issues for summary
const issues = Object.values(moduleStatuses).filter(status => status.includes('❌')).length;
const correct = Object.values(moduleStatuses).filter(status => status.includes('✅')).length;
if (config && issues > 0) {
console.log(` 📊 Summary: ${correct} correct, ${issues} issues`);
}
return { totalSize, totalLines, moduleStatuses, issues: issues || 0 };
}
async function testTreeShaking() {
console.log('🌳 Tree Shaking Demo - Processing bundler-chunk.js');
console.log('============================================================');
// Read the bundled code
const originalCode = fs.readFileSync('bundler-chunk.js', 'utf8');
// Analyze original bundle
const originalStats = analyzeBundle(originalCode, 'Original Bundle');
console.log('');
try {
console.log('✅ WASM module loaded successfully');
console.log('');
console.log('🔄 Testing different feature flag configurations...');
console.log('');
// Test configurations with expected results
const testConfigs = [
{
name: 'allEnabled',
config: { enableFeatureA: true, enableFeatureB: true, enableDebugMode: true },
description: 'All features enabled - should keep all modules'
},
{
name: 'onlyFeatureA',
config: { enableFeatureA: true, enableFeatureB: false, enableDebugMode: false },
description: 'Only Feature A - should remove featureB, expensiveUIUtils, networkUtils, debugUtils'
},
{
name: 'onlyFeatureB',
config: { enableFeatureA: false, enableFeatureB: true, enableDebugMode: false },
description: 'Only Feature B - should remove featureA, heavyMathUtils, dataProcessor, debugUtils'
},
{
name: 'minimal',
config: { enableFeatureA: false, enableFeatureB: false, enableDebugMode: false },
description: 'No features - should remove all feature modules, keep only base functionality'
}
];
for (const test of testConfigs) {
console.log(`⚙️ Testing: ${test.name}`);
console.log(` Config: ${JSON.stringify(test.config)}`);
console.log(` Expected: ${test.description}`);
const startTime = performance.now();
// Use the old format that the WASM expects
const configForWasm = { features: test.config };
const optimizedCode = optimize(originalCode, JSON.stringify(configForWasm));
const endTime = performance.now();
const optimizedStats = analyzeBundle(optimizedCode, `Optimized (${test.name})`, test.config);
console.log('');
console.log(` ⚡ Optimized in ${(endTime - startTime).toFixed(2)}ms`);
const sizeDiff = optimizedStats.totalSize - originalStats.totalSize;
const sizePercent = ((sizeDiff / originalStats.totalSize) * 100).toFixed(1);
console.log(` 📉 Size change: ${originalStats.totalSize} → ${optimizedStats.totalSize} chars (${sizePercent > 0 ? '+' : ''}${sizePercent}%)`);
console.log('');
console.log(` 📄 Optimized output (${test.name}):`);
console.log(' ==================================================');
// Show a condensed version of the output
const lines = optimizedCode.split('\n');
if (lines.length > 50) {
console.log(lines.slice(0, 25).join('\n'));
console.log(' ... (truncated) ...');
console.log(lines.slice(-25).join('\n'));
} else {
console.log(optimizedCode);
}
console.log(' ==================================================');
console.log('--------------------------------------------------');
}
console.log('');
console.log('🎯 Tree Shaking Analysis Summary:');
console.log('✅ Conditional compilation working at entry point level');
console.log('❓ Module-level tree shaking needs enhancement');
} catch (error) {
console.error('❌ Error during processing:', error);
}
}
testTreeShaking();