Skip to content

Commit 0e93480

Browse files
authored
add option to include css origins (#32)
Co-authored-by: Bart Veneman <bartveneman@users.noreply.github.com>
1 parent cbd4bc8 commit 0e93480

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

readme.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,22 @@ is the power behind finding most of the CSS. Additionally, the
5151
### extractCss(url, [options])
5252

5353
Extract CSS from a page. Returns a Promise that contains the CSS as a single
54-
String.
54+
String, or an Array of all entries found when the option `origins: 'include'` is passed.
5555

5656
### Options
5757

5858
Type: `Object`
5959

6060
Default: `{}`
6161

62+
#### origins
63+
64+
Type: `String`
65+
66+
Default: `exclude`
67+
68+
Possible values: `exclude`, `include`
69+
6270
#### waitUntil
6371

6472
Type: `String`
@@ -70,6 +78,6 @@ Can be any value as provided by the
7078

7179
## Related
7280

81+
- [projectwallace.com/get-css](https://www.projectwallace.com/get-css) - Online version of this package
7382
- [Wallace CLI](https://github.com/bartveneman/wallace-cli) - Pretty CSS analytics in your terminal
74-
- [get-css](https://github.com/cssstats/cssstats/tree/master/packages/get-css) -
75-
The original get-css from CSSStats
83+
- [get-css](https://github.com/cssstats/cssstats/tree/master/packages/get-css) - The original get-css from CSSStats

src/index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ InvalidUrlError.prototype = Error.prototype
1313
* @param {string} waitUntil https://github.com/puppeteer/puppeteer/blob/master/docs/api.md#pagegotourl-options
1414
* @returns {string} All CSS that was found
1515
*/
16-
module.exports = async (url, {waitUntil = 'networkidle0'} = {}) => {
16+
module.exports = async (url, {waitUntil = 'networkidle0', origins = 'exclude'} = {}) => {
1717
// Setup a browser instance
1818
const browser = await puppeteer.launch()
1919

@@ -103,8 +103,16 @@ module.exports = async (url, {waitUntil = 'networkidle0'} = {}) => {
103103
const css = links
104104
.concat(styleSheetsApiCss)
105105
.concat(inlineCss)
106-
.map(({css}) => css)
107-
.join('\n')
108106

109-
return Promise.resolve(css)
107+
// Return the complete structure ...
108+
if (origins === 'include') {
109+
return Promise.resolve(css)
110+
}
111+
112+
// ... or return all CSS as a single String
113+
return Promise.resolve(
114+
css
115+
.map(({css}) => css)
116+
.join('\n')
117+
)
110118
}

test/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ test('it finds inline styles - JS', async t => {
8484
t.snapshot(actual)
8585
})
8686

87+
test('it yields an array of entries when the `origins` option equals `include`', async t => {
88+
const actual = await extractCss(server.url + '/kitchen-sink.html', {
89+
origins: 'include'
90+
})
91+
92+
t.true(Array.isArray(actual), 'Result should be an array when { origins: `include` }')
93+
t.is(actual.length, 10)
94+
95+
function isString(item) {
96+
return typeof item === 'string'
97+
}
98+
99+
t.true(actual.every(item => isString(item.type) && ['link-or-import', 'style', 'inline'].includes(item.type)))
100+
t.true(actual.every(item => isString(item.href)))
101+
t.true(actual.every(item => item.href.startsWith('http://localhost:') && /\.(html|css)$/.test(item.href)))
102+
t.true(actual.every(item => isString(item.css)))
103+
104+
// Cannot snapshot due to changing port numbers in `create-test-server`
105+
})
106+
87107
test('it returns a direct link to a CSS file', async t => {
88108
const actual = await extractCss(server.url + '/import-in-css.css')
89109

test/snapshots/index.js.snap

-6 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)