import { resolve as pathResolve } from 'path'; import * as fs from 'fs'; import { logger } from '@spine/logger'; import { cliAnalyzeHook } from '../hooks'; import { getBuildPath } from '@erect/core/paths'; import { config } from '@erect/core/config'; import { webpackConfigFactory } from '../webpack/configFactory'; import { createBuildOptions } from '../webpack/createBuildOptions'; import { exec } from '../utils'; export const log = logger('erect:analyze'); cliAnalyzeHook.addAction('default', () => { const anaylzeFilePath = pathResolve( getBuildPath(), config.bundles.client.buildOutputDir, '__analyze__.json', ); const webpackFactory = webpackConfigFactory( createBuildOptions({ target: 'client', optimize: true, }), log, ); const clientCompiler = webpackFactory.compile(); clientCompiler.run((err, stats) => { if (err != null) { console.error(err); } else { // Write out the json stats file. fs.writeFileSync( anaylzeFilePath, JSON.stringify(stats.toJson('verbose'), null, 4) ); // Run the bundle analyzer against the stats file. const cmd = `webpack-bundle-analyzer ${anaylzeFilePath} ${config.bundles.client.buildOutputDir}`; exec(cmd); } }); }, { priority: 50 });