|
| 1 | +const { resolve } = require('path') |
| 2 | + |
| 3 | +const execa = require('execa') |
| 4 | +const chalk = require('chalk') |
| 5 | +const Conf = require('conf') // for simple kv store |
| 6 | + |
| 7 | +// // TODO: enable this in production |
| 8 | +// const NETLIFY_BUILD_BASE = '/opt/buildhome' |
| 9 | + |
| 10 | +module.exports = { |
| 11 | + name: '@netlify/plugin-lighthouse', |
| 12 | + // users will be tempted to use semver, but we really don't care |
| 13 | + postDeploy: async ({ pluginConfig }) => { |
| 14 | + let { site = process.env.SITE, currentVersion, compareWithVersion = 'init' } = pluginConfig |
| 15 | + |
| 16 | + if (typeof currentVersion === `undefined`) { |
| 17 | + console.log(`lighthouseplugin version not specified, auto assigning ${chalk.yellow("currentVersion='init'")}`) |
| 18 | + currentVersion = 'init' |
| 19 | + } |
| 20 | + |
| 21 | + // kvstore in `${NETLIFY_CACHE_DIR}/${name}.json` |
| 22 | + // we choose to let the user createStore instead of doing it for them |
| 23 | + // bc they may want to set `defaults` and `schema` and `de/serialize` |
| 24 | + const store = new Conf({ |
| 25 | + cwd: resolve('cache'), |
| 26 | + configName: 'netlify-plugin-lighthouse', |
| 27 | + }) |
| 28 | + |
| 29 | + // TODO: fetch previous scores from cache |
| 30 | + await execa('lighthouse-ci', site, { stdio: 'inherit' }) |
| 31 | + |
| 32 | + // serialize response |
| 33 | + const curLightHouse = {} |
| 34 | + const prevLightHouse = store.get(`lighthouse.${compareWithVersion}`) |
| 35 | + let totalImprovement = 0 |
| 36 | + if (prevLightHouse) { |
| 37 | + console.log( |
| 38 | + `Comparing lighthouse results from version: ${chalk.yellow(compareWithVersion)} to version: ${chalk.yellow( |
| 39 | + currentVersion, |
| 40 | + )}:`, |
| 41 | + ) |
| 42 | + Object.entries(curLightHouse).forEach(([k, v]) => { |
| 43 | + const prevV = prevLightHouse[k] |
| 44 | + if (!prevV) return // we should never get here but just in case lighthouse format changes... |
| 45 | + const improvement = v - prevV |
| 46 | + if (improvement < 0) { |
| 47 | + console.log(`- ${chalk.yellow(k)} ${chalk.magenta('regressed')} from ${prevV} to ${v}`) |
| 48 | + } else if (improvement > 0) { |
| 49 | + console.log(`- ${chalk.yellow(k)} ${chalk.green('improved')} from ${prevV} to ${v}`) |
| 50 | + } |
| 51 | + // TODO: print out links to give suggestions on what to do! lets see what lighthouse-ci gives us |
| 52 | + totalImprovement += improvement |
| 53 | + }) |
| 54 | + if (Math.abs(totalImprovement) > 2) { |
| 55 | + // some significance bar |
| 56 | + const color = totalImprovement > 0 ? chalk.green : chalk.magenta |
| 57 | + console.log(`${chalk.yellow.bold('Total Improvement')}: ${color(totalImprovement)} points!`) |
| 58 | + } |
| 59 | + } else { |
| 60 | + if (compareWithVersion) { |
| 61 | + console.warn( |
| 62 | + `Warning: you set ${chalk.yellow('compareWithVersion') + |
| 63 | + '=' + |
| 64 | + chalk.yellow(compareWithVersion)} but that version was not found in our result storage.`, |
| 65 | + ) |
| 66 | + } |
| 67 | + } |
| 68 | + store.set(`lighthouse.${currentVersion}`, curLightHouse) |
| 69 | + console.log(`Saved results as version: ${chalk.yellow(currentVersion)}`) |
| 70 | + }, |
| 71 | +} |
0 commit comments