@@ -26,11 +26,12 @@ public static async Task<string> GenerateAsync(IReadOnlyCollection<string> urls)
2626 await page . SetViewportSizeAsync ( viewport . Width , viewport . Height ) ;
2727
2828 var usedCss = await page . EvaluateAsync < string [ ] > (
29- """
29+ """
3030 async () => {
3131 const styleSheets = Array.from(document.styleSheets);
3232 const usedRules = new Set();
3333 const processedUrls = new Set();
34+ const mediaQueryRules = new Set();
3435
3536 const viewportHeight = window.innerHeight;
3637 const elements = document.querySelectorAll('*');
@@ -39,40 +40,6 @@ public static async Task<string> GenerateAsync(IReadOnlyCollection<string> urls)
3940 return rect.top < viewportHeight;
4041 });
4142
42- async function fetchExternalStylesheet(url) {
43- if (processedUrls.has(url)) return;
44- processedUrls.add(url);
45-
46- try {
47- const response = await fetch(url);
48- const text = await response.text();
49- const blob = new Blob([text], { type: 'text/css' });
50- const styleSheet = new CSSStyleSheet();
51- await styleSheet.replace(text);
52- return styleSheet;
53- } catch (e) {
54- console.error('Failed to fetch:', url, e);
55- return null;
56- }
57- }
58-
59- async function processStyleSheet(sheet) {
60- try {
61- if (sheet.href) {
62- const externalSheet = await fetchExternalStylesheet(sheet.href);
63- if (externalSheet) {
64- Array.from(externalSheet.cssRules).forEach(processRule);
65- }
66- }
67-
68- Array.from(sheet.cssRules).forEach(processRule);
69- } catch (e) {
70- if (sheet.href) {
71- console.error('CORS issue with:', sheet.href);
72- }
73- }
74- }
75-
7643 function processRule(rule) {
7744 switch (rule.type) {
7845 case CSSRule.STYLE_RULE:
@@ -85,9 +52,8 @@ function processRule(rule) {
8552 });
8653 break;
8754 case CSSRule.MEDIA_RULE:
88- if (window.matchMedia(rule.conditionText).matches) {
89- Array.from(rule.cssRules).forEach(processRule);
90- }
55+ // Always include the complete media query block
56+ mediaQueryRules.add(rule.cssText);
9157 break;
9258 case CSSRule.IMPORT_RULE:
9359 processStyleSheet(rule.styleSheet);
@@ -99,11 +65,45 @@ function processRule(rule) {
9965 }
10066 }
10167
68+ async function processStyleSheet(sheet) {
69+ try {
70+ if (sheet.href) {
71+ const externalSheet = await fetchExternalStylesheet(sheet.href);
72+ if (externalSheet) {
73+ Array.from(externalSheet.cssRules).forEach(processRule);
74+ }
75+ }
76+ Array.from(sheet.cssRules).forEach(processRule);
77+ } catch (e) {
78+ if (sheet.href) {
79+ console.error('CORS issue with:', sheet.href);
80+ }
81+ }
82+ }
83+
84+ async function fetchExternalStylesheet(url) {
85+ if (processedUrls.has(url)) return;
86+ processedUrls.add(url);
87+
88+ try {
89+ const response = await fetch(url);
90+ const text = await response.text();
91+ const blob = new Blob([text], { type: 'text/css' });
92+ const styleSheet = new CSSStyleSheet();
93+ await styleSheet.replace(text);
94+ return styleSheet;
95+ } catch (e) {
96+ console.error('Failed to fetch:', url, e);
97+ return null;
98+ }
99+ }
100+
102101 for (const sheet of styleSheets) {
103102 await processStyleSheet(sheet);
104103 }
105104
106- return Array.from(usedRules);
105+ // Combine regular rules and media queries
106+ return [...Array.from(usedRules), ...Array.from(mediaQueryRules)];
107107 }
108108 """ ) ;
109109
0 commit comments