Skip to content

Commit

Permalink
extension: access core through module instead of Runner (#5855)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny authored Aug 17, 2018
1 parent dee7411 commit d103157
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 41 deletions.
25 changes: 15 additions & 10 deletions lighthouse-core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const log = require('lighthouse-logger');
const ChromeProtocol = require('./gather/connections/cri.js');
const Config = require('./config/config');

/** @typedef {import('./gather/connections/connection.js')} Connection */

/*
* The relationship between these root modules:
*
Expand All @@ -18,27 +20,30 @@ const Config = require('./config/config');
* runner.js - marshalls the actions that must be taken (Gather / Audit)
* config file is used to determine which of these actions are needed
*
* lighthouse-cli \
* -- index.js \
* ----- runner.js ----> [Gather / Audit]
* lighthouse-extension /
*
* lighthouse-cli \
* -- core/index.js ----> runner.js ----> [Gather / Audit]
* lighthouse-extension /
*/

/**
* @param {string=} url
* @param {LH.Flags=} flags
* @param {LH.Config.Json=} configJSON
* Run Lighthouse.
* @param {string=} url The URL to test. Optional if running in auditMode.
* @param {LH.Flags=} flags Optional settings for the Lighthouse run. If present,
* they will override any settings in the config.
* @param {LH.Config.Json=} configJSON Configuration for the Lighthouse run. If
* not present, the default config is used.
* @param {Connection=} connection
* @return {Promise<LH.RunnerResult|undefined>}
*/
async function lighthouse(url, flags = {}, configJSON) {
async function lighthouse(url, flags = {}, configJSON, connection) {
// set logging preferences, assume quiet
flags.logLevel = flags.logLevel || 'error';
log.setLevel(flags.logLevel);

// Use ConfigParser to generate a valid config file
const config = new Config(configJSON, flags);
const connection = new ChromeProtocol(flags.port, flags.hostname);

connection = connection || new ChromeProtocol(flags.port, flags.hostname);

// kick off a lighthouse run
return Runner.run(connection, {url, config});
Expand Down
38 changes: 13 additions & 25 deletions lighthouse-extension/app/src/lighthouse-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/
'use strict';

const lighthouse = require('../../../lighthouse-core/index');
const RawProtocol = require('../../../lighthouse-core/gather/connections/raw');
const Runner = require('../../../lighthouse-core/runner');
const Config = require('../../../lighthouse-core/config/config');
const defaultConfig = require('../../../lighthouse-core/config/default-config.js');
const i18n = require('../../../lighthouse-core/lib/i18n');
Expand All @@ -15,32 +15,18 @@ const log = require('lighthouse-logger');
/** @typedef {import('../../../lighthouse-core/gather/connections/connection.js')} Connection */

/**
* @param {Connection} connection
* @param {string} url
* @param {LH.Flags} flags Lighthouse flags.
* @param {Array<string>} categoryIDs Name values of categories to include.
* @param {(url?: string) => void} updateBadgeFn
* @return {Promise<LH.RunnerResult|void>}
* Return a version of the default config, filtered to only run the specified
* categories.
* @param {Array<string>} categoryIDs
* @return {LH.Config.Json}
*/
function runLighthouseForConnection(connection, url, flags, categoryIDs, updateBadgeFn = _ => {}) {
const config = new Config({
function getDefaultConfigForCategories(categoryIDs) {
return {
extends: 'lighthouse:default',
settings: {
onlyCategories: categoryIDs,
},
}, flags);

updateBadgeFn(url);

return Runner.run(connection, {url, config}) // Run Lighthouse.
.then(result => {
updateBadgeFn();
return result;
})
.catch(err => {
updateBadgeFn();
throw err;
});
};
}

/**
Expand All @@ -52,9 +38,11 @@ function runLighthouseForConnection(connection, url, flags, categoryIDs, updateB
*/
function runLighthouseInWorker(port, url, flags, categoryIDs) {
// Default to 'info' logging level.
log.setLevel('info');
flags.logLevel = flags.logLevel || 'info';
const config = getDefaultConfigForCategories(categoryIDs);
const connection = new RawProtocol(port);
return runLighthouseForConnection(connection, url, flags, categoryIDs);

return lighthouse(url, flags, config, connection);
}

/**
Expand All @@ -75,7 +63,7 @@ function listenForStatus(listenCallback) {
if (typeof module !== 'undefined' && module.exports) {
// export for lighthouse-ext-background to require (via browserify).
module.exports = {
runLighthouseForConnection,
getDefaultConfigForCategories,
runLighthouseInWorker,
getDefaultCategories,
listenForStatus,
Expand Down
22 changes: 16 additions & 6 deletions lighthouse-extension/app/src/lighthouse-ext-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
'use strict';

const lighthouse = require('../../../lighthouse-core/index');
const background = require('./lighthouse-background');

const ExtensionProtocol = require('../../../lighthouse-core/gather/connections/extension');
Expand Down Expand Up @@ -53,13 +54,21 @@ function updateBadgeUI(optUrl) {
*/
async function runLighthouseInExtension(flags, categoryIDs) {
// Default to 'info' logging level.
log.setLevel('info');
const connection = new ExtensionProtocol();
flags.logLevel = flags.logLevel || 'info';
flags.output = 'html';

const connection = new ExtensionProtocol();
const url = await connection.getCurrentTabURL();
const runnerResult = await background.runLighthouseForConnection(connection, url, flags,
categoryIDs, updateBadgeUI);
const config = background.getDefaultConfigForCategories(categoryIDs);

updateBadgeUI(url);
let runnerResult;
try {
runnerResult = await lighthouse(url, flags, config, connection);
} finally {
updateBadgeUI();
}

if (!runnerResult) {
// For now, should always be a runnerResult as the extension can't do `gatherMode`
throw new Error('no runnerResult generated by Lighthouse');
Expand All @@ -81,8 +90,9 @@ async function runLighthouseInExtension(flags, categoryIDs) {
* @return {Promise<string|Array<string>|void>}
*/
async function runLighthouseAsInCLI(connection, url, flags, categoryIDs, {logAssets}) {
log.setLevel('info');
const results = await background.runLighthouseForConnection(connection, url, flags, categoryIDs);
flags.logLevel = flags.logLevel || 'info';
const config = background.getDefaultConfigForCategories(categoryIDs);
const results = await lighthouse(url, flags, config, connection);
if (!results) return;

if (logAssets) {
Expand Down
3 changes: 3 additions & 0 deletions lighthouse-extension/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ gulp.task('browserify-lighthouse', () => {
.ignore('rimraf')
.ignore('pako/lib/zlib/inflate.js');

// Don't include the desktop protocol connection.
bundle.ignore(require.resolve('../lighthouse-core/gather/connections/cri.js'));

// Prevent the DevTools background script from getting the stringified HTML.
if (/lighthouse-background/.test(file.path)) {
bundle.ignore(require.resolve('../lighthouse-core/report/html/html-report-assets.js'));
Expand Down

0 comments on commit d103157

Please sign in to comment.