Lighthouse v3 迁移指南

本指南适用于符合以下条件的 Lighthouse v2 用户:

  • 从 Node 或命令行运行 Lighthouse。
  • 依赖 Lighthouse 的 JSON 输出。

如果这些都不适用于您,则运行 Lighthouse 的工作流大致相同。如需简要了解新功能和变更,请参阅正式发布 Lighthouse 3.0

调用变更

Lighthouse 现在默认计算模拟性能,并且节流设置已大幅更改。

CLI 标志

场景 v2 标志 v3 标志
开发者工具 3G 节流 无(默认行为) --throttling-method=devtools
无限制 --disable-network-throttling --disable-cpu-throttling --throttling-method=provided
网络节流、无 CPU 节流 --disable-cpu-throttling --throttling-method=devtools --throttling.cpuSlowdownMultiplier=1
运行性能审核 --perf --preset=perf
运行混合内容审核 --mixed-content --preset=mixed-content

节点模块

在 Lighthouse v3 中,Node 模块接受与 CLI 相同的配置选项。这是一项重大变更,因为 v2 中会忽略其中的许多选项,而现在,它们实际上会影响 Lighthouse 的运行方式。

const fs = require('fs');
const lighthouse = require('lighthouse');

async function run() {
  // `onlyCategories` was previously only available as a config setting.
  // `output` was previously only available in CLI.
  const flags = {onlyCategories: ['performance'], output: 'html'};
  const html = (await lighthouse('https://google.com/', flags)).report;
  fs.writeFileSync('report.html', html);
}

输出方面的变更

JSON 输出中的全新顶级格式

Lighthouse v3 返回的 JSON 对象现在包含 3 个顶级属性:

  • lhr:审核结果。“Lighthouse Results”的简称。这本质上是 v2 中的顶级对象,但 v3 也对此对象的形状引入了破坏性更改。请参阅对结果对象的更改
  • artifacts:审核期间从 Chrome 收集的数据。之前,此属性与 LHR 的属性混合在一起。
  • report:以字符串表示的格式化报告 HTML/JSON/CSV。

对结果对象的更改

JSON 输出中新的顶级格式中所述,现在可通过 lhr 属性查看审核结果。在 v2 中,此对象的内容本质上是顶级 JSON 输出。不过,此对象本身的形状在 v3 中发生了变化。下表列出了所有更改。

  • 如果某行在 v2v3 列中都有值,则表示您应将代码中对 v2 属性的任何引用替换为 v3 等效值。
  • 如果某行在 v3 列中没有值,备注列将描述您的选项。
  • 请注意,ID 等项表示占位符文本。
v2 属性 v3 对等 备注
initialUrl requestedUrl
url finalUrl
generatedTime fetchedTime
reportCategories categories 从数组更改为了键控对象。
reportGroups categoryGroups
audits.ID.name audits.ID.id
audits.ID.description audits.ID.title
audits.ID.helpText audits.ID.description
audits.ID.scoringMode audits.ID.scoreDisplayMode 可能的值已扩展为 numeric|binary|manual|informative|not-applicable|error
audits.ID.score audits.ID.score scoreDisplayMode 是数字或二进制数据时,得分始终是 0 到 1(而非 0-100)之间的数字。其他显示模式的得分始终为 null,因为没有通过/失败的概念。
audits.ID.displayValue audits.ID.displayValue 现在可以是用于字符串插值的 printf 样式参数数组。
audits.ID.debugString audits.ID.explanation audits.ID.errorMessage audits.ID.warnings debugString 值已根据其意图转换为上述三个属性之一。
audits.ID.details audits.ID.details 细节结构变得更加易用。.items 中的每个条目都是具有可靠键的对象,而不是 any[]
audits.ID.error audits.ID.scoreDisplayMode === 'error'
audits.ID.notApplicable audits.ID.scoreDisplayMode === 'not-applicable'
audits.ID.informative audits.ID.scoreDisplayMode === 'informative'
audits.ID.manual audits.ID.scoreDisplayMode === 'manual'
audits.ID.extendedInfo 已移除。请改用 details