Skip to content

Commit

Permalink
build config based on tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed Jan 12, 2017
1 parent a13fa24 commit a6b1c4f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 26 deletions.
28 changes: 14 additions & 14 deletions lighthouse-core/config/config.js
Expand Up @@ -113,7 +113,7 @@ function validatePasses(passes, audits, rootPath) {
if (!Array.isArray(passes)) {
return;
}
const requiredGatherers = getGatherersNeededByAudits(audits);
const requiredGatherers = Config.getGatherersNeededByAudits(audits);

// Log if we are running gathers that are not needed by the audits listed in the config
passes.forEach(pass => {
Expand Down Expand Up @@ -143,19 +143,6 @@ function validatePasses(passes, audits, rootPath) {
});
}

function getGatherersNeededByAudits(audits) {
// It's possible we didn't get given any audits (but existing audit results), in which case
// there is no need to do any work here.
if (!audits) {
return new Set();
}

return audits.reduce((list, audit) => {
audit.meta.requiredArtifacts.forEach(artifact => list.add(artifact));
return list;
}, new Set());
}

function assertValidAudit(auditDefinition, auditPath) {
const auditName = auditPath || auditDefinition.meta.name;

Expand Down Expand Up @@ -280,6 +267,19 @@ class Config {
validatePasses(configJSON.passes, this._audits, this._configDir);
}

static getGatherersNeededByAudits(audits) {
// It's possible we didn't get given any audits (but existing audit results), in which case
// there is no need to do any work here.
if (!audits) {
return new Set();
}

return audits.reduce((list, audit) => {
audit.meta.requiredArtifacts.forEach(artifact => list.add(artifact));
return list;
}, new Set());
}

/**
* Take an array of audits and audit paths and require any paths (possibly
* relative to the optional `configPath`) using `Runner.resolvePlugin`,
Expand Down
48 changes: 38 additions & 10 deletions lighthouse-extension/app/src/lighthouse-background.js
Expand Up @@ -26,6 +26,7 @@ const log = require('../../../lighthouse-core/lib/log');
const ReportGenerator = require('../../../lighthouse-core/report/report-generator');

const STORAGE_KEY = 'lighthouse_v2';
const isExtension = window.chrome && chrome.runtime;
const _flatten = arr => [].concat(...arr);
const _uniq = arr => Array.from(new Set(arr));

Expand Down Expand Up @@ -61,11 +62,17 @@ function getConfigFromTags(config, aggregationTags) {
}
// Push child aggregations to top level if they are wanted but parent isn't
if (!isAggregationSelected(aggregation) && aggregation.items.length) {
chosenAggregations.push(...aggregation.items);
return false;
aggregation.items.forEach(item => {
item.scored = false;
item.categorizable = false;
item.items = [{audits: item.audits}];
delete item.audits;
chosenAggregations.push(item);
});
return;
};

console.error('unexpected to be ehre', aggregation);
log.error('unexpected to be here', aggregation);
});
config.aggregations = chosenAggregations;

Expand All @@ -77,16 +84,33 @@ function getConfigFromTags(config, aggregationTags) {
// The `audits` property in the config is a list of paths of audits to run.
// `requestedAuditNames` is a list of audit *names*. Map paths to names, then
// filter out any paths of audits with names that weren't requested.
const auditPathToName = new Map(Config.requireAudits(config.audits)
.map((AuditClass, index) => {
const auditPath = config.audits[index];
const auditName = AuditClass.meta.name;
return [auditPath, auditName];
}));
const auditObjectsAll = Config.requireAudits(config.audits);
const auditPathToName = new Map(auditObjectsAll.map((AuditClass, index) => {
const auditPath = config.audits[index];
const auditName = AuditClass.meta.name;
return [auditPath, auditName];
}));
config.audits = config.audits.filter(auditPath => {
const auditName = auditPathToName.get(auditPath);
return requestedAuditNames.has(auditName);
});

const auditObjects = Config.requireAudits(config.audits);
const requiredGatherers = Config.getGatherersNeededByAudits(auditObjects);
config.passes = config.passes.filter(pass => {
// remove any unncessary gatherers
pass.gatherers = pass.gatherers.filter(gathererName => requiredGatherers.has(gathererName));
return pass.gatherers.length > 0;
});
if (config.passes.length === 0) {
if (requiredGatherers.has('traces') || requiredGatherers.has('networkRecords')) {
config.passes.push({
recordNetwork: requiredGatherers.has('networkRecords'),
recordTrace: requiredGatherers.has('traces'),
gatherers: []
});
}
}
}

/**
Expand All @@ -95,7 +119,7 @@ function getConfigFromTags(config, aggregationTags) {
* Otherwise, restore the default badge text.
*/
function updateBadgeUI(optUrl) {
if (window.chrome && chrome.runtime) {
if (isExtension) {
const manifest = chrome.runtime.getManifest();

let title = manifest.browser_action.default_title;
Expand Down Expand Up @@ -327,3 +351,7 @@ if (window.chrome && chrome.runtime) {
}
});
}

window.getManifest = function() {
return isExtension && chrome.runtime.getManifest();
};
4 changes: 2 additions & 2 deletions lighthouse-extension/app/src/popup.js
Expand Up @@ -18,7 +18,7 @@

document.addEventListener('DOMContentLoaded', _ => {
const background = chrome.extension.getBackgroundPage();
const defaultAggregations = background.getDefaultAggregations();
// const defaultAggregations = background.getDefaultAggregations();
const defaultAggregationTags = background.getDefaultAggregationTags();

const siteNameEl = window.document.querySelector('header h2');
Expand Down Expand Up @@ -57,7 +57,7 @@ document.addEventListener('DOMContentLoaded', _ => {
};

function getLighthouseVersion() {
return chrome.runtime.getManifest().version;
return background.getManifest().version;
}

function getChromeVersion() {
Expand Down

0 comments on commit a6b1c4f

Please sign in to comment.