Skip to content

Commit

Permalink
Merge branch 'master' into eslintext
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed Aug 27, 2016
2 parents a0d53ac + 186ce07 commit 6008886
Show file tree
Hide file tree
Showing 27 changed files with 790 additions and 472 deletions.
10 changes: 0 additions & 10 deletions lighthouse-cli/index.js
Expand Up @@ -50,7 +50,6 @@ const cli = yargs
'load-page',
'save-assets',
'save-artifacts',
'audit-whitelist',
'list-all-audits',
'list-trace-categories',
'config-path'
Expand All @@ -60,7 +59,6 @@ const cli = yargs
'load-page': 'Loads the page',
'save-assets': 'Save the trace contents & screenshots to disk',
'save-artifacts': 'Save all gathered artifacts to disk',
'audit-whitelist': 'Comma separated list of audits to run',
'list-all-audits': 'Prints a list of all available audits and exits',
'list-trace-categories': 'Prints a list of all required trace categories and exits',
'config-path': 'The absolute path to the config JSON.'
Expand Down Expand Up @@ -92,7 +90,6 @@ Example: --output-path=./lighthouse-results.html`
// default values
.default('mobile', true)
.default('load-page', true)
.default('audit-whitelist', 'all')
.default('output', Printer.OUTPUT_MODE.pretty)
.default('output-path', 'stdout')
.check(argv => {
Expand Down Expand Up @@ -138,13 +135,6 @@ if (cli.verbose) {
flags.logLevel = 'error';
}

// Normalize audit whitelist.
if (!flags.auditWhitelist || flags.auditWhitelist === 'all') {
flags.auditWhitelist = null;
} else {
flags.auditWhitelist = new Set(flags.auditWhitelist.split(',').map(a => a.toLowerCase()));
}

// kick off a lighthouse run
lighthouse(url, flags, config)
.then(results => Printer.write(results, outputMode, outputPath))
Expand Down
4 changes: 1 addition & 3 deletions lighthouse-core/audits/cache-start-url.js
Expand Up @@ -17,7 +17,6 @@

'use strict';

const url = require('url');
const Audit = require('./audit');

class CacheStartUrl extends Audit {
Expand All @@ -41,7 +40,6 @@ class CacheStartUrl extends Audit {
let cacheHasStartUrl = false;
const manifest = artifacts.Manifest && artifacts.Manifest.value;
const cacheContents = artifacts.CacheContents;
const baseURL = artifacts.URL;

if (!(manifest && manifest.start_url && manifest.start_url.value)) {
return CacheStartUrl.generateAuditResult({
Expand All @@ -58,7 +56,7 @@ class CacheStartUrl extends Audit {
}

// Remove any UTM strings.
const startURL = url.resolve(baseURL, manifest.start_url.value).toString();
const startURL = manifest.start_url.value;
const altStartURL = startURL
.replace(/\?utm_([^=]*)=([^&]|$)*/, '')
.replace(/\?$/, '');
Expand Down
104 changes: 35 additions & 69 deletions lighthouse-core/config/config.js
Expand Up @@ -106,29 +106,22 @@ function cleanTrace(trace) {
return trace;
}

function filterPasses(passes, audits, rootPath) {
function validatePasses(passes, audits, rootPath) {
if (!Array.isArray(passes)) {
return;
}
const requiredGatherers = getGatherersNeededByAudits(audits);

// Make sure we only have the gatherers that are needed by the audits
// that have been listed in the config.
const filteredPasses = passes.map(pass => {
const freshPass = Object.assign({}, pass);

freshPass.gatherers = freshPass.gatherers.filter(gatherer => {
// Log if we are running gathers that are not needed by the audits listed in the config
passes.forEach(pass => {
pass.gatherers.forEach(gatherer => {
const GathererClass = GatherRunner.getGathererClass(gatherer, rootPath);
const isGatherRequiredByAudits = requiredGatherers.has(GathererClass.name);
if (isGatherRequiredByAudits === false) {
log.warn('config', `Skipping ${GathererClass.name} gatherer as no audit requires it.`);
log.warn('config', `${GathererClass.name} is included, however no audit requires it.`);
}
return isGatherRequiredByAudits;
});

return freshPass;
})

// Now remove any passes which no longer have gatherers.
.filter(p => p.gatherers.length > 0);
return filteredPasses;
});
}

function getGatherersNeededByAudits(audits) {
Expand All @@ -144,44 +137,20 @@ function getGatherersNeededByAudits(audits) {
}, new Set());
}

function filterAudits(audits, auditWhitelist) {
// If there is no whitelist, assume all.
if (!auditWhitelist) {
return Array.from(audits);
}

const rejected = [];
const filteredAudits = audits.filter(a => {
const auditName = a.toLowerCase();
const inWhitelist = auditWhitelist.has(auditName);

if (!inWhitelist) {
rejected.push(auditName);
}

return inWhitelist;
});

if (rejected.length) {
log.log('info', 'Running these audits:', `${filteredAudits.join(', ')}`);
log.log('info', 'Skipping these audits:', `${rejected.join(', ')}`);
function requireAudits(audits, rootPath) {
if (!audits) {
return null;
}

return filteredAudits;
}

function expandAudits(audits, rootPath) {
const Runner = require('../runner');
const coreList = Runner.getAuditList();

return audits.map(audit => {
// Firstly see if the audit is in Lighthouse itself.
const list = Runner.getAuditList();
const coreAudit = list.find(a => a === `${audit}.js`);

// Assume it's a core audit first.
let requirePath = path.resolve(__dirname, `../audits/${audit}`);
let AuditClass;

// Firstly see if the audit is in Lighthouse itself.
const coreAudit = coreList.find(a => a === `${audit}.js`);
let requirePath = `../audits/${audit}`;

// If not, see if it can be found another way.
if (!coreAudit) {
// Firstly try and see if the audit resolves naturally through the usual means.
Expand Down Expand Up @@ -247,8 +216,9 @@ function assertValidAudit(audit, auditDefinition) {
}

function expandArtifacts(artifacts) {
const expandedArtifacts = Object.assign({}, artifacts);

if (!artifacts) {
return null;
}
// currently only trace logs and performance logs should be imported
if (artifacts.traces) {
Object.keys(artifacts.traces).forEach(key => {
Expand All @@ -264,15 +234,14 @@ function expandArtifacts(artifacts) {
}
trace = cleanTrace(trace);

expandedArtifacts.traces[key] = trace;
artifacts.traces[key] = trace;
});
}

if (artifacts.performanceLog) {
expandedArtifacts.networkRecords = recordsFromLogs(require(artifacts.performanceLog));
artifacts.networkRecords = recordsFromLogs(require(artifacts.performanceLog));
}

return expandedArtifacts;
return artifacts;
}

/**
Expand All @@ -283,27 +252,24 @@ class Config {
* @constructor
* @param{Object} config
*/
constructor(configJSON, auditWhitelist, configPath) {
constructor(configJSON, configPath) {
if (!configJSON) {
configJSON = defaultConfig;
}

// We don't want to mutate the original config object
configJSON = JSON.parse(JSON.stringify(configJSON));
// Store the directory of the config path, if one was provided.
this._configDir = configPath ? path.dirname(configPath) : undefined;

this._audits = configJSON.audits ? expandAudits(
filterAudits(configJSON.audits, auditWhitelist), this._configDir
) : null;
// filterPasses expects audits to have been expanded
this._passes = configJSON.passes ?
filterPasses(configJSON.passes, this._audits, this._configDir) :
null;
this._auditResults = configJSON.auditResults ? Array.from(configJSON.auditResults) : null;
this._artifacts = null;
if (configJSON.artifacts) {
this._artifacts = expandArtifacts(configJSON.artifacts);
}
this._aggregations = configJSON.aggregations ? Array.from(configJSON.aggregations) : null;
this._passes = configJSON.passes || null;
this._auditResults = configJSON.auditResults || null;
this._aggregations = configJSON.aggregations || null;

this._audits = requireAudits(configJSON.audits, this._configDir);
this._artifacts = expandArtifacts(configJSON.artifacts);

// validatePasses must follow after audits are required
validatePasses(configJSON.passes, this._audits, this._configDir);
}

/** @type {string} */
Expand Down
7 changes: 1 addition & 6 deletions lighthouse-core/config/perf.example-custom.json
Expand Up @@ -3,12 +3,7 @@
"network": true,
"trace": true,
"loadPage": true,
"gatherers": [
"url",
"critical-request-chains",
"screenshots",
"speedline"
]
"gatherers": []
}],

"audits": [
Expand Down
14 changes: 8 additions & 6 deletions lighthouse-core/gather/gather-runner.js
Expand Up @@ -267,13 +267,14 @@ class GatherRunner {

static getGathererClass(gatherer, rootPath) {
const Runner = require('../runner');
const list = Runner.getGathererList();
const coreGatherer = list.find(a => a === `${gatherer}.js`);
const coreList = Runner.getGathererList();

// Assume it's a core gatherer first.
let requirePath = path.resolve(__dirname, `./gatherers/${gatherer}`);
let GathererClass;

// First see if it is a core gatherer in Lighthouse itself.
const coreGatherer = coreList.find(a => a === `${gatherer}.js`);
let requirePath = `./gatherers/${gatherer}`;

// If not, see if it can be found another way.
if (!coreGatherer) {
// Firstly try and see if the gatherer resolves naturally through the usual means.
Expand Down Expand Up @@ -329,8 +330,9 @@ class GatherRunner {

static instantiateComputedArtifacts() {
let computedArtifacts = {};
var normalizedPath = require('path').join(__dirname, 'computed');
require('fs').readdirSync(normalizedPath).forEach(function(file) {
require('fs').readdirSync(path.join(__dirname, 'computed')).forEach(function(file) {
// Drop `.js` suffix to keep browserify import happy.
file = file.replace(/\.js$/, '');
const ArtifactClass = require('./computed/' + file);
const artifact = new ArtifactClass();
// define the request* function that will be exposed on `artifacts`
Expand Down
15 changes: 13 additions & 2 deletions lighthouse-core/gather/gatherers/manifest.js
Expand Up @@ -39,11 +39,22 @@ class Manifest extends Gatherer {
return driver.sendCommand('Page.getAppManifest')
.then(response => {
if (response.errors.length) {
this.artifact = Manifest._errorManifest(response.errors.join(', '));
let errorString;
if (response.url) {
errorString = `Unable to retrieve manifest at ${response.url}: `;
}
this.artifact = Manifest._errorManifest(errorString + response.errors.join(', '));
return;
}

this.artifact = manifestParser(response.data);
// The driver will return an empty string for url and the data if the
// page has no manifest.
if (!response.data.length && !response.data.url) {
this.artifact = Manifest._errorManifest('No manifest found.');
return;
}

this.artifact = manifestParser(response.data, response.url, options.url);
}, _ => {
this.artifact = Manifest._errorManifest('Unable to retrieve manifest');
return;
Expand Down

0 comments on commit 6008886

Please sign in to comment.