@vocento/wpo-check
Version:
An internal CLI tool to measure and validate Web Performance KPIs during development and CI/CD workflows.
135 lines (119 loc) • 3.87 kB
JavaScript
import fs from 'fs';
import path from 'path';
import defaultDesktopSettings from 'lighthouse/core/config/lr-desktop-config.js';
import defaultMobileSettings from 'lighthouse/core/config/lr-mobile-config.js';
import { THRESHOLDS, SETTINGS } from '../constants.js';
export function getThresholds(thresholdsPath) {
let thresholdsData = {};
if (fs.existsSync(path.join(process.cwd(), thresholdsPath))) {
thresholdsData = JSON.parse(fs.readFileSync(path.join(process.cwd(), thresholdsPath), 'utf-8'));
}
return {
...THRESHOLDS,
...thresholdsData
};
}
export function getConfig(configPath, urls, mode, reportPath) {
let configData = {};
if (fs.existsSync(path.join(process.cwd(), configPath))) {
configData = JSON.parse(fs.readFileSync(path.join(process.cwd(), configPath), 'utf-8'));
}
const { settings = {} } = configData;
if (settings.device && !['desktop', 'mobile'].includes(settings.device.toLowerCase())) {
console.log('Invalid device specified. Falling back to the default device: mobile.');
}
const defaultSettings = settings?.device?.toLowerCase() === 'desktop' ? defaultDesktopSettings : defaultMobileSettings;
if (settings.throttling) {
SETTINGS.throttling = getThrottling(settings.throttling);
}
if (settings.cpuSlowdown) {
SETTINGS.throttling.cpuSlowdownMultiplier = getCpuSlowdown(settings.cpuSlowdown);
}
if (settings.viewport) {
const [width, height] = getViewport(settings.viewport);
SETTINGS.screenEmulation.width = width;
SETTINGS.screenEmulation.height = height;
}
if (fs.existsSync(path.join(process.cwd(), reportPath))) {
fs.rmSync(path.join(process.cwd(), reportPath));
}
return {
routes: urls.length ? urls : configData.routes,
reportPath,
settings: {
...defaultSettings,
settings: {
...defaultSettings.settings,
...SETTINGS,
throttlingMethod: mode === 'development' ? 'devtools' : 'simulate',
throttling: {
...defaultSettings.settings.throttling,
...SETTINGS.throttling,
},
screenEmulation: {
...defaultSettings.settings.screenEmulation,
...SETTINGS.screenEmulation,
}
},
}
};
}
function getThrottling(throttling) {
switch (throttling) {
case 0:
return {
rttMs: 0,
throughputKbps: 0,
cpuSlowdownMultiplier: 0,
requestLatencyMs: 0,
downloadThroughputKbps: 0,
uploadThroughputKbps: 0,
};
case 1:
return {
rttMs: 40,
throughputKbps: 10240,
cpuSlowdownMultiplier: 0,
requestLatencyMs: 0,
downloadThroughputKbps: 10240,
uploadThroughputKbps: 10240,
};
case 2:
return {
rttMs: 150,
throughputKbps: 1500,
cpuSlowdownMultiplier: 0,
requestLatencyMs: 150,
downloadThroughputKbps: 1500,
uploadThroughputKbps: 750,
};
default:
console.log('Invalid throttling specified. Falling back to the default throttling: no throttling.');
return SETTINGS.throttling;
}
}
function getCpuSlowdown(cpuSlowdown) {
switch (cpuSlowdown) {
case 0:
return 1;
case 2:
return 2;
case 4:
return 4;
default:
console.log('Invalid CPU slowdown specified. Falling back to the default CPU slowdown: no CPU slowdown.');
return SETTINGS.throttling.cpuSlowdownMultiplier;
}
}
function getViewport(viewport) {
if (!/^\d+[xX]\d+$/.test(viewport)) {
console.log(`The specified viewport size format is invalid. Falling back to the default value: ${
SETTINGS.screenEmulation.width
}x${
SETTINGS.screenEmulation.height
}.`);
return [SETTINGS.screenEmulation.width, SETTINGS.screenEmulation.height];
} else {
return viewport.toLowerCase().split('x').map(Number);
}
}