From 0458e803909e780e4e0ffb087dc090f1431413f7 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Wed, 3 Oct 2018 22:53:05 +0200 Subject: [PATCH 1/6] add lighthouse-lr-background --- .../app/src/lighthouse-ext-background.js | 61 ------------ .../app/src/lighthouse-lr-background.js | 95 +++++++++++++++++++ lighthouse-extension/gulpfile.js | 20 ++-- 3 files changed, 107 insertions(+), 69 deletions(-) create mode 100644 lighthouse-extension/app/src/lighthouse-lr-background.js diff --git a/lighthouse-extension/app/src/lighthouse-ext-background.js b/lighthouse-extension/app/src/lighthouse-ext-background.js index 8556f3be299e..f4b81ca5199d 100644 --- a/lighthouse-extension/app/src/lighthouse-ext-background.js +++ b/lighthouse-extension/app/src/lighthouse-ext-background.js @@ -10,14 +10,6 @@ const background = require('./lighthouse-background'); const ExtensionProtocol = require('../../../lighthouse-core/gather/connections/extension'); const log = require('lighthouse-logger'); -const assetSaver = require('../../../lighthouse-core/lib/asset-saver.js'); -const LHError = require('../../../lighthouse-core/lib/lh-error.js'); - -/** @type {Record<'mobile'|'desktop', LH.Config.Json>} */ -const LR_PRESETS = { - mobile: require('../../../lighthouse-core/config/lr-mobile-config'), - desktop: require('../../../lighthouse-core/config/lr-desktop-config'), -}; /** @typedef {import('../../../lighthouse-core/gather/connections/connection.js')} Connection */ @@ -87,56 +79,6 @@ async function runLighthouseInExtension(flags, categoryIDs) { await new Promise(resolve => chrome.windows.create({url: blobURL}, resolve)); } -/** - * Run lighthouse for connection and provide similar results as in CLI. - * @param {Connection} connection - * @param {string} url - * @param {LH.Flags} flags Lighthouse flags, including `output` - * @param {{lrDevice?: 'desktop'|'mobile', categoryIDs?: Array, logAssets: boolean}} lrOpts Options coming from Lightrider - * @return {Promise|void>} - */ -async function runLighthouseInLR(connection, url, flags, {lrDevice, categoryIDs, logAssets}) { - // Certain fixes need to kick-in under LR, see https://github.com/GoogleChrome/lighthouse/issues/5839 - global.isLightRider = true; - - // disableStorageReset because it causes render server hang - flags.disableStorageReset = true; - flags.logLevel = flags.logLevel || 'info'; - const config = lrDevice === 'desktop' ? LR_PRESETS.desktop : LR_PRESETS.mobile; - if (categoryIDs) { - config.settings = config.settings || {}; - config.settings.onlyCategories = categoryIDs; - } - - try { - const results = await lighthouse(url, flags, config, connection); - if (!results) return; - - if (logAssets) { - await assetSaver.logAssets(results.artifacts, results.lhr.audits); - } - return results.report; - } catch (err) { - // If an error ruined the entire lighthouse run, attempt to return a meaningful error. - let runtimeError; - if (!(err instanceof LHError) || !err.lhrRuntimeError) { - runtimeError = { - code: LHError.UNKNOWN_ERROR, - message: `Unknown error encountered with message '${err.message}'`, - }; - } else { - runtimeError = { - code: err.code, - message: err.friendlyMessage ? - `${err.friendlyMessage} (${err.message})` : - err.message, - }; - } - - return JSON.stringify({runtimeError}, null, 2); - } -} - /** * @param {string} reportHtml * @return {string} Blob URL of the report (or error page) HTML @@ -241,7 +183,6 @@ if (typeof module !== 'undefined' && module.exports) { // Export for importing types into popup.js, require()ing into unit tests. module.exports = { runLighthouseInExtension, - runLighthouseInLR, getDefaultCategories: background.getDefaultCategories, isRunning, listenForStatus, @@ -255,8 +196,6 @@ if (typeof window !== 'undefined') { // @ts-ignore window.runLighthouseInExtension = runLighthouseInExtension; // @ts-ignore - window.runLighthouseInLR = runLighthouseInLR; - // @ts-ignore window.getDefaultCategories = background.getDefaultCategories; // @ts-ignore window.isRunning = isRunning; diff --git a/lighthouse-extension/app/src/lighthouse-lr-background.js b/lighthouse-extension/app/src/lighthouse-lr-background.js new file mode 100644 index 000000000000..664bafee678f --- /dev/null +++ b/lighthouse-extension/app/src/lighthouse-lr-background.js @@ -0,0 +1,95 @@ +/** + * @license Copyright 2018 Google Inc. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + */ +'use strict'; + +const lighthouse = require('../../../lighthouse-core/index'); +const background = require('./lighthouse-background'); + +const log = require('lighthouse-logger'); +const assetSaver = require('../../../lighthouse-core/lib/asset-saver.js'); +const LHError = require('../../../lighthouse-core/lib/lh-error.js'); + +/** @type {Record<'mobile'|'desktop', LH.Config.Json>} */ +const LR_PRESETS = { + mobile: require('../../../lighthouse-core/config/lr-mobile-config'), + desktop: require('../../../lighthouse-core/config/lr-desktop-config'), +}; + +/** @typedef {import('../../../lighthouse-core/gather/connections/connection.js')} Connection */ + +/** + * Run lighthouse for connection and provide similar results as in CLI. + * @param {Connection} connection + * @param {string} url + * @param {LH.Flags} flags Lighthouse flags, including `output` + * @param {{lrDevice?: 'desktop'|'mobile', categoryIDs?: Array, logAssets: boolean}} lrOpts Options coming from Lightrider + * @return {Promise|void>} + */ +async function runLighthouseInLR(connection, url, flags, {lrDevice, categoryIDs, logAssets}) { + // Certain fixes need to kick-in under LR, see https://github.com/GoogleChrome/lighthouse/issues/5839 + global.isLightRider = true; + + // disableStorageReset because it causes render server hang + flags.disableStorageReset = true; + flags.logLevel = flags.logLevel || 'info'; + const config = lrDevice === 'desktop' ? LR_PRESETS.desktop : LR_PRESETS.mobile; + if (categoryIDs) { + config.settings = config.settings || {}; + config.settings.onlyCategories = categoryIDs; + } + + try { + const results = await lighthouse(url, flags, config, connection); + if (!results) return; + + if (logAssets) { + await assetSaver.logAssets(results.artifacts, results.lhr.audits); + } + return results.report; + } catch (err) { + // If an error ruined the entire lighthouse run, attempt to return a meaningful error. + let runtimeError; + if (!(err instanceof LHError) || !err.lhrRuntimeError) { + runtimeError = { + code: LHError.UNKNOWN_ERROR, + message: `Unknown error encountered with message '${err.message}'`, + }; + } else { + runtimeError = { + code: err.code, + message: err.friendlyMessage ? + `${err.friendlyMessage} (${err.message})` : + err.message, + }; + } + + return JSON.stringify({runtimeError}, null, 2); + } +} + +/** @param {(status: [string, string, string]) => void} listenCallback */ +function listenForStatus(listenCallback) { + log.events.addListener('status', listenCallback); +} + +if (typeof module !== 'undefined' && module.exports) { + // Export for importing types into popup.js, require()ing into unit tests. + module.exports = { + getDefaultCategories: background.getDefaultCategories, + runLighthouseInLR, + listenForStatus, + }; +} + +// Expose on window for extension, other browser-residing consumers of file. +if (typeof window !== 'undefined') { + // @ts-ignore + window.runLighthouseInLR = runLighthouseInLR; + // @ts-ignore + window.getDefaultCategories = background.getDefaultCategories; + // @ts-ignore + window.listenForStatus = listenForStatus; +} diff --git a/lighthouse-extension/gulpfile.js b/lighthouse-extension/gulpfile.js index 3e71daf2edca..b106bad5ecab 100644 --- a/lighthouse-extension/gulpfile.js +++ b/lighthouse-extension/gulpfile.js @@ -34,6 +34,13 @@ const pkg = require('../package.json'); const distDir = 'dist'; +// list of all consumers we build for (easier to understand which file is used for which) +const CONSUMERS = { + DEVTOOLS: 'lighthouse-background.js', + EXTENSION: 'lighthouse-ext-background.js', + LIGHTRIDER: 'lighthouse-lr-background.js', +}; + const VERSION = pkg.version; const COMMIT_HASH = require('child_process') .execSync('git rev-parse HEAD') @@ -96,7 +103,7 @@ gulp.task('chromeManifest', () => { const manifestOpts = { buildnumber: false, background: { - target: 'scripts/lighthouse-ext-background.js', + target: `scripts/${CONSUMERS.EXTENSION}`, }, }; return gulp.src('app/manifest.json') @@ -114,10 +121,7 @@ function applyBrowserifyTransforms(bundle) { } gulp.task('browserify-lighthouse', () => { - return gulp.src([ - 'app/src/lighthouse-background.js', - 'app/src/lighthouse-ext-background.js', - ], {read: false}) + return gulp.src(Object.values(CONSUMERS).map(consumer => `app/src/${consumer}`), {read: false}) .pipe(tap(file => { let bundle = browserify(file.path); // , {debug: true}); // for sourcemaps bundle = applyBrowserifyTransforms(bundle); @@ -135,6 +139,8 @@ gulp.task('browserify-lighthouse', () => { bundle.ignore(require.resolve('../lighthouse-core/gather/connections/cri.js')); // Prevent the DevTools background script from getting the stringified HTML. + // eslint-disable-next-line + console.log(file.path); if (/lighthouse-background/.test(file.path)) { bundle.ignore(require.resolve('../lighthouse-core/report/html/html-report-assets.js')); } @@ -197,9 +203,7 @@ gulp.task('compilejs', () => { // sourceMaps: 'both' }; - return gulp.src([ - 'dist/scripts/lighthouse-background.js', - 'dist/scripts/lighthouse-ext-background.js']) + return gulp.src(Object.values(CONSUMERS).map(consumer => `dist/scripts/${consumer}`)) .pipe(tap(file => { const minified = babel.transform(file.contents.toString(), opts).code; file.contents = new Buffer(minified); From 350b3f109b232abf2a2cf68a808420c130a5650b Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Wed, 3 Oct 2018 23:40:09 +0200 Subject: [PATCH 2/6] ignore locales --- lighthouse-extension/gulpfile.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lighthouse-extension/gulpfile.js b/lighthouse-extension/gulpfile.js index b106bad5ecab..4a28dfa3157a 100644 --- a/lighthouse-extension/gulpfile.js +++ b/lighthouse-extension/gulpfile.js @@ -57,6 +57,13 @@ const gatherers = LighthouseRunner.getGathererList() const computedArtifacts = LighthouseRunner.getComputedGathererList() .map(f => '../lighthouse-core/gather/computed/' + f.replace(/\.js$/, '')); +const locales = fs.readdirSync('../lighthouse-core/lib/i18n/locales/') + .map(f => require.resolve(`../lighthouse-core/lib/i18n/locales/${f}`)); + +const isDevtools = (file) => new RegExp(`${CONSUMERS.DEVTOOLS}$`).test(file); +const isExtension = (file) => new RegExp(`${CONSUMERS.EXTENSION}$`).test(file); +// const isLightrider = (file) => new RegExp(`${CONSUMERS.LIGHTRIDER}$`).test(file); + gulp.task('extras', () => { return gulp.src([ 'app/*.*', @@ -139,12 +146,15 @@ gulp.task('browserify-lighthouse', () => { bundle.ignore(require.resolve('../lighthouse-core/gather/connections/cri.js')); // Prevent the DevTools background script from getting the stringified HTML. - // eslint-disable-next-line - console.log(file.path); - if (/lighthouse-background/.test(file.path)) { + if (isDevtools(file.path)) { bundle.ignore(require.resolve('../lighthouse-core/report/html/html-report-assets.js')); } + if (isDevtools(file.path) || isExtension(file.path)) { + // eslint-disable-next-line + bundle.ignore(locales); + } + // Expose the audits, gatherers, and computed artifacts so they can be dynamically loaded. const corePath = '../lighthouse-core/'; const driverPath = `${corePath}gather/`; From 89ab6a818e19608fb87baa0c5b1919bdff94c1dc Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Thu, 4 Oct 2018 06:49:08 +0200 Subject: [PATCH 3/6] review --- .../app/src/lighthouse-lr-background.js | 13 ------------- lighthouse-extension/gulpfile.js | 12 ++++++------ 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/lighthouse-extension/app/src/lighthouse-lr-background.js b/lighthouse-extension/app/src/lighthouse-lr-background.js index 664bafee678f..426d424280c0 100644 --- a/lighthouse-extension/app/src/lighthouse-lr-background.js +++ b/lighthouse-extension/app/src/lighthouse-lr-background.js @@ -6,9 +6,7 @@ 'use strict'; const lighthouse = require('../../../lighthouse-core/index'); -const background = require('./lighthouse-background'); -const log = require('lighthouse-logger'); const assetSaver = require('../../../lighthouse-core/lib/asset-saver.js'); const LHError = require('../../../lighthouse-core/lib/lh-error.js'); @@ -70,17 +68,10 @@ async function runLighthouseInLR(connection, url, flags, {lrDevice, categoryIDs, } } -/** @param {(status: [string, string, string]) => void} listenCallback */ -function listenForStatus(listenCallback) { - log.events.addListener('status', listenCallback); -} - if (typeof module !== 'undefined' && module.exports) { // Export for importing types into popup.js, require()ing into unit tests. module.exports = { - getDefaultCategories: background.getDefaultCategories, runLighthouseInLR, - listenForStatus, }; } @@ -88,8 +79,4 @@ if (typeof module !== 'undefined' && module.exports) { if (typeof window !== 'undefined') { // @ts-ignore window.runLighthouseInLR = runLighthouseInLR; - // @ts-ignore - window.getDefaultCategories = background.getDefaultCategories; - // @ts-ignore - window.listenForStatus = listenForStatus; } diff --git a/lighthouse-extension/gulpfile.js b/lighthouse-extension/gulpfile.js index 4a28dfa3157a..8bb01e10462a 100644 --- a/lighthouse-extension/gulpfile.js +++ b/lighthouse-extension/gulpfile.js @@ -60,9 +60,8 @@ const computedArtifacts = LighthouseRunner.getComputedGathererList() const locales = fs.readdirSync('../lighthouse-core/lib/i18n/locales/') .map(f => require.resolve(`../lighthouse-core/lib/i18n/locales/${f}`)); -const isDevtools = (file) => new RegExp(`${CONSUMERS.DEVTOOLS}$`).test(file); -const isExtension = (file) => new RegExp(`${CONSUMERS.EXTENSION}$`).test(file); -// const isLightrider = (file) => new RegExp(`${CONSUMERS.LIGHTRIDER}$`).test(file); +const isDevtools = file => file.endsWith(CONSUMERS.DEVTOOLS); +const isExtension = file => file.endsWith(CONSUMERS.EXTENSION); gulp.task('extras', () => { return gulp.src([ @@ -128,7 +127,8 @@ function applyBrowserifyTransforms(bundle) { } gulp.task('browserify-lighthouse', () => { - return gulp.src(Object.values(CONSUMERS).map(consumer => `app/src/${consumer}`), {read: false}) + const consumerSources = Object.values(CONSUMERS).map(consumer => `app/src/${consumer}`); + return gulp.src(consumerSources, {read: false}) .pipe(tap(file => { let bundle = browserify(file.path); // , {debug: true}); // for sourcemaps bundle = applyBrowserifyTransforms(bundle); @@ -151,7 +151,6 @@ gulp.task('browserify-lighthouse', () => { } if (isDevtools(file.path) || isExtension(file.path)) { - // eslint-disable-next-line bundle.ignore(locales); } @@ -213,7 +212,8 @@ gulp.task('compilejs', () => { // sourceMaps: 'both' }; - return gulp.src(Object.values(CONSUMERS).map(consumer => `dist/scripts/${consumer}`)) + const compiledSources = Object.values(CONSUMERS).map(consumer => `dist/scripts/${consumer}`); + return gulp.src(compiledSources) .pipe(tap(file => { const minified = babel.transform(file.contents.toString(), opts).code; file.contents = new Buffer(minified); From ef3609031eacd4735c61a2264780059b92d866e1 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Thu, 4 Oct 2018 07:18:24 +0200 Subject: [PATCH 4/6] fix tests --- lighthouse-extension/gulpfile.js | 2 +- .../test/app/src/lighthouse-ext-background-test.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lighthouse-extension/gulpfile.js b/lighthouse-extension/gulpfile.js index 8bb01e10462a..de0249793c11 100644 --- a/lighthouse-extension/gulpfile.js +++ b/lighthouse-extension/gulpfile.js @@ -130,7 +130,7 @@ gulp.task('browserify-lighthouse', () => { const consumerSources = Object.values(CONSUMERS).map(consumer => `app/src/${consumer}`); return gulp.src(consumerSources, {read: false}) .pipe(tap(file => { - let bundle = browserify(file.path); // , {debug: true}); // for sourcemaps + let bundle = browserify(file.path, {debug: true}); // for sourcemaps bundle = applyBrowserifyTransforms(bundle); // scripts will need some additional transforms, ignores and requires… diff --git a/lighthouse-extension/test/app/src/lighthouse-ext-background-test.js b/lighthouse-extension/test/app/src/lighthouse-ext-background-test.js index c66858d8e6a9..32bf39dcc9fd 100644 --- a/lighthouse-extension/test/app/src/lighthouse-ext-background-test.js +++ b/lighthouse-extension/test/app/src/lighthouse-ext-background-test.js @@ -6,12 +6,12 @@ 'use strict'; const assert = require('assert'); -const lhBackground = require('../../../app/src/lighthouse-ext-background.js'); +const lhBackground = require('../../../app/src/lighthouse-lr-background.js'); const LHError = require('../../../../lighthouse-core/lib/lh-error.js'); /* eslint-env mocha */ -describe('lighthouse-ext-background', () => { +describe('lighthouse-lr-background', () => { describe('#runLighthouseInLR', () => { it('returns a runtimeError LHR when lighthouse throws a runtimeError', async () => { const connectionError = new LHError(LHError.errors.FAILED_DOCUMENT_REQUEST); @@ -36,7 +36,7 @@ describe('lighthouse-ext-background', () => { it('returns an unknown-runtimeError LHR when lighthouse throws an unknown error', async () => { const errorMsg = 'Errors are the best!'; const connectionError = new Error(errorMsg); - assert.strictEqual(connectionError.lhrRuntimeError, undefined); + assert.strictEqual(connectionError.message, undefined); const mockConnection = { async connect() { throw connectionError; From d562aad34b2aecb46d1c77ef97a877e171bec316 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Thu, 4 Oct 2018 07:20:00 +0200 Subject: [PATCH 5/6] revert bad test change --- .../test/app/src/lighthouse-ext-background-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lighthouse-extension/test/app/src/lighthouse-ext-background-test.js b/lighthouse-extension/test/app/src/lighthouse-ext-background-test.js index 32bf39dcc9fd..757224c31305 100644 --- a/lighthouse-extension/test/app/src/lighthouse-ext-background-test.js +++ b/lighthouse-extension/test/app/src/lighthouse-ext-background-test.js @@ -36,7 +36,7 @@ describe('lighthouse-lr-background', () => { it('returns an unknown-runtimeError LHR when lighthouse throws an unknown error', async () => { const errorMsg = 'Errors are the best!'; const connectionError = new Error(errorMsg); - assert.strictEqual(connectionError.message, undefined); + assert.strictEqual(connectionError.lhrRuntimeError, undefined); const mockConnection = { async connect() { throw connectionError; From a8c063afe37cc479b4c62ca76084cd7861ed60f6 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Thu, 4 Oct 2018 07:43:44 +0200 Subject: [PATCH 6/6] move ext-test to lr-test --- ...se-ext-background-test.js => lighthouse-lr-background-test.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lighthouse-extension/test/app/src/{lighthouse-ext-background-test.js => lighthouse-lr-background-test.js} (100%) diff --git a/lighthouse-extension/test/app/src/lighthouse-ext-background-test.js b/lighthouse-extension/test/app/src/lighthouse-lr-background-test.js similarity index 100% rename from lighthouse-extension/test/app/src/lighthouse-ext-background-test.js rename to lighthouse-extension/test/app/src/lighthouse-lr-background-test.js