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(legacy-javascript): key on script id, not url #13746

Merged
merged 3 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ class UnusedBytes extends Audit {

const simulationBeforeChanges = simulator.simulate(graph, {label: beforeLabel});

// TODO: change this to wastedBytesByScriptId
const wastedBytesByUrl = options.providedWastedBytesByUrl || new Map();
if (!options.providedWastedBytesByUrl) {
for (const {url, wastedBytes} of results) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class DuplicatedJavascript extends ByteEfficiencyAudit {
const networkRecord = getRequestForScript(networkRecords, script);

if (!script || script.length === undefined) {
// This should never happen because we found the wasted bytes from bundles, which required contents in a ScriptElement.
// This should never happen because we found the wasted bytes from bundles, which required contents in a Script.
continue;
}

Expand Down
31 changes: 15 additions & 16 deletions lighthouse-core/audits/byte-efficiency/legacy-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,21 +277,21 @@ class LegacyJavascript extends ByteEfficiencyAudit {
* @param {LH.Artifacts['Scripts']} scripts
* @param {LH.Artifacts.NetworkRequest[]} networkRecords
* @param {LH.Artifacts.Bundle[]} bundles
* @return {Map<string, PatternMatchResult[]>}
* @return {Map<LH.Artifacts.Script, PatternMatchResult[]>}
*/
static detectAcrossScripts(matcher, scripts, networkRecords, bundles) {
/** @type {Map<string, PatternMatchResult[]>} */
const urlToMatchResults = new Map();
/** @type {Map<LH.Artifacts.Script, PatternMatchResult[]>} */
const scriptToMatchResults = new Map();
const polyfillData = this.getPolyfillData();

for (const {scriptId, url, content} of Object.values(scripts)) {
if (!content) continue;
for (const script of Object.values(scripts)) {
if (!script.content) continue;

// Start with pattern matching against the downloaded script.
const matches = matcher.match(content);
const matches = matcher.match(script.content);

// If it's a bundle with source maps, add in the polyfill modules by name too.
const bundle = bundles.find(b => b.script.scriptId === scriptId);
const bundle = bundles.find(b => b.script.scriptId === script.scriptId);
if (bundle) {
for (const {coreJs2Module, coreJs3Module, name} of polyfillData) {
// Skip if the pattern matching found a match for this polyfill.
Expand All @@ -311,11 +311,10 @@ class LegacyJavascript extends ByteEfficiencyAudit {
}

if (!matches.length) continue;
// TODO: scriptId
urlToMatchResults.set(url, matches);
scriptToMatchResults.set(script, matches);
}

return urlToMatchResults;
return scriptToMatchResults;
}

/**
Expand Down Expand Up @@ -410,15 +409,15 @@ class LegacyJavascript extends ByteEfficiencyAudit {
/** @type {Map<string, number>} */
const transferRatioByUrl = new Map();

const urlToMatchResults =
const scriptToMatchResults =
this.detectAcrossScripts(matcher, artifacts.Scripts, networkRecords, bundles);
for (const [url, matches] of urlToMatchResults.entries()) {
for (const [script, matches] of scriptToMatchResults.entries()) {
const transferRatio = await this.estimateTransferRatioForScript(
transferRatioByUrl, url, artifacts, networkRecords);
transferRatioByUrl, script.url, artifacts, networkRecords);
const wastedBytes = Math.round(this.estimateWastedBytes(matches) * transferRatio);
/** @type {typeof items[number]} */
const item = {
url,
url: script.url,
wastedBytes,
subItems: {
type: 'subitems',
Expand All @@ -428,13 +427,13 @@ class LegacyJavascript extends ByteEfficiencyAudit {
totalBytes: 0,
};

const bundle = bundles.find(bundle => bundle.script.url === url); // TODO: scriptId
const bundle = bundles.find(bundle => bundle.script.scriptId === script.scriptId);
for (const match of matches) {
const {name, line, column} = match;
/** @type {SubItem} */
const subItem = {
signal: name,
location: ByteEfficiencyAudit.makeSourceLocation(url, line, column, bundle),
location: ByteEfficiencyAudit.makeSourceLocation(script.url, line, column, bundle),
};
item.subItems.items.push(subItem);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class RenderBlockingResources extends Audit {
for (const item of unusedCssItems) {
wastedBytesByUrl.set(item.url, item.wastedBytes);
}
} catch (_) {}
} catch {}

return wastedBytesByUrl;
}
Expand Down