Skip to content

Commit

Permalink
feat: update plugin to use the Ecoindex nodes mesure method 🔥
Browse files Browse the repository at this point in the history
  • Loading branch information
hrenaud committed Nov 6, 2023
1 parent 3b60ff2 commit 9088217
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 22 deletions.
6 changes: 2 additions & 4 deletions lighthouse-plugin-ecoindex/audits/ecoindex-ghg.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ class EcoindexGreenhouseGasEmissionAudit extends Audit {
title: 'Greenhouse Gas Emission (eqCO2)',
failureTitle:
'Greenhouse Gas Emission (eqCO2), your page generates a lot of greenhouse gas',
description:
'Pages should be lightweight in order to be more sustainable.',
description: 'Greenhouse Gas Emission (eqCO2) of your page.',
requiredArtifacts: ['DOMStats', 'devtoolsLogs'],
supportedModes: ['navigation', 'timespan', 'snapshot'],
scoreDisplayMode: 'numeric',
Expand Down Expand Up @@ -44,8 +43,7 @@ class EcoindexGreenhouseGasEmissionAudit extends Audit {
{
id: 'eco-index-ghg',
title: 'Greenhouse Gas Emission',
description:
'The EcoIndex score evaluating the environmental impact of the page.',
description: 'Greenhouse Gas Emission (eqCO2) of your page.',
scoreDisplayMode: 'numeric',
},
]
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-plugin-ecoindex/audits/ecoindex-grade.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class EcoindexGradeAudit extends Audit {
title: 'Grade',
failureTitle: 'Grade, your page has a big impact',
description:
'Pages should be lightweight in order to be more sustainable.',
'The EcoIndex score evaluating the environmental impact of the page.',
requiredArtifacts: ['DOMStats', 'devtoolsLogs'],
supportedModes: ['navigation', 'timespan', 'snapshot'],
scoreDisplayMode: 'numeric',
Expand Down
9 changes: 4 additions & 5 deletions lighthouse-plugin-ecoindex/audits/ecoindex-nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ class EcoindexNodesAudit extends Audit {
static get meta() {
return {
id: 'eco-index-nodes',
title: 'DOM elements (nodes)',
failureTitle: 'DOM elements (nodes), your page is too complex',
description:
'Pages should be lightweight in order to be more sustainable.',
requiredArtifacts: ['DOMStats', 'devtoolsLogs'],
title: 'Ecoindex DOM elements (nodes)',
failureTitle: 'Ecoindex DOM elements (nodes), your page is too complex',
description: 'The number of HTML elements in the DOM.',
requiredArtifacts: ['NodesMinusSvgsGatherer', 'devtoolsLogs'],
supportedModes: ['navigation', 'timespan', 'snapshot'],
scoreDisplayMode: 'numeric',
}
Expand Down
3 changes: 1 addition & 2 deletions lighthouse-plugin-ecoindex/audits/ecoindex-requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class EcoindexRequestsAudit extends Audit {
id: 'eco-index-requests',
title: 'Number of requests',
failureTitle: 'Number of requests, your page calls too many resources',
description:
'Pages should be lightweight in order to be more sustainable.',
description: 'The number of network requests made by the page.',
requiredArtifacts: ['DOMStats', 'devtoolsLogs'],
supportedModes: ['navigation', 'timespan', 'snapshot'],
scoreDisplayMode: 'numeric',
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-plugin-ecoindex/audits/ecoindex-score.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class EcoindexScoreAudit extends Audit {
title: 'Ecoindex revealant metrics',
failureTitle: 'Ecoindex, your page has an impact',
description:
'Pages should be lightweight in order to be more sustainable.',
'The EcoIndex score evaluating the environmental impact of the page.',
requiredArtifacts: ['DOMStats', 'devtoolsLogs'],
supportedModes: ['navigation', 'timespan', 'snapshot'],
scoreDisplayMode: 'numeric',
Expand Down
6 changes: 2 additions & 4 deletions lighthouse-plugin-ecoindex/audits/ecoindex-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class EcoindexSizeAudit extends Audit {
id: 'eco-index-size',
title: 'Page size in Megaoctets',
failureTitle: 'Page size in Megaoctets, your page is too heavy',
description:
'Pages should be lightweight in order to be more sustainable.',
description: 'The total size of all assets and page, in bytes.',
requiredArtifacts: ['DOMStats', 'devtoolsLogs'],
supportedModes: ['navigation', 'timespan', 'snapshot'],
scoreDisplayMode: 'numeric',
Expand Down Expand Up @@ -43,8 +42,7 @@ class EcoindexSizeAudit extends Audit {
{
id: 'eco-index-size',
title: 'Page size in Megaoctets',
description:
'The EcoIndex score evaluating the environmental impact of the page.',
description: 'The total size of all assets and page, in bytes.',
scoreDisplayMode: 'numeric',
},
]
Expand Down
6 changes: 2 additions & 4 deletions lighthouse-plugin-ecoindex/audits/ecoindex-water.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class EcoindexWaterConsumptionAudit extends Audit {
id: 'eco-index-water',
title: 'Water Consumption (cl)',
failureTitle: 'Water Consumption (cl), your page consumes a lot of water',
description:
'Pages should be lightweight in order to be more sustainable.',
description: 'The quantity of water consumed by the page.',
requiredArtifacts: ['DOMStats', 'devtoolsLogs'],
supportedModes: ['navigation', 'timespan', 'snapshot'],
scoreDisplayMode: 'numeric',
Expand Down Expand Up @@ -43,8 +42,7 @@ class EcoindexWaterConsumptionAudit extends Audit {
{
id: 'eco-index-water',
title: 'Water Consumption',
description:
'The EcoIndex score evaluating the environmental impact of the page.',
description: 'The quantity of water consumed by the page.',
scoreDisplayMode: 'numeric',
},
]
Expand Down
73 changes: 73 additions & 0 deletions lighthouse-plugin-ecoindex/audits/lighthouse-nodes.js
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
69 changes: 69 additions & 0 deletions lighthouse-plugin-ecoindex/audits/warn-nodes-count.js
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 lighthouse-plugin-ecoindex/gatherers/nodes-minus-svg-gatherer.js
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
6 changes: 6 additions & 0 deletions lighthouse-plugin-ecoindex/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ export default {
{ path: 'lighthouse-plugin-ecoindex/audits/ecoindex-water.js' },
{ path: 'lighthouse-plugin-ecoindex/audits/ecoindex-ghg.js' },
{ path: 'lighthouse-plugin-ecoindex/audits/ecoindex-nodes.js' },
// { path: 'lighthouse-plugin-ecoindex/audits/lighthouse-nodes.js' },
{ path: 'lighthouse-plugin-ecoindex/audits/ecoindex-size.js' },
{ path: 'lighthouse-plugin-ecoindex/audits/ecoindex-requests.js' },
{
path: 'lighthouse-plugin-ecoindex/audits/warn-nodes-count.js',
},
],
groups: {
ecologic: {
Expand All @@ -31,8 +35,10 @@ export default {
{ id: 'eco-index-water', weight: 0, group: 'ecologic' },
{ id: 'eco-index-ghg', weight: 0, group: 'ecologic' },
{ id: 'eco-index-nodes', weight: 0, group: 'technic' },
// { id: 'lighthouse-nodes', weight: 0, group: 'technic' },
{ id: 'eco-index-size', weight: 0, group: 'technic' },
{ id: 'eco-index-requests', weight: 0, group: 'technic' },
{ id: 'warn-nodes-count', weight: 0 },
],
},
}
11 changes: 10 additions & 1 deletion lighthouse-plugin-ecoindex/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ export async function getLoadingExperience(
context,
isTechnical = false,
) {
const domSize = artifacts.DOMStats.totalBodyElements
let domSize = -1
try {
if (artifacts.NodesMinusSvgsGatherer) {
domSize = artifacts.NodesMinusSvgsGatherer.value
} else {
domSize = artifacts.DOMStats.totalBodyElements
}
} catch (error) {
throw new Error('NodesMinusSvgsGatherer not found')
}

// repiqué de https://github.com/GoogleChrome/lighthouse/blob/main/core/audits/byte-efficiency/total-byte-weight.js#L61
const devtoolsLog = artifacts.devtoolsLogs[Audit.DEFAULT_PASS]
Expand Down

0 comments on commit 9088217

Please sign in to comment.