Skip to content
This repository was archived by the owner on Sep 30, 2020. It is now read-only.

feat(scripts): add "lighthouse" subcommand for gathering perf metrics #396

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/liferay-npm-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"babel-jest": "^25.1.0",
"babel-loader": "8.0.6",
"babel-plugin-transform-react-remove-prop-types": "0.4.24",
"chrome-launcher": "0.12.0",
"cosmiconfig": "^6.0.0",
"cross-env": "^7.0.0",
"cross-spawn": "7.0.1",
Expand All @@ -55,6 +56,7 @@
"liferay-npm-bundler": "2.18.2",
"liferay-npm-bundler-preset-liferay-dev": "4.2.5",
"liferay-theme-tasks": "9.4.3",
"lighthouse": "5.6.0",
"metal-tools-soy": "4.3.2",
"minimist": "^1.2.0",
"prettier": "1.19.1",
Expand Down
4 changes: 4 additions & 0 deletions packages/liferay-npm-scripts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ module.exports = async function() {
require('./scripts/format')({check: true});
},

async lighthouse() {
await require('./scripts/lighthouse')();
},

async lint() {
await require('./scripts/lint')();
},
Expand Down
51 changes: 51 additions & 0 deletions packages/liferay-npm-scripts/src/scripts/lighthouse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* SPDX-FileCopyrightText: © 2019 Liferay, Inc. <https://liferay.com>
* SPDX-License-Identifier: BSD-3-Clause
*/

const launcher = require('chrome-launcher');
const lh = require('lighthouse');
const path = require('path');

/**
* Wrapper for the Lighthouse performance auditing command-line tool.
*
* @see https://developers.google.com/web/tools/lighthouse
*/
async function lighthouse() {
const chromeFlags = ['--headless'];
const chromePath = process.env.CHROME_PATH
? path.resolve(process.env.CHROME_PATH)
: undefined;

const logLevel = process.env.DEBUG ? 'verbose' : 'error';

const launcherOptions = {
chromeFlags,
chromePath,
logLevel
};

const chrome = await launcher.launch(launcherOptions);

const url = process.env.PORTAL_URL || 'http://localhost:8080/';

const lighthouseOptions = {
logLevel,
onlyCategories: ['performance'],
port: chrome.port
};

const config = undefined;

const results = await lh(url, lighthouseOptions, config);

await chrome.kill();

/* eslint-disable no-console */
console.log(results.lhr); // "JS-consumable output"
console.log(results.report); // "HTML/JSON/CSV as a string"
console.log(results.artifacts); // "trace/screenshots/other"
}

module.exports = lighthouse;
Loading