lighthouse 12.4.0-dev.20250302 → 12.4.0-dev.20250303
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -4,15 +4,22 @@
|
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
import * as ImageDeliveryInsightModule from '@paulirish/trace_engine/models/trace/insights/ImageDelivery.js';
|
|
7
8
|
import {UIStrings} from '@paulirish/trace_engine/models/trace/insights/ImageDelivery.js';
|
|
8
9
|
|
|
9
10
|
import {Audit} from '../audit.js';
|
|
10
11
|
import * as i18n from '../../lib/i18n/i18n.js';
|
|
11
12
|
import {adaptInsightToAuditProduct} from './insight-audit.js';
|
|
13
|
+
import {TraceEngineResult} from '../../computed/trace-engine-result.js';
|
|
12
14
|
|
|
13
15
|
// eslint-disable-next-line max-len
|
|
14
16
|
const str_ = i18n.createIcuMessageFn('node_modules/@paulirish/trace_engine/models/trace/insights/ImageDelivery.js', UIStrings);
|
|
15
17
|
|
|
18
|
+
const getOptimizationMessage =
|
|
19
|
+
// @ts-expect-error TODO(cjamcl): update trace engine lib to modify types accordingly.
|
|
20
|
+
// right now return type of getOptimizationMessage is wrongly `string`.
|
|
21
|
+
TraceEngineResult.localizeFunction(str_, ImageDeliveryInsightModule.getOptimizationMessage);
|
|
22
|
+
|
|
16
23
|
class ImageDeliveryInsight extends Audit {
|
|
17
24
|
/**
|
|
18
25
|
* @return {LH.Audit.Meta}
|
|
@@ -46,10 +53,6 @@ class ImageDeliveryInsight extends Audit {
|
|
|
46
53
|
return;
|
|
47
54
|
}
|
|
48
55
|
|
|
49
|
-
const relatedEventsMap = insight.relatedEvents && !Array.isArray(insight.relatedEvents) ?
|
|
50
|
-
insight.relatedEvents :
|
|
51
|
-
null;
|
|
52
|
-
|
|
53
56
|
/** @type {LH.Audit.Details.Table['headings']} */
|
|
54
57
|
const headings = [
|
|
55
58
|
/* eslint-disable max-len */
|
|
@@ -66,11 +69,9 @@ class ImageDeliveryInsight extends Audit {
|
|
|
66
69
|
wastedBytes: image.byteSavings,
|
|
67
70
|
subItems: {
|
|
68
71
|
type: /** @type {const} */ ('subitems'),
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
reason,
|
|
73
|
-
wastedBytes: image.optimizations[i].byteSavings,
|
|
72
|
+
items: image.optimizations.map(optimization => ({
|
|
73
|
+
reason: getOptimizationMessage(optimization),
|
|
74
|
+
wastedBytes: optimization.byteSavings,
|
|
74
75
|
})),
|
|
75
76
|
},
|
|
76
77
|
}));
|
|
@@ -13,6 +13,33 @@ declare class TraceEngineResult {
|
|
|
13
13
|
* @return {Promise<LH.Artifacts.TraceEngineResult>}
|
|
14
14
|
*/
|
|
15
15
|
static runTraceEngine(traceEvents: LH.TraceEvent[]): Promise<LH.Artifacts.TraceEngineResult>;
|
|
16
|
+
/**
|
|
17
|
+
* Adapts the given DevTools function that returns a localized string to one
|
|
18
|
+
* that returns a LH.IcuMessage.
|
|
19
|
+
*
|
|
20
|
+
* @template {any[]} Args
|
|
21
|
+
* @template {import('../lib/trace-engine.js').DevToolsIcuMessage} Ret
|
|
22
|
+
* @param {ReturnType<i18n.createIcuMessageFn>} str_
|
|
23
|
+
* @param {(...args: Args) => Ret} fn
|
|
24
|
+
* @return {(...args: Args) => LH.IcuMessage}
|
|
25
|
+
*/
|
|
26
|
+
static localizeFunction<Args extends any[], Ret extends import("../lib/trace-engine.js").DevToolsIcuMessage>(str_: ReturnType<typeof i18n.createIcuMessageFn>, fn: (...args: Args) => Ret): (...args: Args) => LH.IcuMessage;
|
|
27
|
+
/**
|
|
28
|
+
* Converts the input parameters given to `i18nString` usages in DevTools to a
|
|
29
|
+
* LH.IcuMessage.
|
|
30
|
+
*
|
|
31
|
+
* @param {ReturnType<i18n.createIcuMessageFn>} str_
|
|
32
|
+
* @param {import('../lib/trace-engine.js').DevToolsIcuMessage} traceEngineI18nObject
|
|
33
|
+
* @return {LH.IcuMessage}
|
|
34
|
+
*/
|
|
35
|
+
static localize(str_: ReturnType<typeof i18n.createIcuMessageFn>, traceEngineI18nObject: import("../lib/trace-engine.js").DevToolsIcuMessage): LH.IcuMessage;
|
|
36
|
+
/**
|
|
37
|
+
* Recursively finds all DevToolsIcuMessage objects and replaces them with LH.IcuMessage.
|
|
38
|
+
*
|
|
39
|
+
* @param {ReturnType<i18n.createIcuMessageFn>} str_
|
|
40
|
+
* @param {object} object
|
|
41
|
+
*/
|
|
42
|
+
static localizeObject(str_: ReturnType<typeof i18n.createIcuMessageFn>, object: object): void;
|
|
16
43
|
/**
|
|
17
44
|
* @param {import('@paulirish/trace_engine/models/trace/insights/types.js').TraceInsightSets} insightSets
|
|
18
45
|
*/
|
|
@@ -27,4 +54,5 @@ declare class TraceEngineResult {
|
|
|
27
54
|
}, context: LH.Artifacts.ComputedContext): Promise<LH.Artifacts.TraceEngineResult>;
|
|
28
55
|
}
|
|
29
56
|
import * as LH from '../../types/lh.js';
|
|
57
|
+
import * as i18n from '../lib/i18n/i18n.js';
|
|
30
58
|
//# sourceMappingURL=trace-engine-result.d.ts.map
|
|
@@ -33,9 +33,57 @@ class TraceEngineResult {
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
|
-
*
|
|
36
|
+
* Adapts the given DevTools function that returns a localized string to one
|
|
37
|
+
* that returns a LH.IcuMessage.
|
|
38
|
+
*
|
|
39
|
+
* @template {any[]} Args
|
|
40
|
+
* @template {import('../lib/trace-engine.js').DevToolsIcuMessage} Ret
|
|
41
|
+
* @param {ReturnType<i18n.createIcuMessageFn>} str_
|
|
42
|
+
* @param {(...args: Args) => Ret} fn
|
|
43
|
+
* @return {(...args: Args) => LH.IcuMessage}
|
|
37
44
|
*/
|
|
38
|
-
static
|
|
45
|
+
static localizeFunction(str_, fn) {
|
|
46
|
+
return (...args) => this.localize(str_, fn(...args));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Converts the input parameters given to `i18nString` usages in DevTools to a
|
|
51
|
+
* LH.IcuMessage.
|
|
52
|
+
*
|
|
53
|
+
* @param {ReturnType<i18n.createIcuMessageFn>} str_
|
|
54
|
+
* @param {import('../lib/trace-engine.js').DevToolsIcuMessage} traceEngineI18nObject
|
|
55
|
+
* @return {LH.IcuMessage}
|
|
56
|
+
*/
|
|
57
|
+
static localize(str_, traceEngineI18nObject) {
|
|
58
|
+
/** @type {Record<string, string|number>|undefined} */
|
|
59
|
+
let values;
|
|
60
|
+
if (traceEngineI18nObject.values) {
|
|
61
|
+
values = {};
|
|
62
|
+
for (const [key, value] of Object.entries(traceEngineI18nObject.values)) {
|
|
63
|
+
if (value && typeof value === 'object' && '__i18nBytes' in value) {
|
|
64
|
+
values[key] = value.__i18nBytes;
|
|
65
|
+
// TODO: use an actual byte formatter. Right now, this shows the exact number of bytes.
|
|
66
|
+
} else if (value && typeof value === 'object' && 'i18nId' in value) {
|
|
67
|
+
// TODO: add support for str_ values to be IcuMessage. For now, we translate it here.
|
|
68
|
+
// This means that locale swapping won't work for this portion of the IcuMessage.
|
|
69
|
+
// @ts-expect-error
|
|
70
|
+
values[key] = str_(value.i18nId, value.values).formattedDefault;
|
|
71
|
+
} else {
|
|
72
|
+
values[key] = value;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return str_(traceEngineI18nObject.i18nId, values);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Recursively finds all DevToolsIcuMessage objects and replaces them with LH.IcuMessage.
|
|
82
|
+
*
|
|
83
|
+
* @param {ReturnType<i18n.createIcuMessageFn>} str_
|
|
84
|
+
* @param {object} object
|
|
85
|
+
*/
|
|
86
|
+
static localizeObject(str_, object) {
|
|
39
87
|
/**
|
|
40
88
|
* Execute `cb(traceEngineI18nObject)` on every i18n object, recursively. The cb return
|
|
41
89
|
* value replaces traceEngineI18nObject.
|
|
@@ -79,6 +127,33 @@ class TraceEngineResult {
|
|
|
79
127
|
}
|
|
80
128
|
}
|
|
81
129
|
|
|
130
|
+
// Pass `{i18nId: string, values?: {}}` through Lighthouse's i18n pipeline.
|
|
131
|
+
// This is equivalent to if we directly did `str_(UIStrings.whatever, ...)`
|
|
132
|
+
recursiveReplaceLocalizableStrings(object, (traceEngineI18nObject) => {
|
|
133
|
+
let values = traceEngineI18nObject.values;
|
|
134
|
+
if (values) {
|
|
135
|
+
values = structuredClone(values);
|
|
136
|
+
for (const [key, value] of Object.entries(values)) {
|
|
137
|
+
if (value && typeof value === 'object' && '__i18nBytes' in value) {
|
|
138
|
+
// @ts-expect-error
|
|
139
|
+
values[key] = value.__i18nBytes;
|
|
140
|
+
// TODO: use an actual byte formatter. Right now, this shows the exact number of bytes.
|
|
141
|
+
} else if (value && typeof value === 'object' && 'i18nId' in value) {
|
|
142
|
+
// TODO: add support for str_ values to be IcuMessage.
|
|
143
|
+
// @ts-expect-error
|
|
144
|
+
values[key] = str_(value.i18nId, value.values).formattedDefault;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return str_(traceEngineI18nObject.i18nId, values);
|
|
150
|
+
}, new Set());
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* @param {import('@paulirish/trace_engine/models/trace/insights/types.js').TraceInsightSets} insightSets
|
|
155
|
+
*/
|
|
156
|
+
static localizeInsights(insightSets) {
|
|
82
157
|
for (const insightSet of insightSets.values()) {
|
|
83
158
|
for (const [name, model] of Object.entries(insightSet.model)) {
|
|
84
159
|
if (model instanceof Error) {
|
|
@@ -96,28 +171,7 @@ class TraceEngineResult {
|
|
|
96
171
|
|
|
97
172
|
const key = `node_modules/@paulirish/trace_engine/models/trace/insights/${name}.js`;
|
|
98
173
|
const str_ = i18n.createIcuMessageFn(key, traceEngineUIStrings);
|
|
99
|
-
|
|
100
|
-
// Pass `{i18nId: string, values?: {}}` through Lighthouse's i18n pipeline.
|
|
101
|
-
// This is equivalent to if we directly did `str_(UIStrings.whatever, ...)`
|
|
102
|
-
recursiveReplaceLocalizableStrings(model, (traceEngineI18nObject) => {
|
|
103
|
-
let values = traceEngineI18nObject.values;
|
|
104
|
-
if (values) {
|
|
105
|
-
values = structuredClone(values);
|
|
106
|
-
for (const [key, value] of Object.entries(values)) {
|
|
107
|
-
if (value && typeof value === 'object' && '__i18nBytes' in value) {
|
|
108
|
-
// @ts-expect-error
|
|
109
|
-
values[key] = value.__i18nBytes;
|
|
110
|
-
// TODO: use an actual byte formatter. Right now, this shows the exact number of bytes.
|
|
111
|
-
} else if (value && typeof value === 'object' && 'i18nId' in value) {
|
|
112
|
-
// TODO: add support for str_ values to be IcuMessage.
|
|
113
|
-
// @ts-expect-error
|
|
114
|
-
values[key] = str_(value.i18nId, value.values).formattedDefault;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return str_(traceEngineI18nObject.i18nId, values);
|
|
120
|
-
}, new Set());
|
|
174
|
+
this.localizeObject(str_, model);
|
|
121
175
|
}
|
|
122
176
|
}
|
|
123
177
|
}
|
|
@@ -4,6 +4,12 @@ export type SaneSyntheticLayoutShift = SyntheticLayoutShift & {
|
|
|
4
4
|
data: NonNullable<SyntheticLayoutShift["args"]["data"]>;
|
|
5
5
|
};
|
|
6
6
|
};
|
|
7
|
+
export type DevToolsIcuMessage = {
|
|
8
|
+
i18nId: string;
|
|
9
|
+
values: Record<string, string | number | {
|
|
10
|
+
__i18nBytes: number;
|
|
11
|
+
}>;
|
|
12
|
+
};
|
|
7
13
|
export const TraceProcessor: typeof TraceEngine.Processor.TraceProcessor;
|
|
8
14
|
export const TraceHandlers: typeof TraceEngine.Handlers.ModelHandlers;
|
|
9
15
|
export const Insights: typeof TraceEngine.Insights;
|
package/core/lib/trace-engine.js
CHANGED
|
@@ -4,6 +4,7 @@ import {polyfillDOMRect} from './polyfill-dom-rect.js';
|
|
|
4
4
|
|
|
5
5
|
/** @typedef {import('@paulirish/trace_engine').Types.Events.SyntheticLayoutShift} SyntheticLayoutShift */
|
|
6
6
|
/** @typedef {SyntheticLayoutShift & {args: {data: NonNullable<SyntheticLayoutShift['args']['data']>}}} SaneSyntheticLayoutShift */
|
|
7
|
+
/** @typedef {{i18nId: string, values: Record<string, string|number|{__i18nBytes: number}>}} DevToolsIcuMessage */
|
|
7
8
|
|
|
8
9
|
polyfillDOMRect();
|
|
9
10
|
|
package/package.json
CHANGED