Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core(tsc): fix ImageUsage artifact type and gather bug #5113

Merged
merged 4 commits into from
May 4, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lighthouse-cli/test/smokehouse/byte-efficiency/expectations.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,18 @@ module.exports = [
},
},
'uses-responsive-images': {
displayValue: [
'Potential savings of %d\xa0KB',
75,
],
extendedInfo: {
value: {
wastedKb: '>50',
results: {
length: 3,
},
results: [
{wastedPercent: '<60'},
{wastedPercent: '<60'},
{wastedPercent: '<60'},
],
},
},
},
Expand Down
7 changes: 4 additions & 3 deletions lighthouse-core/audits/image-aspect-ratio.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
* audit will list all images that don't match with their display size
* aspect ratio.
*/
// @ts-nocheck - TODO(bckenny): fix optional width/height in ImageUsage artifact
'use strict';

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

const URL = require('../lib/url-shim');
const THRESHOLD = 0.05;

/** @typedef {Required<LH.Artifacts.SingleImageUsage>} WellDefinedImage */

class ImageAspectRatio extends Audit {
/**
* @return {LH.Audit.Meta}
Expand All @@ -32,7 +33,7 @@ class ImageAspectRatio extends Audit {
}

/**
* @param {Required<LH.Artifacts.SingleImageUsage>} image
* @param {WellDefinedImage} image
* @return {Error|{url: string, displayedAspectRatio: string, actualAspectRatio: string, doRatiosMatch: boolean}}
*/
static computeAspectRatios(image) {
Expand Down Expand Up @@ -76,7 +77,7 @@ class ImageAspectRatio extends Audit {
image.height &&
!image.usesObjectFit;
}).forEach(image => {
const wellDefinedImage = /** @type {Required<LH.Artifacts.SingleImageUsage>} */ (image);
const wellDefinedImage = /** @type {WellDefinedImage} */ (image);
const processed = ImageAspectRatio.computeAspectRatios(wellDefinedImage);
if (processed instanceof Error) {
debugString = processed.message;
Expand Down
43 changes: 20 additions & 23 deletions lighthouse-core/gather/gatherers/image-usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const Driver = require('../driver.js'); // eslint-disable-line no-unused-vars

/* global window, getElementsInDocument, Image */

/** @typedef {MakeOptional<LH.Artifacts.SingleImageUsage, 'networkRecord'>} ImageSizeInfo */
/** @typedef {Omit<LH.Artifacts.SingleImageUsage, 'networkRecord'>} ImageSizeInfo */

/** @return {Array<ImageSizeInfo>} */
/* istanbul ignore next */
Expand Down Expand Up @@ -145,7 +145,7 @@ class ImageUsage extends Gatherer {
* @param {LH.Gatherer.LoadData} loadData
* @return {Promise<LH.Artifacts['ImageUsage']>}
*/
afterPass(passContext, loadData) {
async afterPass(passContext, loadData) {
const driver = passContext.driver;
const indexedNetworkRecords = loadData.networkRecords.reduce((map, record) => {
if (/^image/.test(record._mimeType) && record.finished) {
Expand All @@ -167,27 +167,24 @@ class ImageUsage extends Gatherer {
return (${collectImageElementInfo.toString()})();
})()`;

/** @type {Promise<Array<ImageSizeInfo>>} */
const evaluatePromise = driver.evaluateAsync(expression);
return evaluatePromise.then(elements => {
return elements.reduce((promise, element) => {
return promise.then(collector => {
// Images within `picture` behave strangely and natural size information isn't accurate,
// CSS images have no natural size information at all.
// Try to get the actual size if we can.
const elementPromise = (element.isPicture || element.isCss) && element.networkRecord ?
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was the bug. element.networkRecord was never defined at this point

this.fetchElementWithSizeInformation(driver, element) :
Promise.resolve(element);

return elementPromise.then(element => {
// link up the image with its network record
element.networkRecord = indexedNetworkRecords[element.src];
collector.push(/** @type {LH.Artifacts.SingleImageUsage} */ (element));
return collector;
});
});
}, Promise.resolve(/** @type {LH.Artifacts['ImageUsage']} */ ([])));
});
/** @type {Array<ImageSizeInfo>} */
const elements = await driver.evaluateAsync(expression);

/** @type {LH.Artifacts['ImageUsage']} */
const imageUsage = [];
for (let element of elements) {
// Images within `picture` behave strangely and natural size information isn't accurate,
// CSS images have no natural size information at all.
// Try to get the actual size if we can.
if (element.isPicture || element.isCss) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we maintain the original concern and only fetch when we can find the network record? fetchElementWithSize is the expensive path

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we maintain the original concern and only fetch when we can find the network record?

Only uses-responsive-images seems to use naturalWidth/naturalHeight from css/<picture> images, and it requires a networkRecord to work, so makes sense

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when we can find the network record?

...actually this conversation clearly shows that it should have been an optional property...

element = await this.fetchElementWithSizeInformation(driver, element);
}

// link up the image with its network record
imageUsage.push(Object.assign({networkRecord: indexedNetworkRecords[element.src]}, element));
}

return imageUsage;
}
}

Expand Down
4 changes: 3 additions & 1 deletion typings/artifacts.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ declare global {
endTime: number;
responseReceivedTime: number;
mimeType: string;
}
};
width?: number;
height?: number;
}

export interface OptimizedImage {
Expand Down
3 changes: 3 additions & 0 deletions typings/externs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ declare global {
[P in K]+?: T[P]
}

/** Remove properties K from T. */
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

/** Obtain the type of the first parameter of a function. */
type FirstParamType<T extends (arg1: any, ...args: any[]) => any> =
T extends (arg1: infer P, ...args: any[]) => any ? P : any;
Expand Down