|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const penthouse = require('penthouse'); |
| 4 | + |
| 5 | +function buildCriticalCSS(cb) { |
| 6 | + penthouse({ |
| 7 | + url: 'file:///' + __dirname + '/dist/index.html', // @todo: need to figure out dynamic port if serving page itself |
| 8 | + |
| 9 | + // the original css to extract critcial css from |
| 10 | + css: path.resolve(process.cwd(), 'dist/styleguide/css/pattern-lab.css'), |
| 11 | + |
| 12 | + // OPTIONAL params |
| 13 | + width: 1300, // viewport width |
| 14 | + height: 900, // viewport height |
| 15 | + |
| 16 | + // when true, will not filter out larger media queries |
| 17 | + keepLargerMediaQueries: true, |
| 18 | + |
| 19 | + // selectors to keep |
| 20 | + forceInclude: [ |
| 21 | + // '.my-selector', |
| 22 | + '.pl-c-body--theme-light', |
| 23 | + '.pl-c-body--theme-sidebar', |
| 24 | + '.pl-c-body--theme-sidebar .pl-c-viewport', |
| 25 | + '.pl-c-body--theme-density-compact', |
| 26 | + // /^\.regexWorksToo/ |
| 27 | + ], |
| 28 | + propertiesToRemove: [ |
| 29 | + '(.*)transition(.*)', |
| 30 | + 'cursor', |
| 31 | + 'pointer-events', |
| 32 | + '(-webkit-)?tap-highlight-color', |
| 33 | + '(.*)user-select', |
| 34 | + ], |
| 35 | + timeout: 30000, // ms; abort critical CSS generation after this timeout |
| 36 | + pageLoadSkipTimeout: 0, // ms; stop waiting for page load after this timeout (for sites with broken page load event timings) |
| 37 | + maxEmbeddedBase64Length: 1000, // characters; strip out inline base64 encoded resources larger than this |
| 38 | + userAgent: 'Penthouse Critical Path CSS Generator', // specify which user agent string when loading the page |
| 39 | + renderWaitTime: 1000, // ms; render wait timeout before CSS processing starts (default: 100) |
| 40 | + blockJSRequests: false, // set to false to load (external) JS (default: true) |
| 41 | + customPageHeaders: { |
| 42 | + 'Accept-Encoding': 'identity', // add if getting compression errors like 'Data corrupted' |
| 43 | + }, |
| 44 | + strict: false, // set to true to throw on CSS errors |
| 45 | + puppeteer: { |
| 46 | + getBrowser: undefined, // A function that resolves with a puppeteer browser to use instead of launching a new browser session |
| 47 | + }, |
| 48 | + }) |
| 49 | + .then(function(criticalCss) { |
| 50 | + fs.writeFileSync( |
| 51 | + path.resolve( |
| 52 | + process.cwd(), |
| 53 | + 'dist/styleguide/css/pattern-lab.critical.css' |
| 54 | + ), |
| 55 | + criticalCss |
| 56 | + ); |
| 57 | + cb(); |
| 58 | + }) |
| 59 | + .catch(err => { |
| 60 | + console.log(err); // handle any errors thrown |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +module.exports = { |
| 65 | + buildCriticalCSS, |
| 66 | +}; |
0 commit comments