-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update plugin to use the Ecoindex nodes mesure method 🔥
- Loading branch information
Showing
12 changed files
with
206 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { | ||
createErrorResult, | ||
createValueResult, | ||
getLoadingExperience, | ||
} from '../utils/index.js' | ||
|
||
import { Audit } from 'lighthouse' | ||
|
||
/** | ||
* @deprecated use lighthouse-plugin-ecoindex/audits/ecoindex-nodes.js | ||
*/ | ||
class LighthouseNodesAudit extends Audit { | ||
static get meta() { | ||
return { | ||
id: 'lighthouse-nodes', | ||
title: 'LH DOM elements (nodes)', | ||
failureTitle: 'LH DOM elements (nodes), your page is too complex', | ||
description: | ||
'Pages should be lightweight in order to be more sustainable.', | ||
// desabled because we don't need to run this audit if we don't have the NodesMinusSvgsGatherer | ||
requiredArtifacts: ['DOMStats', 'devtoolsLogs'], | ||
|
||
supportedModes: ['navigation', 'timespan', 'snapshot'], | ||
scoreDisplayMode: 'numeric', | ||
} | ||
} | ||
|
||
static get metrics() { | ||
return [ | ||
{ | ||
id: 'dom-size', | ||
title: 'DOM Size', | ||
description: 'The size of the DOM in bytes.', | ||
scoreDisplayMode: 'numeric', | ||
}, | ||
{ | ||
id: 'request-count', | ||
title: 'Request Count', | ||
description: 'The number of network requests made by the page.', | ||
scoreDisplayMode: 'numeric', | ||
}, | ||
{ | ||
id: 'total-compressed-size', | ||
title: 'Total Compressed Size', | ||
description: 'The total size of all compressed responses in bytes.', | ||
scoreDisplayMode: 'numeric', | ||
}, | ||
{ | ||
id: 'eco-index-nodes', | ||
title: 'DOM elements', | ||
description: | ||
'The EcoIndex score evaluating the environmental impact of the page.', | ||
scoreDisplayMode: 'numeric', | ||
}, | ||
] | ||
} | ||
|
||
static async audit(artifacts, context) { | ||
console.log('artifacts', JSON.stringify(artifacts)) | ||
if (artifacts['NodesMinusSvgsGatherer']) { | ||
return { score: null, notApplicable: true } | ||
} | ||
try { | ||
const ecoIndexScore = await getLoadingExperience(artifacts, context, true) | ||
// console.log('nodes', ecoIndexScore.nodes) | ||
return createValueResult(ecoIndexScore, 'nodes', false, true) | ||
} catch (error) { | ||
createErrorResult(error) | ||
} | ||
} | ||
} | ||
|
||
export default LighthouseNodesAudit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { Audit } from 'lighthouse' | ||
|
||
class WarnNodesCount extends Audit { | ||
static get meta() { | ||
return { | ||
id: 'warn-nodes-count', | ||
title: '⚠️ Ecoindex nodes number might be ≠ Lighthouse nodes number.', | ||
failureTitle: | ||
'⚠️ Ecoindex nodes number might be ≠ Lighthouse nodes number.', | ||
description: | ||
'Explication: In Ecoindex, we count all HTML nodes, withouts SVGs content.', | ||
|
||
// The name of the custom gatherer class that provides input to this audit. | ||
requiredArtifacts: ['NodesMinusSvgsGatherer', 'DOMStats', 'devtoolsLogs'], | ||
} | ||
} | ||
|
||
static get metrics() { | ||
return [ | ||
{ | ||
id: 'dom-size', | ||
title: 'DOM Size', | ||
description: 'The size of the DOM in bytes.', | ||
scoreDisplayMode: 'numeric', | ||
}, | ||
{ | ||
id: 'request-count', | ||
title: 'Request Count', | ||
description: 'The number of network requests made by the page.', | ||
scoreDisplayMode: 'numeric', | ||
}, | ||
{ | ||
id: 'total-compressed-size', | ||
title: 'Total Compressed Size', | ||
description: 'The total size of all compressed responses in bytes.', | ||
scoreDisplayMode: 'numeric', | ||
}, | ||
{ | ||
id: 'custom-audit', | ||
title: 'custom-audit', | ||
description: 'custom-audit.', | ||
scoreDisplayMode: 'numeric', | ||
}, | ||
] | ||
} | ||
|
||
static audit(artifacts) { | ||
const value = artifacts.NodesMinusSvgsGatherer.value | ||
const DOMStats = artifacts.DOMStats.totalBodyElements | ||
// const success = isNaN(value) && value >= 0 | ||
|
||
return { | ||
numericValue: value, | ||
// Cast true/false to 1/0 | ||
score: 0.8, | ||
displayValue: `Ecoindex: ${value} - Lighthouse: ${DOMStats}`, | ||
numericUnit: 'DOM elements', | ||
// displayValue: value, | ||
} | ||
} | ||
} | ||
|
||
export default WarnNodesCount |
35 changes: 35 additions & 0 deletions
35
lighthouse-plugin-ecoindex/gatherers/nodes-minus-svg-gatherer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { Gatherer } from 'lighthouse' | ||
|
||
class NodesMinusSvgsGatherer extends Gatherer { | ||
meta = { | ||
supportedModes: ['navigation', 'timespan', 'snapshot'], | ||
} | ||
|
||
async getArtifact(context) { | ||
const { driver } = context | ||
/** | ||
* nodes = await self.page.locator("*").all() | ||
* svgs = await self.page.locator("//*[local-name()='svg']//*").all() | ||
*/ | ||
const { executionContext } = driver | ||
const nodes = await executionContext.evaluateAsync( | ||
`document.querySelectorAll('*').length`, | ||
) | ||
const svgs = await executionContext.evaluateAsync( | ||
`document.querySelectorAll("svg *").length`, | ||
) | ||
const value = nodes - svgs | ||
// console.log('nodes', nodes) | ||
// console.log('svgs', svgs) | ||
// console.log('value', value) | ||
return { value } | ||
} | ||
} | ||
|
||
export default NodesMinusSvgsGatherer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters