|
1 | | -const { resolve } = require('path') |
| 1 | +const httpServer = require("http-server"); |
| 2 | +const puppeteer = require("puppeteer"); |
| 3 | +const lighthouse = require("lighthouse"); |
| 4 | +const chromeLauncher = require("chrome-launcher"); |
| 5 | +require("dotenv").config(); |
2 | 6 |
|
3 | | -const execa = require('execa') |
4 | | -const chalk = require('chalk') |
5 | | -const Conf = require('conf') // for simple kv store |
| 7 | +const getServer = (url, serveDir) => { |
| 8 | + if (url) { |
| 9 | + console.log(`Scanning url '${url}'`); |
| 10 | + // return a mock server for readability |
| 11 | + const server = { |
| 12 | + listen: async (func) => { |
| 13 | + await func(); |
| 14 | + }, |
| 15 | + close: () => undefined, |
| 16 | + }; |
| 17 | + return { server, url }; |
| 18 | + } |
6 | 19 |
|
7 | | -// // TODO: enable this in production |
8 | | -// const NETLIFY_BUILD_BASE = '/opt/buildhome' |
| 20 | + if (!serveDir) { |
| 21 | + throw new Error("Empty publish dir"); |
| 22 | + } |
| 23 | + |
| 24 | + console.log(`Serving and scanning site from directory '${serveDir}'`); |
| 25 | + const s = httpServer.createServer({ root: serveDir }); |
| 26 | + const port = 5000; |
| 27 | + const host = "localhost"; |
| 28 | + const server = { |
| 29 | + listen: (func) => s.listen(port, host, func), |
| 30 | + close: () => s.close(), |
| 31 | + }; |
| 32 | + return { url: `http://${host}:${port}`, server }; |
| 33 | +}; |
| 34 | + |
| 35 | +const belowThreshold = (id, expected, results) => { |
| 36 | + const category = results.find((c) => c.id === id); |
| 37 | + if (!category) { |
| 38 | + console.warn("Could not find category", id); |
| 39 | + } |
| 40 | + const actual = category ? category.score : Number.MAX_SAFE_INTEGER; |
| 41 | + return actual < expected; |
| 42 | +}; |
| 43 | + |
| 44 | +const getError = (id, expected, results) => { |
| 45 | + const category = results.find((c) => c.id === id); |
| 46 | + return `Expected category '${category.title}' to be greater or equal to '${expected}' but got '${category.score}'`; |
| 47 | +}; |
9 | 48 |
|
10 | 49 | module.exports = { |
11 | | - name: '@netlify/plugin-lighthouse', |
12 | | - // users will be tempted to use semver, but we really don't care |
13 | | - onPostDeploy: async ({ pluginConfig }) => { |
14 | | - let { site = process.env.SITE, currentVersion, compareWithVersion = 'init' } = pluginConfig |
| 50 | + onSuccess: async ({ |
| 51 | + constants: { PUBLISH_DIR: serveDir = process.env.PUBLISH_DIR } = {}, |
| 52 | + utils, |
| 53 | + inputs: { |
| 54 | + audit_url: auditUrl = process.env.AUDIT_URL, |
| 55 | + thresholds = process.env.THRESHOLDS || {}, |
| 56 | + } = {}, |
| 57 | + } = {}) => { |
| 58 | + try { |
| 59 | + utils = utils || { |
| 60 | + build: { |
| 61 | + failBuild: () => { |
| 62 | + process.exit(1); |
| 63 | + }, |
| 64 | + }, |
| 65 | + status: { |
| 66 | + show: () => undefined, |
| 67 | + }, |
| 68 | + }; |
15 | 69 |
|
16 | | - if (typeof currentVersion === `undefined`) { |
17 | | - console.log(`lighthouseplugin version not specified, auto assigning ${chalk.yellow("currentVersion='init'")}`) |
18 | | - currentVersion = 'init' |
19 | | - } |
| 70 | + if (typeof thresholds === "string") { |
| 71 | + thresholds = JSON.parse(thresholds); |
| 72 | + } |
20 | 73 |
|
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 | | - }) |
| 74 | + const { server, url } = getServer(auditUrl, serveDir); |
| 75 | + const browserFetcher = puppeteer.createBrowserFetcher(); |
| 76 | + const revisions = await browserFetcher.localRevisions(); |
| 77 | + if (revisions.length <= 0) { |
| 78 | + throw new Error("Could not find local browser"); |
| 79 | + } |
| 80 | + const info = await browserFetcher.revisionInfo(revisions[0]); |
| 81 | + |
| 82 | + const { error, results } = await new Promise((resolve) => { |
| 83 | + server.listen(async () => { |
| 84 | + let chrome; |
| 85 | + try { |
| 86 | + chrome = await chromeLauncher.launch({ |
| 87 | + chromePath: info.executablePath, |
| 88 | + chromeFlags: ["--headless", "--no-sandbox", "--disable-gpu"], |
| 89 | + }); |
| 90 | + const results = await lighthouse(url, { |
| 91 | + port: chrome.port, |
| 92 | + }); |
| 93 | + if (results.lhr.runtimeError) { |
| 94 | + resolve({ error: new Error(results.lhr.runtimeError.message) }); |
| 95 | + } |
| 96 | + resolve({ error: false, results }); |
| 97 | + } catch (error) { |
| 98 | + resolve({ error }); |
| 99 | + } finally { |
| 100 | + if (chrome) { |
| 101 | + await chrome.kill().catch(() => undefined); |
| 102 | + } |
| 103 | + server.close(); |
| 104 | + } |
| 105 | + }); |
| 106 | + }); |
| 107 | + if (error) { |
| 108 | + throw error; |
| 109 | + } else { |
| 110 | + const categories = Object.values( |
| 111 | + results.lhr.categories |
| 112 | + ).map(({ title, score, id }) => ({ title, score, id })); |
28 | 113 |
|
29 | | - // TODO: fetch previous scores from cache |
30 | | - await execa('lighthouse-ci', site, { stdio: 'inherit' }) |
| 114 | + const errors = Object.entries(thresholds) |
| 115 | + .filter(([id, expected]) => belowThreshold(id, expected, categories)) |
| 116 | + .map(([id, expected]) => getError(id, expected, categories)); |
31 | 117 |
|
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}`) |
| 118 | + const summary = JSON.stringify({ results: categories }, null, 2); |
| 119 | + console.log(summary); |
| 120 | + utils.status.show({ |
| 121 | + summary, |
| 122 | + }); |
| 123 | + |
| 124 | + if (errors.length > 0) { |
| 125 | + throw new Error(errors.join("\n")); |
50 | 126 | } |
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 | 127 | } |
| 128 | + } catch (error) { |
| 129 | + console.error(`\nError: ${error.message}\n`); |
| 130 | + utils.build.failBuild(`failed with error: ${error.message}`); |
67 | 131 | } |
68 | | - store.set(`lighthouse.${currentVersion}`, curLightHouse) |
69 | | - console.log(`Saved results as version: ${chalk.yellow(currentVersion)}`) |
70 | 132 | }, |
71 | | -} |
| 133 | +}; |
0 commit comments