@mizchi/vlmkit-capture 0.8.0 → 0.8.1
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.
- package/dist/batch-prescan.d.mts +51 -0
- package/dist/batch-prescan.mjs +66 -0
- package/dist/capture-config.d.mts +50 -0
- package/dist/capture-config.mjs +164 -0
- package/dist/capturer.d.mts +45 -0
- package/dist/capturer.mjs +94 -0
- package/dist/cloudflare-quick-actions.d.mts +114 -0
- package/dist/cloudflare-quick-actions.mjs +142 -0
- package/dist/crater-client.d.mts +159 -0
- package/dist/crater-client.mjs +362 -0
- package/dist/crater-smoke.d.mts +98 -0
- package/dist/crater-smoke.mjs +269 -0
- package/dist/crater-wasm.d.mts +65 -0
- package/dist/crater-wasm.mjs +132 -0
- package/{src/detection-types.ts → dist/detection-types.d.mts} +4 -2
- package/dist/detection-types.mjs +1 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.mjs +11 -0
- package/dist/playwright-analyzer.d.mts +38 -0
- package/dist/playwright-analyzer.mjs +117 -0
- package/dist/playwright-launch-error.d.mts +7 -0
- package/dist/playwright-launch-error.mjs +25 -0
- package/dist/prescanner.d.mts +39 -0
- package/dist/prescanner.mjs +61 -0
- package/dist/viewport-discovery.d.mts +114 -0
- package/dist/viewport-discovery.mjs +294 -0
- package/package.json +18 -6
- package/src/batch-prescan.ts +0 -115
- package/src/capture-config.ts +0 -219
- package/src/capturer.ts +0 -138
- package/src/cloudflare-quick-actions.ts +0 -304
- package/src/crater-client.ts +0 -537
- package/src/crater-smoke.ts +0 -336
- package/src/crater-wasm.ts +0 -260
- package/src/index.ts +0 -157
- package/src/playwright-analyzer.ts +0 -180
- package/src/playwright-launch-error.ts +0 -41
- package/src/prescanner.ts +0 -123
- package/src/viewport-discovery.ts +0 -455
|
@@ -1,455 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Viewport Discovery -- discover breakpoints from CSS and generate boundary-check viewports
|
|
3
|
-
*
|
|
4
|
-
* Extracts breakpoints from @media queries and generates viewport lists
|
|
5
|
-
* at boundary +/-1px + random samples within ranges (quickcheck-style).
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
// ---- Types ----
|
|
9
|
-
|
|
10
|
-
export interface Breakpoint {
|
|
11
|
-
value: number; // px
|
|
12
|
-
type: "min-width" | "max-width";
|
|
13
|
-
raw: string; // e.g. "(min-width: 768px)"
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface ResponsiveBreakpoint {
|
|
17
|
-
axis: "width";
|
|
18
|
-
op: "ge" | "gt" | "le" | "lt";
|
|
19
|
-
valuePx: number;
|
|
20
|
-
raw: string;
|
|
21
|
-
normalized: string;
|
|
22
|
-
guards: string[];
|
|
23
|
-
ruleCount: number;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** Where a viewport candidate originated. */
|
|
27
|
-
export type ViewportSource =
|
|
28
|
-
| "standard"
|
|
29
|
-
| "regex-boundary"
|
|
30
|
-
| "regex-sample"
|
|
31
|
-
| "crater-required"
|
|
32
|
-
| "crater-rule-map";
|
|
33
|
-
|
|
34
|
-
export interface ViewportSpec {
|
|
35
|
-
width: number;
|
|
36
|
-
height: number;
|
|
37
|
-
label: string;
|
|
38
|
-
reason: string; // why this viewport was chosen
|
|
39
|
-
/**
|
|
40
|
-
* Optional provenance for downstream reports — present when discovery
|
|
41
|
-
* went through `generateViewports` / the Crater discovery path. Older
|
|
42
|
-
* callers that build a plain spec without this field still validate.
|
|
43
|
-
*/
|
|
44
|
-
source?: ViewportSource;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export interface DiscoveryResult {
|
|
48
|
-
breakpoints: Breakpoint[];
|
|
49
|
-
responsiveBreakpoints: ResponsiveBreakpoint[];
|
|
50
|
-
viewports: ViewportSpec[];
|
|
51
|
-
/**
|
|
52
|
-
* Which backend supplied the viewport list. `"crater"` means the
|
|
53
|
-
* BiDi v0.18.0 viewport intelligence APIs returned a usable set;
|
|
54
|
-
* `"regex"` means the inline-CSS regex discovery did. `"hybrid"`
|
|
55
|
-
* means Crater seeded the list and regex contributed additional
|
|
56
|
-
* widths (or vice versa).
|
|
57
|
-
*/
|
|
58
|
-
backend: "regex" | "crater" | "hybrid";
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
type ViewportBreakpoint = Breakpoint | ResponsiveBreakpoint;
|
|
62
|
-
|
|
63
|
-
// ---- Breakpoint extraction ----
|
|
64
|
-
|
|
65
|
-
const MEDIA_PATTERN = /@media\s+([^{]+)\{/g;
|
|
66
|
-
const WIDTH_PATTERN = /\(\s*(min|max)-width\s*:\s*([\d.]+)(px|rem|em)\s*\)/g;
|
|
67
|
-
|
|
68
|
-
/** Extract all breakpoints from CSS text */
|
|
69
|
-
export function extractBreakpoints(css: string): Breakpoint[] {
|
|
70
|
-
const breakpoints = new Map<string, Breakpoint>(); // dedupe by key
|
|
71
|
-
|
|
72
|
-
for (const mediaMatch of css.matchAll(MEDIA_PATTERN)) {
|
|
73
|
-
const condition = mediaMatch[1].trim();
|
|
74
|
-
for (const widthMatch of condition.matchAll(WIDTH_PATTERN)) {
|
|
75
|
-
const type = `${widthMatch[1]}-width` as "min-width" | "max-width";
|
|
76
|
-
let value = parseFloat(widthMatch[2]);
|
|
77
|
-
const unit = widthMatch[3];
|
|
78
|
-
// rem/em → px (assume 16px base)
|
|
79
|
-
if (unit === "rem" || unit === "em") value *= 16;
|
|
80
|
-
value = Math.round(value);
|
|
81
|
-
|
|
82
|
-
const key = `${type}:${value}`;
|
|
83
|
-
if (!breakpoints.has(key)) {
|
|
84
|
-
breakpoints.set(key, { value, type, raw: `(${type}: ${value}px)` });
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return [...breakpoints.values()].sort((a, b) => a.value - b.value);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function extractInlineStyleCss(html: string): string {
|
|
93
|
-
const styleMatch = html.match(/<style[^>]*>([\s\S]*?)<\/style>/g);
|
|
94
|
-
if (!styleMatch) return "";
|
|
95
|
-
return styleMatch.map((s) => s.replace(/<\/?style[^>]*>/g, "")).join("\n");
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/** Extract breakpoints from HTML <style> */
|
|
99
|
-
export function extractBreakpointsFromHtml(html: string): Breakpoint[] {
|
|
100
|
-
return extractBreakpoints(extractInlineStyleCss(html));
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function readAttr(tag: string, name: string): string | undefined {
|
|
104
|
-
const re = new RegExp(`\\b${name}\\s*=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s"'>]+))`, "i");
|
|
105
|
-
const m = tag.match(re);
|
|
106
|
-
return m?.[1] ?? m?.[2] ?? m?.[3];
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function isStylesheetLink(tag: string): boolean {
|
|
110
|
-
if (!/^<link\b/i.test(tag)) return false;
|
|
111
|
-
const rel = readAttr(tag, "rel");
|
|
112
|
-
if (!rel) return false;
|
|
113
|
-
return rel.split(/\s+/).some((part) => part.toLowerCase() === "stylesheet");
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export function extractStylesheetHrefsFromHtml(html: string): string[] {
|
|
117
|
-
const linkTags = html.match(/<link\b[^>]*>/gi) ?? [];
|
|
118
|
-
return linkTags
|
|
119
|
-
.filter(isStylesheetLink)
|
|
120
|
-
.map((tag) => readAttr(tag, "href"))
|
|
121
|
-
.filter((href): href is string => !!href);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export function extractBreakpointsFromHtmlWithStylesheets(
|
|
125
|
-
html: string,
|
|
126
|
-
stylesheetTexts: string[],
|
|
127
|
-
): Breakpoint[] {
|
|
128
|
-
return extractBreakpoints([
|
|
129
|
-
extractInlineStyleCss(html),
|
|
130
|
-
...stylesheetTexts,
|
|
131
|
-
].join("\n"));
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function isResponsiveBreakpoint(
|
|
135
|
-
breakpoint: ViewportBreakpoint,
|
|
136
|
-
): breakpoint is ResponsiveBreakpoint {
|
|
137
|
-
return "axis" in breakpoint;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function normalizeResponsiveBreakpoint(
|
|
141
|
-
breakpoint: ViewportBreakpoint,
|
|
142
|
-
): ResponsiveBreakpoint {
|
|
143
|
-
if (isResponsiveBreakpoint(breakpoint)) {
|
|
144
|
-
const guards = [...new Set(breakpoint.guards)].sort((a, b) => a.localeCompare(b));
|
|
145
|
-
return {
|
|
146
|
-
axis: breakpoint.axis,
|
|
147
|
-
op: breakpoint.op,
|
|
148
|
-
valuePx: breakpoint.valuePx,
|
|
149
|
-
raw: breakpoint.raw,
|
|
150
|
-
normalized: breakpoint.normalized,
|
|
151
|
-
guards,
|
|
152
|
-
ruleCount: breakpoint.ruleCount ?? 1,
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const op = breakpoint.type === "min-width" ? "ge" : "le";
|
|
157
|
-
const operator = op === "ge" ? ">=" : "<=";
|
|
158
|
-
return {
|
|
159
|
-
axis: "width",
|
|
160
|
-
op,
|
|
161
|
-
valuePx: breakpoint.value,
|
|
162
|
-
raw: breakpoint.raw,
|
|
163
|
-
normalized: `(width ${operator} ${breakpoint.value}px)`,
|
|
164
|
-
guards: [],
|
|
165
|
-
ruleCount: 1,
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function compareResponsiveBreakpoint(
|
|
170
|
-
left: ResponsiveBreakpoint,
|
|
171
|
-
right: ResponsiveBreakpoint,
|
|
172
|
-
): number {
|
|
173
|
-
if (left.valuePx !== right.valuePx) return left.valuePx - right.valuePx;
|
|
174
|
-
const order = { lt: 0, le: 1, ge: 2, gt: 3 } as const;
|
|
175
|
-
if (order[left.op] !== order[right.op]) return order[left.op] - order[right.op];
|
|
176
|
-
return left.guards.join("|").localeCompare(right.guards.join("|"));
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
export function toResponsiveBreakpoints(
|
|
180
|
-
breakpoints: ViewportBreakpoint[],
|
|
181
|
-
): ResponsiveBreakpoint[] {
|
|
182
|
-
const merged = new Map<string, ResponsiveBreakpoint>();
|
|
183
|
-
|
|
184
|
-
for (const breakpoint of breakpoints) {
|
|
185
|
-
const normalized = normalizeResponsiveBreakpoint(breakpoint);
|
|
186
|
-
const key = [
|
|
187
|
-
normalized.axis,
|
|
188
|
-
normalized.op,
|
|
189
|
-
normalized.valuePx,
|
|
190
|
-
normalized.guards.join("&"),
|
|
191
|
-
].join(":");
|
|
192
|
-
const existing = merged.get(key);
|
|
193
|
-
if (existing) {
|
|
194
|
-
existing.ruleCount += normalized.ruleCount;
|
|
195
|
-
continue;
|
|
196
|
-
}
|
|
197
|
-
merged.set(key, { ...normalized });
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
return [...merged.values()].sort(compareResponsiveBreakpoint);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
export function mergeResponsiveBreakpoints(
|
|
204
|
-
...collections: ViewportBreakpoint[][]
|
|
205
|
-
): ResponsiveBreakpoint[] {
|
|
206
|
-
return toResponsiveBreakpoints(collections.flat());
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
export function extractResponsiveBreakpointsFromHtml(
|
|
210
|
-
html: string,
|
|
211
|
-
): ResponsiveBreakpoint[] {
|
|
212
|
-
return toResponsiveBreakpoints(extractBreakpointsFromHtml(html));
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
export function extractResponsiveBreakpointsFromHtmlWithStylesheets(
|
|
216
|
-
html: string,
|
|
217
|
-
stylesheetTexts: string[],
|
|
218
|
-
): ResponsiveBreakpoint[] {
|
|
219
|
-
return toResponsiveBreakpoints(
|
|
220
|
-
extractBreakpointsFromHtmlWithStylesheets(html, stylesheetTexts),
|
|
221
|
-
);
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
// ---- Viewport generation ----
|
|
225
|
-
|
|
226
|
-
export interface ViewportOptions {
|
|
227
|
-
height?: number; // default: 900
|
|
228
|
-
maxViewports?: number; // upper limit (cost control)
|
|
229
|
-
randomSamples?: number; // random samples within range (default: 0)
|
|
230
|
-
seed?: number; // random seed
|
|
231
|
-
includeStandard?: boolean; // include standard viewports (375, 1280, 1440) (default: true)
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
const STANDARD_VIEWPORTS: Array<{ width: number; label: string }> = [
|
|
235
|
-
{ width: 375, label: "mobile" },
|
|
236
|
-
{ width: 1280, label: "desktop" },
|
|
237
|
-
{ width: 1440, label: "wide" },
|
|
238
|
-
];
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* Generate viewport list from breakpoints (quickcheck-style).
|
|
242
|
-
*
|
|
243
|
-
* For each breakpoint:
|
|
244
|
-
* - boundary +1px: just after breakpoint activates
|
|
245
|
-
* - boundary -1px: just before breakpoint activates
|
|
246
|
-
* - (optional) random samples within range
|
|
247
|
-
*/
|
|
248
|
-
export function generateViewports(
|
|
249
|
-
breakpoints: ViewportBreakpoint[],
|
|
250
|
-
options: ViewportOptions = {},
|
|
251
|
-
): ViewportSpec[] {
|
|
252
|
-
const responsiveBreakpoints = toResponsiveBreakpoints(breakpoints);
|
|
253
|
-
const height = options.height ?? 900;
|
|
254
|
-
const maxViewports = options.maxViewports ?? 20;
|
|
255
|
-
const randomSamples = options.randomSamples ?? 0;
|
|
256
|
-
const includeStandard = options.includeStandard ?? true;
|
|
257
|
-
const seed = options.seed ?? 42;
|
|
258
|
-
|
|
259
|
-
const viewportMap = new Map<number, ViewportSpec>();
|
|
260
|
-
|
|
261
|
-
function add(width: number, label: string, reason: string, source: ViewportSource) {
|
|
262
|
-
if (width < 320 || width > 2560) return;
|
|
263
|
-
if (!viewportMap.has(width)) {
|
|
264
|
-
viewportMap.set(width, { width, height, label, reason, source });
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
// Standard viewports
|
|
269
|
-
if (includeStandard) {
|
|
270
|
-
for (const sv of STANDARD_VIEWPORTS) {
|
|
271
|
-
add(sv.width, sv.label, "standard", "standard");
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
// Boundary viewports for each breakpoint
|
|
276
|
-
for (const bp of responsiveBreakpoints) {
|
|
277
|
-
if (bp.op === "ge") {
|
|
278
|
-
add(bp.valuePx - 1, `below-${bp.valuePx}`, `${bp.raw} boundary-below`, "regex-boundary");
|
|
279
|
-
add(bp.valuePx, `at-${bp.valuePx}`, `${bp.raw} boundary-at`, "regex-boundary");
|
|
280
|
-
} else if (bp.op === "gt") {
|
|
281
|
-
add(bp.valuePx, `at-${bp.valuePx}`, `${bp.raw} boundary-at`, "regex-boundary");
|
|
282
|
-
add(bp.valuePx + 1, `above-${bp.valuePx}`, `${bp.raw} boundary-above`, "regex-boundary");
|
|
283
|
-
} else if (bp.op === "le") {
|
|
284
|
-
add(bp.valuePx, `at-${bp.valuePx}`, `${bp.raw} boundary-at`, "regex-boundary");
|
|
285
|
-
add(bp.valuePx + 1, `above-${bp.valuePx}`, `${bp.raw} boundary-above`, "regex-boundary");
|
|
286
|
-
} else {
|
|
287
|
-
add(bp.valuePx - 1, `below-${bp.valuePx}`, `${bp.raw} boundary-below`, "regex-boundary");
|
|
288
|
-
add(bp.valuePx, `at-${bp.valuePx}`, `${bp.raw} boundary-at`, "regex-boundary");
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
// Random samples within breakpoint ranges
|
|
293
|
-
if (randomSamples > 0 && responsiveBreakpoints.length > 0) {
|
|
294
|
-
const allWidths = [...new Set(responsiveBreakpoints.map((b) => b.valuePx))].sort((a, b) => a - b);
|
|
295
|
-
const ranges: Array<[number, number]> = [];
|
|
296
|
-
|
|
297
|
-
// Build ranges: [320, bp1], [bp1, bp2], ..., [bpN, 1920]
|
|
298
|
-
ranges.push([320, allWidths[0] - 1]);
|
|
299
|
-
for (let i = 0; i < allWidths.length - 1; i++) {
|
|
300
|
-
ranges.push([allWidths[i], allWidths[i + 1] - 1]);
|
|
301
|
-
}
|
|
302
|
-
ranges.push([allWidths[allWidths.length - 1], 1920]);
|
|
303
|
-
|
|
304
|
-
let s = seed;
|
|
305
|
-
const rand = () => { s = (s * 1664525 + 1013904223) & 0x7fffffff; return s / 0x7fffffff; };
|
|
306
|
-
|
|
307
|
-
for (const [lo, hi] of ranges) {
|
|
308
|
-
if (hi - lo < 2) continue;
|
|
309
|
-
for (let j = 0; j < randomSamples; j++) {
|
|
310
|
-
const w = Math.round(lo + rand() * (hi - lo));
|
|
311
|
-
add(w, `sample-${w}`, `random sample in [${lo}, ${hi}]`, "regex-sample");
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
// Sort by width and limit
|
|
317
|
-
const sorted = [...viewportMap.values()].sort((a, b) => a.width - b.width);
|
|
318
|
-
return sorted.slice(0, maxViewports);
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
/**
|
|
322
|
-
* Discover breakpoints from HTML and generate boundary-check viewports.
|
|
323
|
-
*/
|
|
324
|
-
export function discoverViewports(
|
|
325
|
-
html: string,
|
|
326
|
-
options: ViewportOptions = {},
|
|
327
|
-
): DiscoveryResult {
|
|
328
|
-
const breakpoints = extractBreakpointsFromHtml(html);
|
|
329
|
-
const responsiveBreakpoints = toResponsiveBreakpoints(breakpoints);
|
|
330
|
-
const viewports = generateViewports(responsiveBreakpoints, options);
|
|
331
|
-
return { breakpoints, responsiveBreakpoints, viewports, backend: "regex" };
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
// ---- Crater viewport intelligence ----
|
|
335
|
-
|
|
336
|
-
export interface CraterViewportSource {
|
|
337
|
-
getRequiredTestViewports(): Promise<{ viewports: Array<{ width: number; reason: string }> }>;
|
|
338
|
-
getCssRuleViewportMap?(viewportWidths?: number[]): Promise<{
|
|
339
|
-
rules: Array<{ activeAtWidths?: number[]; inactiveAtWidths?: number[] }>;
|
|
340
|
-
}>;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
function clampViewport(width: number): boolean {
|
|
344
|
-
return Number.isFinite(width) && width >= 320 && width <= 2560;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
function buildCraterViewport(
|
|
348
|
-
width: number,
|
|
349
|
-
height: number,
|
|
350
|
-
reason: string,
|
|
351
|
-
source: ViewportSource,
|
|
352
|
-
): ViewportSpec | null {
|
|
353
|
-
if (!clampViewport(width)) return null;
|
|
354
|
-
return {
|
|
355
|
-
width,
|
|
356
|
-
height,
|
|
357
|
-
label: `at-${width}`,
|
|
358
|
-
reason,
|
|
359
|
-
source,
|
|
360
|
-
};
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
/**
|
|
364
|
-
* Drive Crater v0.18.0 viewport intelligence (`getRequiredTestViewports`
|
|
365
|
-
* + optional `getCssRuleViewportMap`) to seed the viewport list with
|
|
366
|
-
* breakpoints the renderer itself determined are load-bearing for the
|
|
367
|
-
* current document. Falls back gracefully when either RPC fails.
|
|
368
|
-
*/
|
|
369
|
-
export async function discoverViewportsViaCrater(
|
|
370
|
-
client: CraterViewportSource,
|
|
371
|
-
options: ViewportOptions = {},
|
|
372
|
-
): Promise<{ viewports: ViewportSpec[]; widthsSeen: Set<number> }> {
|
|
373
|
-
const height = options.height ?? 900;
|
|
374
|
-
const widthsSeen = new Set<number>();
|
|
375
|
-
const viewports: ViewportSpec[] = [];
|
|
376
|
-
|
|
377
|
-
function pushIfNew(spec: ViewportSpec | null) {
|
|
378
|
-
if (!spec) return;
|
|
379
|
-
if (widthsSeen.has(spec.width)) return;
|
|
380
|
-
widthsSeen.add(spec.width);
|
|
381
|
-
viewports.push(spec);
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
try {
|
|
385
|
-
const required = await client.getRequiredTestViewports();
|
|
386
|
-
for (const v of required.viewports ?? []) {
|
|
387
|
-
pushIfNew(buildCraterViewport(v.width, height, v.reason || "crater required", "crater-required"));
|
|
388
|
-
}
|
|
389
|
-
} catch {
|
|
390
|
-
// ignored — the regex fallback will still run from discoverViewportsWithBackend.
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
if (client.getCssRuleViewportMap) {
|
|
394
|
-
try {
|
|
395
|
-
const map = await client.getCssRuleViewportMap();
|
|
396
|
-
const extraWidths = new Set<number>();
|
|
397
|
-
for (const rule of map.rules ?? []) {
|
|
398
|
-
for (const w of rule.activeAtWidths ?? []) extraWidths.add(w);
|
|
399
|
-
for (const w of rule.inactiveAtWidths ?? []) extraWidths.add(w);
|
|
400
|
-
}
|
|
401
|
-
for (const width of extraWidths) {
|
|
402
|
-
pushIfNew(buildCraterViewport(width, height, "crater rule/viewport map", "crater-rule-map"));
|
|
403
|
-
}
|
|
404
|
-
} catch {
|
|
405
|
-
// ignored
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
return { viewports, widthsSeen };
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
/**
|
|
413
|
-
* Hybrid viewport discovery: prefer Crater v0.18.0 viewport intelligence
|
|
414
|
-
* when available, fall back to regex breakpoints for any widths Crater
|
|
415
|
-
* didn't surface, and tag each viewport with its `source`. The returned
|
|
416
|
-
* `backend` field tells downstream reports which path supplied the list.
|
|
417
|
-
*/
|
|
418
|
-
export async function discoverViewportsWithBackend(
|
|
419
|
-
html: string,
|
|
420
|
-
options: ViewportOptions & { craterClient?: CraterViewportSource | null } = {},
|
|
421
|
-
): Promise<DiscoveryResult> {
|
|
422
|
-
const breakpoints = extractBreakpointsFromHtml(html);
|
|
423
|
-
const responsiveBreakpoints = toResponsiveBreakpoints(breakpoints);
|
|
424
|
-
const regexViewports = generateViewports(responsiveBreakpoints, options);
|
|
425
|
-
|
|
426
|
-
const client = options.craterClient ?? null;
|
|
427
|
-
if (!client) {
|
|
428
|
-
return { breakpoints, responsiveBreakpoints, viewports: regexViewports, backend: "regex" };
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
const craterResult = await discoverViewportsViaCrater(client, options);
|
|
432
|
-
if (craterResult.viewports.length === 0) {
|
|
433
|
-
return { breakpoints, responsiveBreakpoints, viewports: regexViewports, backend: "regex" };
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
// Fold regex output in as fallback, keeping anything Crater didn't already cover.
|
|
437
|
-
const merged: ViewportSpec[] = [...craterResult.viewports];
|
|
438
|
-
let usedFallback = false;
|
|
439
|
-
for (const v of regexViewports) {
|
|
440
|
-
if (craterResult.widthsSeen.has(v.width)) continue;
|
|
441
|
-
merged.push(v);
|
|
442
|
-
if (v.source !== "standard") usedFallback = true;
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
const maxViewports = options.maxViewports ?? 20;
|
|
446
|
-
merged.sort((a, b) => a.width - b.width);
|
|
447
|
-
const limited = merged.slice(0, maxViewports);
|
|
448
|
-
|
|
449
|
-
return {
|
|
450
|
-
breakpoints,
|
|
451
|
-
responsiveBreakpoints,
|
|
452
|
-
viewports: limited,
|
|
453
|
-
backend: usedFallback ? "hybrid" : "crater",
|
|
454
|
-
};
|
|
455
|
-
}
|