import * as fs from 'fs' import ChainableWebpackConfig from 'webpack-chain' import SpeedMeasureWebpackPlugin from 'speed-measure-webpack-plugin' import { transformSMPoutput } from '../../utils/smp-output' /** smp 变异性能分析 * 由于使用 webpack-chain的缘故, 导致 无法使用 smp.wrap() 监听 plugin 配置失效. * 当前, 也有解决办法, 比如: 覆盖 config.toString 来达到写入 smp.wrap 的目的, 但 * 由于 覆盖后, 会导致 compilation.hooks.htmlWebpackPluginAlterAssetTags.tap 抛出空指针异常, 故暂时放弃 plugin性能分析能力. * 另外, 如果要单独针对某个插件耗时进行分析, 也可以通过 configureWebpack 加载插件进行测试. */ export const smp = (config: ChainableWebpackConfig) => { config.plugin('smp').use(SpeedMeasureWebpackPlugin, [ { granularLoaderData: true, outputFormat: (json) => { const logPath = `${process.cwd()}/logs` if (!fs.existsSync(logPath)) fs.mkdirSync(logPath) fs.writeFileSync(`${logPath}/smp.md`, transformSMPoutput(json, true), { encoding: 'utf-8' }) return transformSMPoutput(json) + `\n Tips: 详细数据请查看 [smp.json](./logs/smp.md)\n---\n` } } ]) }