From 4fe835e293359c471bd788a79928f40588ebbad5 Mon Sep 17 00:00:00 2001 From: Bart Veneman Date: Sun, 22 Mar 2020 20:31:27 +0100 Subject: [PATCH] add option to include css origins --- readme.md | 14 +++++++++++--- src/index.js | 16 ++++++++++++---- test/index.js | 20 ++++++++++++++++++++ test/snapshots/index.js.snap | Bin 636 -> 630 bytes 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/readme.md b/readme.md index 7613b43..be65fd7 100644 --- a/readme.md +++ b/readme.md @@ -51,7 +51,7 @@ is the power behind finding most of the CSS. Additionally, the ### extractCss(url, [options]) Extract CSS from a page. Returns a Promise that contains the CSS as a single -String. +String, or an Array of all entries found when the option `origins: 'include'` is passed. ### Options @@ -59,6 +59,14 @@ Type: `Object` Default: `{}` +#### origins + +Type: `String` + +Default: `exclude` + +Possible values: `exclude`, `include` + #### waitUntil Type: `String` @@ -70,6 +78,6 @@ Can be any value as provided by the ## Related +- [projectwallace.com/get-css](https://www.projectwallace.com/get-css) - Online version of this package - [Wallace CLI](https://github.com/bartveneman/wallace-cli) - Pretty CSS analytics in your terminal -- [get-css](https://github.com/cssstats/cssstats/tree/master/packages/get-css) - - The original get-css from CSSStats +- [get-css](https://github.com/cssstats/cssstats/tree/master/packages/get-css) - The original get-css from CSSStats diff --git a/src/index.js b/src/index.js index 47b4bf4..c57a075 100644 --- a/src/index.js +++ b/src/index.js @@ -13,7 +13,7 @@ InvalidUrlError.prototype = Error.prototype * @param {string} waitUntil https://github.com/puppeteer/puppeteer/blob/master/docs/api.md#pagegotourl-options * @returns {string} All CSS that was found */ -module.exports = async (url, {waitUntil = 'networkidle0'} = {}) => { +module.exports = async (url, {waitUntil = 'networkidle0', origins = 'exclude'} = {}) => { // Setup a browser instance const browser = await puppeteer.launch() @@ -103,8 +103,16 @@ module.exports = async (url, {waitUntil = 'networkidle0'} = {}) => { const css = links .concat(styleSheetsApiCss) .concat(inlineCss) - .map(({css}) => css) - .join('\n') - return Promise.resolve(css) + // Return the complete structure ... + if (origins === 'include') { + return Promise.resolve(css) + } + + // ... or return all CSS as a single String + return Promise.resolve( + css + .map(({css}) => css) + .join('\n') + ) } diff --git a/test/index.js b/test/index.js index 0fe24b6..548b4e5 100644 --- a/test/index.js +++ b/test/index.js @@ -84,6 +84,26 @@ test('it finds inline styles - JS', async t => { t.snapshot(actual) }) +test('it yields an array of entries when the `origins` option equals `include`', async t => { + const actual = await extractCss(server.url + '/kitchen-sink.html', { + origins: 'include' + }) + + t.true(Array.isArray(actual), 'Result should be an array when { origins: `include` }') + t.is(actual.length, 10) + + function isString(item) { + return typeof item === 'string' + } + + t.true(actual.every(item => isString(item.type) && ['link-or-import', 'style', 'inline'].includes(item.type))) + t.true(actual.every(item => isString(item.href))) + t.true(actual.every(item => item.href.startsWith('http://localhost:') && /\.(html|css)$/.test(item.href))) + t.true(actual.every(item => isString(item.css))) + + // Cannot snapshot due to changing port numbers in `create-test-server` +}) + test('it returns a direct link to a CSS file', async t => { const actual = await extractCss(server.url + '/import-in-css.css') diff --git a/test/snapshots/index.js.snap b/test/snapshots/index.js.snap index fd6263aa3ca98e773bfd4845261a6085c15b59d0..e3153e41f4a126827ed5ffe47daa2b7e5f3f85e8 100644 GIT binary patch delta 552 zcmV+@0@wZg1oi}fK~_N^Q*L2!b7*gLAa*he0sve5Xiy;MCNJ?jmaE3-elCR1&u^GJt?H69a=Vm%dz->LH)q4%{(cIS%Y) z1dA>OVqS4!&1Y4|AD?l_GkUqMRDcmIdK!q2#c9`1n`d2ryvBB7N5$NUS&U#&b!G+z zIZKB{^R4^aH%k847+hz0h7l}!0f=7}|4wkuO|T3(!2Fwk@$*THU{Mbi28O$yy!zks z#C}BbHhsOY$ghkMEV==R7x3Mxvs2;c(TOYLkBEK{%m@~}4#e7N%Xt<{S?v6i_rL02 zY1TJJu&6nID+2>F13TDnjI0cTOeRbYnYjh|MI{QQML8Nu5Vme+o^En+u^tdBXE&eRWrGAWN^)})sugOvKvKF8#i=Q}<(VZJAPoxDwOm9SSC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