Skip to content

Commit

Permalink
extension(build): ignore all locales from devtools & extension build (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet authored and paulirish committed Oct 4, 2018
1 parent 71658c6 commit 1ce80f4
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 73 deletions.
61 changes: 0 additions & 61 deletions lighthouse-extension/app/src/lighthouse-ext-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down Expand Up @@ -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<string>, logAssets: boolean}} lrOpts Options coming from Lightrider
* @return {Promise<string|Array<string>|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
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down
82 changes: 82 additions & 0 deletions lighthouse-extension/app/src/lighthouse-lr-background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* @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 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<string>, logAssets: boolean}} lrOpts Options coming from Lightrider
* @return {Promise<string|Array<string>|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);
}
}

if (typeof module !== 'undefined' && module.exports) {
// Export for importing types into popup.js, require()ing into unit tests.
module.exports = {
runLighthouseInLR,
};
}

// Expose on window for extension, other browser-residing consumers of file.
if (typeof window !== 'undefined') {
// @ts-ignore
window.runLighthouseInLR = runLighthouseInLR;
}
34 changes: 24 additions & 10 deletions lighthouse-extension/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -50,6 +57,12 @@ 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 => file.endsWith(CONSUMERS.DEVTOOLS);
const isExtension = file => file.endsWith(CONSUMERS.EXTENSION);

gulp.task('extras', () => {
return gulp.src([
'app/*.*',
Expand Down Expand Up @@ -96,7 +109,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')
Expand All @@ -114,12 +127,10 @@ function applyBrowserifyTransforms(bundle) {
}

gulp.task('browserify-lighthouse', () => {
return gulp.src([
'app/src/lighthouse-background.js',
'app/src/lighthouse-ext-background.js',
], {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
let bundle = browserify(file.path, {debug: true}); // for sourcemaps
bundle = applyBrowserifyTransforms(bundle);

// scripts will need some additional transforms, ignores and requires…
Expand All @@ -135,10 +146,14 @@ gulp.task('browserify-lighthouse', () => {
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)) {
if (isDevtools(file.path)) {
bundle.ignore(require.resolve('../lighthouse-core/report/html/html-report-assets.js'));
}

if (isDevtools(file.path) || isExtension(file.path)) {
bundle.ignore(locales);
}

// Expose the audits, gatherers, and computed artifacts so they can be dynamically loaded.
const corePath = '../lighthouse-core/';
const driverPath = `${corePath}gather/`;
Expand Down Expand Up @@ -197,9 +212,8 @@ gulp.task('compilejs', () => {
// sourceMaps: 'both'
};

return gulp.src([
'dist/scripts/lighthouse-background.js',
'dist/scripts/lighthouse-ext-background.js'])
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 1ce80f4

Please sign in to comment.