The Coverage tool is good at finding used and unused code. However, there doesn't appear to be a way to save or export only the used code. Even hiding unused code would be helpful.
I'm attempting to reduce the amount of Bootstrap CSS in my application; the file is more than 7000 lines. The only way to get just the used code is to carefully scroll thru the file, look for green sections, then copy that code to a new file. It's time-consuming and unreliable.
Is there a different way? Chrome 60 does not seem to have added this functionality.
To make things simpler, Chrome only shows you the local resources (so you don't get confused on as to whether you are editing the local or the network resource). To save your changes, press CTRL + S when editing the file.
Press F12 (or Go to Tools > Web Developer > Network) Refresh the page having issues. Right-click on the loaded results. Select Save all as har.
You can do this with puppeteer
(async () => { const browser = await puppeteer.launch(); const page = await browser.newPage() //Start sending raw DevTools Protocol commands are sent using `client.send()` //First off enable the necessary "Domains" for the DevTools commands we care about const client = await page.target().createCDPSession() await client.send('Page.enable') await client.send('DOM.enable') await client.send('CSS.enable') const inlineStylesheetIndex = new Set(); client.on('CSS.styleSheetAdded', stylesheet => { const { header } = stylesheet if (header.isInline || header.sourceURL === '' || header.sourceURL.startsWith('blob:')) { inlineStylesheetIndex.add(header.styleSheetId); } }); //Start tracking CSS coverage await client.send('CSS.startRuleUsageTracking') await page.goto(`http://localhost`) // const content = await page.content(); // console.log(content); const rules = await client.send('CSS.takeCoverageDelta') const usedRules = rules.coverage.filter(rule => { return rule.used }) const slices = []; for (const usedRule of usedRules) { // console.log(usedRule.styleSheetId) if (inlineStylesheetIndex.has(usedRule.styleSheetId)) { continue; } const stylesheet = await client.send('CSS.getStyleSheetText', { styleSheetId: usedRule.styleSheetId }); slices.push(stylesheet.text.slice(usedRule.startOffset, usedRule.endOffset)); } console.log(slices.join('')); await page.close(); await browser.close(); })();
You can do this with Headless Chrome and puppeteer:
npm i puppeteer --save
csscoverage.js
and change localhost to point to your website.:
const puppeteer = require('puppeteer'); const util = require('util'); const fs = require("fs"); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.coverage.startCSSCoverage(); await page.goto('https://localhost'); // Change this const css_coverage = await page.coverage.stopCSSCoverage(); console.log(util.inspect(css_coverage, { showHidden: false, depth: null })); await browser.close(); let final_css_bytes = ''; let total_bytes = 0; let used_bytes = 0; for (const entry of css_coverage) { final_css_bytes = ""; total_bytes += entry.text.length; for (const range of entry.ranges) { used_bytes += range.end - range.start - 1; final_css_bytes += entry.text.slice(range.start, range.end) + '\n'; } filename = entry.url.split('/').pop(); fs.writeFile('./'+filename, final_css_bytes, error => { if (error) { console.log('Error creating file:', error); } else { console.log('File saved'); } }); } })();
node csscoverage.js
This will output all the CSS you're using into the separate files they appear in (stopping you from merging external libraries into your own code, like the other answer does).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With