import puppeteer from 'puppeteer'; import fetch from 'node-fetch'; import assert from 'assert'; import {diff} from 'jest-diff'; import * as css from 'lightningcss'; let urls = [ 'https://getbootstrap.com/docs/5.1/examples/headers/', 'https://getbootstrap.com/docs/5.1/examples/heroes/', 'https://getbootstrap.com/docs/5.1/examples/features/', 'https://getbootstrap.com/docs/5.1/examples/sidebars/', 'https://getbootstrap.com/docs/5.1/examples/footers/', 'https://getbootstrap.com/docs/5.1/examples/dropdowns/', 'https://getbootstrap.com/docs/5.1/examples/list-groups/', 'https://getbootstrap.com/docs/5.1/examples/modals/', 'http://csszengarden.com', 'http://csszengarden.com/221/', 'http://csszengarden.com/219/', 'http://csszengarden.com/218/', 'http://csszengarden.com/217/', 'http://csszengarden.com/216/', 'http://csszengarden.com/215/' ]; let success = true; const browser = await puppeteer.launch(); const page = await browser.newPage(); for (let url of urls) { await test(url); } async function test(url) { console.log(`Testing ${url}...`); await page.goto(url); await page.waitForNetworkIdle(); // Snapshot the computed styles of all elements let elements = await page.$$('body *'); let computed = []; for (let element of elements) { let style = await element.evaluate(node => { let res = {}; let style = window.getComputedStyle(node); for (let i = 0; i < style.length; i++) { res[style.item(i)] = style.getPropertyValue(style.item(i)); } return res; }); for (let key in style) { style[key] = normalize(key, style[key]); } computed.push(style); } // Find stylesheets, load, and minify. let stylesheets = await page.evaluate(() => { return [...document.styleSheets].map(styleSheet => styleSheet.href).filter(Boolean); }); let texts = await Promise.all(stylesheets.map(async url => { let res = await fetch(url); return res.text(); })); let minified = texts.map((code, i) => { let minified = css.transform({ filename: 'test.css', code: Buffer.from(code), minify: true, targets: { chrome: 95 << 16 } }).code.toString(); console.log(new URL(stylesheets[i]).pathname, '–', code.length + ' bytes', '=>', minified.length + ' bytes'); return minified; }); await page.setCacheEnabled(false); // Disable the original stylesheets and insert a