Chrome Devtools Coverage: how to save or capture code used code?

后端 未结 8 1052
抹茶落季
抹茶落季 2020-12-14 05:50

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 b

相关标签:
8条回答
  • 2020-12-14 06:09

    I talked with the engineer who owns this feature. As of Chrome 64 there's still no way to export the results of a coverage analysis.

    Star issue #717195 to help the team prioritize this feature request.

    0 讨论(0)
  • 2020-12-14 06:17
    1. first of all you need to download and install "Google Chrome Dev".
    2. on Google chrome Dev go to Inspect element > Sources > Ctrl+shift+p
    3. Enter "coverage" and select "Start Instrumenting coverage and reload Page"
    4. Then use Export icon this will give you a json file.

    you can also visit : Chrome DevTools: Export your raw Code Coverage Data

    0 讨论(0)
  • 2020-12-14 06:19

    Chrome canary 73 can do it. You will need Windows or Mac OS. There is an export function (Down arrow icon) next to the record and clear buttons. You'll get a json file and then you can use that to programmatically remove the unused lines.

    0 讨论(0)
  • 2020-12-14 06:23

    More practical version based on Atoms.

    Improved to work without any files.

    PHP Sandbox http://sandbox.onlinephpfunctions.com/

    JSON Formater to be converted to 1line https://www.freeformatter.com/json-formatter.html#ad-output

    Unmify it https://unminify.com/

    $jsont = <<<'EOD'
    { "url":"test"} 
    EOD;
    
    $a = json_decode($jsont);
    $sText = $a->text;
    $sOut = "";
    foreach ($a->ranges as $iPos => $oR) {
        $sOut .= substr($sText, $oR->start, ($oR->end-$oR->start))." \n";
    }
    echo '<style rel="stylesheet" type="text/css">' . $sOut . '</style>';
    
    0 讨论(0)
  • 2020-12-14 06:23

    Here's a version that will keep media queries, based on Christopher Schiefer's:

    $jsont = <<<'EOD'
    { "url":"test"} 
    EOD;
    
    $a = json_decode($jsont);
    $sText = $a->text;
    preg_match_all('(@media(?>[^{]|(?0))*?{)', $sText, $mediaStartsTmp, PREG_OFFSET_CAPTURE);
    preg_match_all("/\}(\s|\\n|\\t)*\}/", $sText, $mediaEndsTmp, PREG_OFFSET_CAPTURE);
    $mediaStarts = empty($mediaStartsTmp) ? array() : $mediaStartsTmp[0];
    $mediaEnds = empty($mediaEndsTmp) ? array() : $mediaEndsTmp[0];
    $sOut = "";
    $needMediaClose = false;
    foreach ($a->ranges as $iPos => $oR) {
        if ($needMediaClose) { //you are in a media query
            //add closing bracket if you were in a media query and are past it
            if ($oR->start > $mediaEnds[0][1]) {
                $sOut .= "}\n";
                array_splice($mediaEnds, 0, 1);
                $needMediaClose = false;
            }
        }
        if (!$needMediaClose) {
            //remove any skipped media queries
            while (!empty($mediaEnds) && $oR->start > $mediaEnds[0][1]) {
                array_splice($mediaStarts, 0, 1);
                array_splice($mediaEnds, 0, 1);
            }
        }
        if (!empty($mediaStarts) && $oR->start > $mediaStarts[0][1]) {
           $sOut .= "\n" . $mediaStarts[0][0] . "\n";
           array_splice($mediaStarts, 0, 1);
           $needMediaClose = true;
        }
        $sOut .= mb_substr($sText, $oR->start, ($oR->end-$oR->start))." \n";
    }
    if ($needMediaClose) { $sOut .= '}'; }
    echo '<style rel="stylesheet" type="text/css">' . $sOut . '</style>';
    
    0 讨论(0)
  • 2020-12-14 06:25

    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();
    })();
    
    0 讨论(0)
提交回复
热议问题