-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
fix(unused-css-rules): update to support new coverage format #2518
Changes from 5 commits
4608a68
c56b19f
b669d5e
210c514
57de9c9
a0b414f
237a94f
1e35aa9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
'use strict'; | ||
|
||
const ByteEfficiencyAudit = require('./byte-efficiency-audit'); | ||
const URL = require('../../lib/url-shim'); | ||
|
||
const PREVIEW_LENGTH = 100; | ||
|
||
|
@@ -34,30 +33,26 @@ class UnusedCSSRules extends ByteEfficiencyAudit { | |
*/ | ||
static indexStylesheetsById(styles, networkRecords) { | ||
const indexedNetworkRecords = networkRecords | ||
.filter(record => record._resourceType && record._resourceType._name === 'stylesheet') | ||
.reduce((indexed, record) => { | ||
indexed[record.url] = record; | ||
return indexed; | ||
}, {}); | ||
|
||
return styles.reduce((indexed, stylesheet) => { | ||
indexed[stylesheet.header.styleSheetId] = Object.assign({ | ||
used: [], | ||
unused: [], | ||
usedRules: [], | ||
networkRecord: indexedNetworkRecords[stylesheet.header.sourceURL], | ||
}, stylesheet); | ||
return indexed; | ||
}, {}); | ||
} | ||
|
||
/** | ||
* Counts the number of unused rules and adds count information to sheets. | ||
* Adds used rules to their corresponding stylesheet. | ||
* @param {!Array.<{styleSheetId: string, used: boolean}>} rules The output of the CSSUsage gatherer. | ||
* @param {!Object} indexedStylesheets Stylesheet information indexed by id. | ||
* @return {number} The number of unused rules. | ||
*/ | ||
static countUnusedRules(rules, indexedStylesheets) { | ||
let unused = 0; | ||
|
||
static indexUsedRules(rules, indexedStylesheets) { | ||
rules.forEach(rule => { | ||
const stylesheetInfo = indexedStylesheets[rule.styleSheetId]; | ||
|
||
|
@@ -66,14 +61,41 @@ class UnusedCSSRules extends ByteEfficiencyAudit { | |
} | ||
|
||
if (rule.used) { | ||
stylesheetInfo.used.push(rule); | ||
} else { | ||
unused++; | ||
stylesheetInfo.unused.push(rule); | ||
stylesheetInfo.usedRules.push(rule); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* @param {!Object} stylesheetInfo | ||
* @return {{wastedBytes: number, totalBytes: number, wastedPercent: number}} | ||
*/ | ||
static computeUsage(stylesheetInfo) { | ||
let usedUncompressedBytes = 0; | ||
const totalUncompressedBytes = stylesheetInfo.content.length; | ||
|
||
return unused; | ||
for (const usedRule of stylesheetInfo.usedRules) { | ||
usedUncompressedBytes += usedRule.endOffset - usedRule.startOffset; | ||
} | ||
|
||
const networkRecord = stylesheetInfo.networkRecord; | ||
let totalBytes = networkRecord && stylesheetInfo.networkRecord.transferSize; | ||
if (!networkRecord || networkRecord._resourceType._name !== 'stylesheet') { | ||
// If we don't know for sure how many bytes this sheet used on the network, | ||
// we can guess it was roughly the size of the content length. | ||
const wasCompressed = !networkRecord || | ||
networkRecord._transferSize !== networkRecord._resourceSize; | ||
totalBytes = wasCompressed ? Math.round(totalUncompressedBytes / 3) : totalUncompressedBytes; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a little confusing to follow. What do you think of something like let totalTransferredBytes;
if (networkRecord && networkRecord._resourceType._name === 'stylesheet') {
totalTransferredBytes = networkRecord.transferSize;
} else {
// If we don't know for sure how many bytes this sheet used on the network,
// we can guess it was roughly the size of the content length.
const wasCompressed = !networkRecord ||
networkRecord._transferSize !== networkRecord._resourceSize;
totalTransferredBytes = wasCompressed ? Math.round(totalUncompressedBytes / 3) :
totalUncompressedBytes;
} the variable rename alone might be enough to clear things up, though There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah sg, done |
||
|
||
const percentUnused = (totalUncompressedBytes - usedUncompressedBytes) / totalUncompressedBytes; | ||
const wastedBytes = Math.round(percentUnused * totalBytes); | ||
|
||
return { | ||
wastedBytes, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not worth logging There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah didn't seem worth it anymore, the cases where it happens now are wholly expected and there's no sense being noisy |
||
wastedPercent: percentUnused * 100, | ||
totalBytes, | ||
}; | ||
} | ||
|
||
/** | ||
|
@@ -115,40 +137,23 @@ class UnusedCSSRules extends ByteEfficiencyAudit { | |
/** | ||
* @param {!Object} stylesheetInfo The stylesheetInfo object. | ||
* @param {string} pageUrl The URL of the page, used to identify inline styles. | ||
* @return {{url: string, label: string, code: string}} The result for the details object. | ||
* @return {?{url: string, wastedBytes: number, totalBytes: number}} | ||
*/ | ||
static mapSheetToResult(stylesheetInfo, pageUrl) { | ||
const numUsed = stylesheetInfo.used.length; | ||
const numUnused = stylesheetInfo.unused.length; | ||
|
||
if ((numUsed === 0 && numUnused === 0) || stylesheetInfo.isDuplicate) { | ||
if (stylesheetInfo.isDuplicate) { | ||
return null; | ||
} | ||
|
||
let url = stylesheetInfo.header.sourceURL; | ||
let isInline = false; | ||
if (!url || url === pageUrl) { | ||
const contentPreview = UnusedCSSRules.determineContentPreview(stylesheetInfo.content); | ||
url = '*inline*```' + contentPreview + '```'; | ||
} else { | ||
url = URL.getURLDisplayName(url); | ||
url = {type: 'code', text: contentPreview}; | ||
isInline = true; | ||
} | ||
|
||
// If we don't know for sure how many bytes this sheet used on the network, | ||
// we can guess it was roughly the size of the content gzipped. | ||
const totalBytes = stylesheetInfo.networkRecord ? | ||
stylesheetInfo.networkRecord.transferSize : | ||
Math.round(stylesheetInfo.content.length / 3); | ||
|
||
const percentUnused = numUnused / (numUsed + numUnused); | ||
const wastedBytes = Math.round(percentUnused * totalBytes); | ||
|
||
return { | ||
url, | ||
numUnused, | ||
wastedBytes, | ||
wastedPercent: percentUnused * 100, | ||
totalBytes, | ||
}; | ||
const usage = UnusedCSSRules.computeUsage(stylesheetInfo, isInline); | ||
return Object.assign({url}, usage); | ||
} | ||
|
||
/** | ||
|
@@ -163,21 +168,21 @@ class UnusedCSSRules extends ByteEfficiencyAudit { | |
const devtoolsLogs = artifacts.devtoolsLogs[ByteEfficiencyAudit.DEFAULT_PASS]; | ||
return artifacts.requestNetworkRecords(devtoolsLogs).then(networkRecords => { | ||
const indexedSheets = UnusedCSSRules.indexStylesheetsById(styles, networkRecords); | ||
UnusedCSSRules.countUnusedRules(usage, indexedSheets); | ||
const results = Object.keys(indexedSheets).map(sheetId => { | ||
return UnusedCSSRules.mapSheetToResult(indexedSheets[sheetId], pageUrl); | ||
}).filter(sheet => sheet && sheet.wastedBytes > 1024); | ||
UnusedCSSRules.indexUsedRules(usage, indexedSheets); | ||
|
||
const results = Object.keys(indexedSheets) | ||
.map(sheetId => UnusedCSSRules.mapSheetToResult(indexedSheets[sheetId], pageUrl)) | ||
.filter(sheet => sheet && sheet.wastedBytes > 1024); | ||
|
||
const headings = [ | ||
{key: 'url', itemType: 'url', text: 'URL'}, | ||
{key: 'numUnused', itemType: 'url', text: 'Unused Rules'}, | ||
{key: 'totalKb', itemType: 'text', text: 'Original'}, | ||
{key: 'potentialSavings', itemType: 'text', text: 'Potential Savings'}, | ||
]; | ||
|
||
return { | ||
results, | ||
headings | ||
headings, | ||
}; | ||
}); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we make this more precise since we control the test page?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, the score gets bucketed by the generic byte efficiency audit though so it's not that useful. I added an assertion on the "wastedKb" instead which is a little more explicit and useful