Skip to content

Instantly share code, notes, and snippets.

@daviddarke
Last active November 15, 2020 01:56
Show Gist options
  • Select an option

  • Save daviddarke/711eaf6cb4fffa5968cdf6c5ff810a93 to your computer and use it in GitHub Desktop.

Select an option

Save daviddarke/711eaf6cb4fffa5968cdf6c5ff810a93 to your computer and use it in GitHub Desktop.

Revisions

  1. daviddarke revised this gist May 1, 2019. 1 changed file with 12 additions and 11 deletions.
    23 changes: 12 additions & 11 deletions coverage-css.js
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,15 @@
    var fs = require('fs');
    // Check that required variables exist
    if( ( process.argv[2] == undefined ) || ( process.argv[3] == undefined ) ){
    console.log('Missing variables')
    console.log('1, local path to coverage file. 2, url to target css file')
    return;
    }

    (async () => {

    var fs = require("fs");
    // Get content from file
    var contents = fs.readFileSync("Coverage.json");
    var contents = fs.readFileSync( process.argv[2] );
    // Define to JSON type
    var jsonContent = JSON.parse(contents);

    @@ -13,23 +18,19 @@ var fs = require('fs');
    for (i = 0; i < jsonContent.length; i++) {

    // console.log( jsonContent[i] );
    if( jsonContent[i].url == process.argv[3] ){

    for (j = 0; j < jsonContent[i].ranges.length; j++) {
    console.log( 'Critical CSS from: ' + process.argv[3] );

    // console.log( 'start' );
    // console.log( jsonContent[i].ranges[j].start );
    // console.log( 'end' );
    // console.log( jsonContent[i].ranges[j].end );

    if( jsonContent[i].url == 'https://atomicsmash-725c.kxcdn.com/wp-content/themes/atomicsmash5/assets/css/screen.css?ver=190222-114139' ){
    for (j = 0; j < jsonContent[i].ranges.length; j++) {
    slices.push( jsonContent[i].text.slice( jsonContent[i].ranges[j].start, jsonContent[i].ranges[j].end ));
    }
    }

    // jsonContent[i].text;

    }

    console.log('------------------------------------------------')
    console.log(slices.join(''));
    console.log('------------------------------------------------')

    })();
  2. daviddarke renamed this gist May 1, 2019. 1 changed file with 0 additions and 0 deletions.
  3. daviddarke created this gist Apr 28, 2019.
    35 changes: 35 additions & 0 deletions generate-above-the-fold-css-from-coverage-data.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    var fs = require('fs');

    (async () => {

    var fs = require("fs");
    // Get content from file
    var contents = fs.readFileSync("Coverage.json");
    // Define to JSON type
    var jsonContent = JSON.parse(contents);

    const slices = [];

    for (i = 0; i < jsonContent.length; i++) {

    // console.log( jsonContent[i] );

    for (j = 0; j < jsonContent[i].ranges.length; j++) {

    // console.log( 'start' );
    // console.log( jsonContent[i].ranges[j].start );
    // console.log( 'end' );
    // console.log( jsonContent[i].ranges[j].end );

    if( jsonContent[i].url == 'https://atomicsmash-725c.kxcdn.com/wp-content/themes/atomicsmash5/assets/css/screen.css?ver=190222-114139' ){
    slices.push( jsonContent[i].text.slice( jsonContent[i].ranges[j].start, jsonContent[i].ranges[j].end ));
    }
    }

    // jsonContent[i].text;

    }

    console.log(slices.join(''));

    })();