perf-audit-cli
Version: 
CLI tool for continuous performance monitoring and analysis
168 lines (163 loc) • 5.81 kB
JavaScript
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
    if (typeof path === "string" && /^\.\.?\//.test(path)) {
        return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
            return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
        });
    }
    return path;
};
import fs from 'fs';
import path from 'path';
import { DEFAULT_CLIENT_BUDGETS, DEFAULT_CLIENT_OUTPUT_PATH, DEFAULT_CONFIG_FILE, DEFAULT_IGNORE_PATHS, DEFAULT_LIGHTHOUSE_SCORES, DEFAULT_METRICS, DEFAULT_REPORTS_OUTPUT_DIR, DEFAULT_SERVER_BUDGETS, DEFAULT_SERVER_OUTPUT_PATH, } from "../constants/index.js";
import { Logger } from "./logger.js";
const DEFAULT_CONFIG = {
    project: {
        client: {
            outputPath: DEFAULT_CLIENT_OUTPUT_PATH,
        },
        server: {
            outputPath: DEFAULT_SERVER_OUTPUT_PATH,
        },
    },
    budgets: {
        client: {
            bundles: {
                main: DEFAULT_CLIENT_BUDGETS.MAIN,
                vendor: DEFAULT_CLIENT_BUDGETS.VENDOR,
                total: DEFAULT_CLIENT_BUDGETS.TOTAL,
            },
        },
        server: {
            bundles: {
                main: DEFAULT_SERVER_BUDGETS.MAIN,
                vendor: DEFAULT_SERVER_BUDGETS.VENDOR,
                total: DEFAULT_SERVER_BUDGETS.TOTAL,
            },
        },
        lighthouse: {
            performance: DEFAULT_LIGHTHOUSE_SCORES.PERFORMANCE,
            accessibility: DEFAULT_LIGHTHOUSE_SCORES.ACCESSIBILITY,
            bestPractices: DEFAULT_LIGHTHOUSE_SCORES.BEST_PRACTICES,
            seo: DEFAULT_LIGHTHOUSE_SCORES.SEO,
        },
        metrics: {
            fcp: DEFAULT_METRICS.FCP,
            lcp: DEFAULT_METRICS.LCP,
            cls: DEFAULT_METRICS.CLS,
            tti: DEFAULT_METRICS.TTI,
        },
    },
    analysis: {
        target: 'both',
        gzip: true,
        ignorePaths: [...DEFAULT_IGNORE_PATHS],
    },
    reports: {
        formats: ['console', 'json', 'html'],
        outputDir: DEFAULT_REPORTS_OUTPUT_DIR,
    },
};
export async function loadConfig(configPath) {
    const configFilePath = process.env.PERF_AUDIT_CONFIG_FILE ?? DEFAULT_CONFIG_FILE;
    const defaultConfigPath = path.join(process.cwd(), configFilePath);
    const finalConfigPath = configPath ?? defaultConfigPath;
    try {
        if (fs.existsSync(finalConfigPath)) {
            const configModule = await import(__rewriteRelativeImportExtension(path.resolve(finalConfigPath)));
            const userConfig = configModule.default || configModule;
            return mergeConfig(DEFAULT_CONFIG, userConfig);
        }
    }
    catch {
        Logger.warn(`Failed to load config file: ${finalConfigPath}`);
        Logger.warn('Using default configuration');
    }
    return DEFAULT_CONFIG;
}
function mergeConfig(defaultConfig, userConfig) {
    const mergeLighthouseBudget = () => {
        if (defaultConfig.budgets?.lighthouse !== undefined)
            return defaultConfig.budgets.lighthouse;
        if (userConfig.budgets?.lighthouse !== undefined)
            return userConfig.budgets.lighthouse;
        return undefined;
    };
    return {
        project: { ...defaultConfig.project, ...userConfig.project },
        budgets: {
            client: {
                bundles: { ...defaultConfig.budgets.client.bundles, ...userConfig.budgets?.client?.bundles },
            },
            server: {
                bundles: { ...defaultConfig.budgets.server.bundles, ...userConfig.budgets?.server?.bundles },
            },
            lighthouse: mergeLighthouseBudget(),
            metrics: { ...defaultConfig.budgets.metrics, ...userConfig.budgets?.metrics },
        },
        analysis: { ...defaultConfig.analysis, ...userConfig.analysis },
        reports: { ...defaultConfig.reports, ...userConfig.reports },
    };
}
export function generateConfigFile(outputPath = DEFAULT_CONFIG_FILE) {
    const configContent = `module.exports = {
  // プロジェクト設定
  project: {
    // クライアントサイドの設定
    client: {
      outputPath: './dist/client',
    },
    // サーバーサイドの設定(SSR対応)
    server: {
      outputPath: './dist/server',
    },
  },
  // パフォーマンスバジェット
  budgets: {
    // クライアントサイドバジェット
    client: {
      bundles: {
        main: { max: '150KB', warning: '120KB' },
        vendor: { max: '100KB', warning: '80KB' },
        total: { max: '500KB', warning: '400KB' },
      },
    },
    // サーバーサイドバジェット
    server: {
      bundles: {
        main: { max: '200KB', warning: '150KB' },
        vendor: { max: '150KB', warning: '120KB' },
        total: { max: '800KB', warning: '600KB' },
      },
    },
    // メトリクス設定(クライアントサイドのみ)
    metrics: {
      fcp: { max: 1500, warning: 1000 },
      lcp: { max: 2500, warning: 2000 },
      cls: { max: 0.1, warning: 0.05 },
      tti: { max: 3500, warning: 3000 },
    },
  },
  // 分析設定
  analysis: {
    // 解析対象の選択: 'client', 'server', 'both'
    target: 'both',
    gzip: true,
    ignorePaths: ['**/*.test.js', '**/*.spec.js'],
  },
  // レポート設定
  reports: {
    formats: ['console', 'json', 'html'],
    outputDir: './performance-reports',
  },
  // プラグイン設定
  plugins: [
    { name: 'bundle-analyzer', enabled: true },
    { name: 'performance-tracker', enabled: true },
    { name: 'ci-reporter', enabled: true }
  ]
}
`;
    fs.writeFileSync(outputPath, configContent);
    Logger.success(`Configuration file created: ${outputPath}`);
}
//# sourceMappingURL=config.js.map