Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | 251x 251x 251x 251x 251x 251x 251x 251x 251x 251x 251x 251x 251x 251x 26x 20x 16x 1x 26x 251x 251x 251x 251x 251x 251x 3x 3x 3x 3x 3x 3x 3x 3x 3x 251x 251x 251x 251x 251x 251x 33x 33x 33x 33x 33x 42x 42x 27x 42x 15x 15x 42x 33x 33x 251x 251x 251x 251x 251x 251x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 251x 251x 251x 251x 251x 251x 251x 251x 251x 251x 251x 251x 251x 251x 251x 251x 6x 6x 6x 6x 6x 6x 3x 3x 3x 3x 3x 6x 251x 251x 251x 251x 251x 251x 251x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 12x 9x 6x 15x 15x 15x 15x 15x 12x 15x 3x 3x 3x 3x 3x 3x 3x 3x 15x 15x 15x 15x 15x 15x 15x 15x 15x 12x 12x 15x 15x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 15x 15x 15x 15x 12x 9x 6x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 6x 6x 6x 6x 6x 3x 3x 3x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 251x 251x 251x 251x 251x 251x 251x 5x 5x 5x 5x 2x 2x 2x 3x 3x 5x 5x 5x 5x 5x 5x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 4x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 5x 5x 4x 6x 6x 6x 6x 6x 6x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 5x 5x 5x 5x 5x | import fs from 'fs';
import path from 'path';
import ComponentParser from '../../lib/compiler/ComponentParser.js';
import StyleProcessor from '../../lib/compiler/StyleProcessor.js';
import { AtlasNodeKind } from '../../lib/compiler/atlas/AppModel.js';
import { buildModel } from './atlas.js';
import { bold, cyan, green, yellow, gray } from '../colors.js';
/**
* Formats byte counts into human readable strings (e.g. 500 B, 1.25 KB, 2.10 MB).
* @param {number} bytes - Size in bytes.
* @returns {string} Formatted size string.
*/
export function formatBytes(bytes) {
if (bytes === 0 || isNaN(bytes)) return '0 B';
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(2)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(2)} MB`;
}
/**
* Converts a string to PascalCase.
* @param {string} str
* @returns {string}
*/
function toPascalCase(str) {
if (!str) return '';
return str
.replace(/[-_.]+/g, ' ')
.split(' ')
.filter(Boolean)
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join('');
}
/**
* Recursively gets all files in a directory.
* @param {string} dir
* @returns {string[]}
*/
function getAllFiles(dir) {
if (!fs.existsSync(dir)) return [];
const results = [];
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
results.push(...getAllFiles(fullPath));
} else if (entry.isFile()) {
results.push(fullPath);
}
}
return results;
}
/**
* Extracts raw template content from component source code.
* @param {string} content
* @returns {string}
*/
function extractRawTemplate(content) {
if (!content) return '';
return content
.replace(/<!--[\s\S]*?-->/g, '')
.replace(/<state.*? \/>/g, '')
.replace(/<computed.*? \/>/g, '')
.replace(/<action.*?>[\s\S]*?<\/action>/g, '')
.replace(/<resource.*?>[\s\S]*?<\/resource>/g, '')
.replace(/<resource\s+[\s\S]*?\/>/gi, '')
.trim();
}
/**
* Reads what each unit declares from the compiler's semantic model.
*
* Keyed by file path rather than by name: `stats` derives a display name from
* whatever `class X` a file contains, while the compiler names a unit after
* its file. Those disagree often enough that matching on them would silently
* report zero. The path is the same fact on both sides.
*
* Falls back to per-file parsing when the model cannot be built — `stats` is a
* footprint report and should still produce byte sizes for a project that does
* not currently compile.
* @param {object} cli - The AvenxCLI instance.
* @returns {{available: boolean, stateCounts: Map<string, number>}} Declared
* state counts by root-relative file path.
*/
function collectSemantics(cli) {
/** @type {Map<string, number>} */
const stateCounts = new Map();
try {
const model = buildModel(cli);
if (model.errors.length > 0) {
return { available: false, stateCounts };
}
for (const node of model.nodes.values()) {
if (node.kind !== AtlasNodeKind.STATE || !node.owner) continue;
const owner = model.getNode(node.owner);
if (!owner || !owner.file) continue;
stateCounts.set(owner.file, (stateCounts.get(owner.file) || 0) + 1);
}
return { available: true, stateCounts };
} catch {
return { available: false, stateCounts };
}
}
/**
* Scans the project source directory and collects component, page, bridge, and guard metrics.
* @param {object} cli - AvenxCLI instance containing baseDir and config.
* @returns {object} Analysis result containing summary and item metrics.
*/
export function analyzeStats(cli) {
const srcRel = (cli.config && cli.config.srcDir) || 'src';
const rootDir = cli.baseDir || process.cwd();
const srcDir = path.join(rootDir, srcRel);
const allFiles = getAllFiles(srcDir);
// ComponentParser's first argument is a StyleProcessor. It used to be handed
// the config object, so every styleProcessor.process() call threw into the
// catch below and scoped CSS was silently reported as zero bytes.
const parser = new ComponentParser(new StyleProcessor((cli.config && cli.config.style) || {}, cli.config), (cli.config && cli.config.voidTags) || [], cli.config);
// Byte sizes have to be measured from the files. What each file *is*, and
// what it declares, is something the compiler already knows, so those come
// from the model rather than from a second set of regexes.
const semantics = collectSemantics(cli);
const items = [];
let totalFiles = 0;
let totalComponents = 0;
let totalPages = 0;
let totalBridges = 0;
let totalGuards = 0;
let totalFileSizeBytes = 0;
let totalRawTemplateBytes = 0;
let totalCompiledTemplateBytes = 0;
let totalScopedCssBytes = 0;
let totalStateProps = 0;
for (const file of allFiles) {
if (!file.endsWith('.js') && !file.endsWith('.ts')) continue;
const relPath = path.relative(rootDir, file).split(path.sep).join('/');
const filename = path.basename(file);
const stat = fs.statSync(file);
const fileSizeBytes = stat.size;
const isPage = file.includes(`${path.sep}pages${path.sep}`) || filename.endsWith('.page.js');
const isBridge =
file.includes(`${path.sep}bridges${path.sep}`) ||
file.includes(`${path.sep}global${path.sep}`) ||
filename.endsWith('.bridge.js');
const isGuard = file.includes(`${path.sep}guards${path.sep}`) || filename.endsWith('.guard.js');
const isComponent =
!isPage &&
!isBridge &&
!isGuard &&
(file.includes(`${path.sep}components${path.sep}`) || filename.endsWith('.component.js'));
let type = 'other';
if (isComponent) type = 'component';
else if (isPage) type = 'page';
else if (isBridge) type = 'bridge';
else if (isGuard) type = 'guard';
const content = fs.readFileSync(file, 'utf-8');
const classMatch = content.match(/(?:export\s+(?:default\s+)?)?class\s+([A-Za-z0-9_$]+)/);
let name;
if (classMatch) {
name = classMatch[1];
} else {
const base = filename
.replace(/\.component\.js$/, '')
.replace(/\.page\.js$/, '')
.replace(/\.bridge\.js$/, '')
.replace(/\.guard\.js$/, '')
.replace(/\.js$/, '');
name = toPascalCase(base);
}
let statePropsCount = semantics.stateCounts.get(relPath) || 0;
let rawTemplateBytes = 0;
let compiledTemplateBytes = 0;
let scopedCssBytes = 0;
try {
const state = parser.expressionParser.parseState(content);
if (!semantics.available) {
statePropsCount = Object.keys(state || {}).length;
}
if (isComponent || isPage) {
// 2. Raw template extraction & byte size
const rawTpl = extractRawTemplate(content);
rawTemplateBytes = Buffer.byteLength(rawTpl, 'utf8');
// 3. Compiled template extraction & byte size.
//
// The declarations have to be read before the template is transformed,
// even though only its size is wanted here. `extractTemplate` validates
// every identifier a binding names against the state, computed values
// and actions it is given, so calling it with empty maps reported an
// action the component plainly declares as undeclared -- `avenx stats`
// printed AVX_W03 for a file `avenx build` and `avenx check` both
// accepted. Passing what the file actually declares costs one extra
// parse of a string already in memory.
const computed = parser.extractComputed(content);
const methods = parser.extractMethods(content, name, file);
const resources = parser.expressionParser.parseResources(content);
const compiledTpl = parser.extractTemplate(content, {}, name, file, state, computed, methods, resources);
compiledTemplateBytes = Buffer.byteLength(compiledTpl || '', 'utf8');
// 4. Scoped CSS extraction & byte size
let rawCss = '';
const styleMatch = content.match(/<style[\s\S]*?>([\s\S]*?)<\/style>/i);
if (styleMatch) {
rawCss = styleMatch[1];
}
const cssFile = file.replace(/\.(component|page)\.js$/, '.$1.css');
if (fs.existsSync(cssFile)) {
rawCss += '\n' + fs.readFileSync(cssFile, 'utf-8');
}
if (rawCss.trim()) {
const scopedCss = parser.styleProcessor.process(rawCss, {}, name, '');
scopedCssBytes = Buffer.byteLength(scopedCss || '', 'utf8');
}
}
} catch {
// Fallback on parse failure
}
totalFiles++;
if (isComponent) totalComponents++;
else if (isPage) totalPages++;
else if (isBridge) totalBridges++;
else if (isGuard) totalGuards++;
totalFileSizeBytes += fileSizeBytes;
totalRawTemplateBytes += rawTemplateBytes;
totalCompiledTemplateBytes += compiledTemplateBytes;
totalScopedCssBytes += scopedCssBytes;
totalStateProps += statePropsCount;
items.push({
name,
type,
file: relPath,
fileSizeBytes,
rawTemplateBytes,
compiledTemplateBytes,
scopedCssBytes,
statePropsCount,
});
}
items.sort((a, b) => a.file.localeCompare(b.file));
let templateReductionPercent = 0;
if (totalRawTemplateBytes > 0) {
const diff = totalRawTemplateBytes - totalCompiledTemplateBytes;
templateReductionPercent = Number(((diff / totalRawTemplateBytes) * 100).toFixed(1));
}
return {
summary: {
totalFiles,
totalComponents,
totalPages,
totalBridges,
totalGuards,
totalFileSizeBytes,
totalRawTemplateBytes,
totalCompiledTemplateBytes,
totalScopedCssBytes,
totalStateProps,
templateReductionPercent,
},
items,
};
}
/**
* Runs the `avenx stats` CLI command to output footprint metrics in text or JSON format.
* @param {object} cli - AvenxCLI instance.
* @param {string[]} [args] - Command arguments.
*/
export function runStats(cli, args = []) {
const isJson = args.includes('--json') || args.includes('-j');
const statsData = analyzeStats(cli);
if (isJson) {
console.log(JSON.stringify(statsData, null, 2));
return;
}
const { summary, items } = statsData;
const srcRel = (cli.config && cli.config.srcDir) || 'src';
console.log(bold(cyan(`📊 Avenx Component & Bundle Footprint Metrics (${srcRel}/)`)));
console.log(gray(`Project: ${cli.baseDir}\n`));
if (items.length === 0) {
console.log(gray('No source files found in ') + cyan(`${srcRel}/`) + gray('.'));
return;
}
// Print Items Table
console.log(
bold(
[
'Name'.padEnd(25),
'Type'.padEnd(12),
'File Size'.padStart(10),
'Raw Tpl'.padStart(10),
'Comp Tpl'.padStart(10),
'CSS Size'.padStart(10),
'State'.padStart(7),
].join(' '),
),
);
console.log(gray('─'.repeat(85)));
for (const item of items) {
const fileStr = formatBytes(item.fileSizeBytes).padStart(10);
const rawStr = item.rawTemplateBytes > 0 ? formatBytes(item.rawTemplateBytes).padStart(10) : gray('-'.padStart(10));
const compStr =
item.compiledTemplateBytes > 0 ? formatBytes(item.compiledTemplateBytes).padStart(10) : gray('-'.padStart(10));
const cssStr = item.scopedCssBytes > 0 ? formatBytes(item.scopedCssBytes).padStart(10) : gray('-'.padStart(10));
const stateStr = String(item.statePropsCount).padStart(7);
const typeStr =
item.type === 'component'
? green('Component'.padEnd(12))
: item.type === 'page'
? cyan('Page'.padEnd(12))
: item.type === 'bridge'
? yellow('Bridge'.padEnd(12))
: gray(item.type.padEnd(12));
console.log(
`${item.name.padEnd(25)} ${typeStr} ${fileStr} ${rawStr} ${compStr} ${cssStr} ${stateStr}`,
);
}
console.log(gray('─'.repeat(85)));
// Summary Totals
console.log(`\n${bold(cyan('Summary Totals:'))}`);
console.log(` ${gray('Total Files:')} ${bold(String(summary.totalFiles))}`);
console.log(
` ${gray(' Components:')} ${green(String(summary.totalComponents))} | ${gray('Pages:')} ${cyan(String(summary.totalPages))} | ${gray('Bridges:')} ${yellow(String(summary.totalBridges))}`,
);
console.log(` ${gray('Total Source Size:')} ${bold(formatBytes(summary.totalFileSizeBytes))}`);
console.log(` ${gray('Raw Template Payload:')} ${formatBytes(summary.totalRawTemplateBytes)}`);
console.log(
` ${gray('Compiled Template:')} ${formatBytes(summary.totalCompiledTemplateBytes)} (${summary.templateReductionPercent >= 0 ? `-${summary.templateReductionPercent}%` : `+${Math.abs(summary.templateReductionPercent)}%`})`,
);
console.log(` ${gray('Scoped CSS Payload:')} ${formatBytes(summary.totalScopedCssBytes)}`);
console.log(` ${gray('State Properties:')} ${summary.totalStateProps}\n`);
}
|