Skip to content

fix error when referencing a css file directly #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ module.exports = async (url, {waitUntil = 'networkidle0'} = {}) => {
)
}

// If the response is a CSS file, return that file
// instead of running our complicated setup
const headers = response.headers()

if (headers['content-type'].includes('text/css')) {
return Promise.resolve(response.text())
}

const coverage = await page.coverage.stopCSSCoverage()

// Get all CSS generated with the CSSStyleSheet API
Expand Down
7 changes: 7 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ test('it finds inline styles - JS', async t => {
t.snapshot(actual)
})

test('it returns a direct link to a CSS file', async t => {
const actual = await extractCss(server.url + '/import-in-css.css')

t.true(actual.includes('.css-imported-with-css {}'))
t.snapshot(actual)
})

test('it rejects if the url has an HTTP error status', async t => {
server.get('/404-page', (req, res) => {
res.status(404).send()
Expand Down
6 changes: 6 additions & 0 deletions test/snapshots/index.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,9 @@ Generated by [AVA](https://ava.li).

`[x-extract-css-inline-style] { color: red; font-size: 12px; border-style: solid; }␊
[x-extract-css-inline-style] { border-color: blue; border-width: 1px; }`

## it returns a direct link to a CSS file

> Snapshot 1

'.css-imported-with-css {}'
Binary file modified test/snapshots/index.js.snap
Binary file not shown.