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 fd6263a..e3153e4 100644 Binary files a/test/snapshots/index.js.snap and b/test/snapshots/index.js.snap differ