npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

extract-css-core

v3.0.0

Published

Extract all CSS from a given url, both server side and client side rendered.

Downloads

20,811

Readme

NPM Version Node.js CI

Usage

const extractCss = require('extract-css-core')

const css = await extractCss('https://www.projectwallace.com')
//=> html{font-size:100%} etc.

Or, if you want more details:

const entries = await extractCss('https://www.projectwallace.com', {
	origins: 'include'
})

// entries will look something like this
[
	{
		href: 'https://www.projectwallace.com',
		type: 'link',
		css: '@font-face{font-display:swap;font-family:Teko;...'
	},
	{
		href: 'https://www.projectwallace.com/client/Seo.0f4fe72f.css',
		type: 'style',
		css: '.hero__text.svelte-qhblau a{color:var(--teal-400)}...'
	},
	{
		href: 'https://www.projectwallace.com/client/some-css-file.css',
		type: 'import',
		css: '.some-css {}'
	},
	{
		href: 'https://www.projectwallace.com',
		type: 'inline',
		css: '[x-extract-css-inline-style] { position: absolute; }'
	}
]

Installation

npm install extract-css-core
# or
yarn add extract-css-core

Motivation, solution and shortcomings

Motivation

Existing packages like get-css look at a server-generated piece of HTML and get all the <link> and <style> tags from it. This works fine for 100% server rendered pages 👍, but not for pages with CSS-in-JS styling and inline styles 👎.

Solution

This module uses an instance of Chromium to render a page. This has the benefit that most of the styles can be rendered, even when generated by JavaScript. The document.styleSheets API is used to get all styles, including CSS-in-JS. Lastly, a plain old document.querySelectorAll('[style]') finds all inline styling.

API

extractCss(url, [options])

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

Options

Type: Object

Default: {}

origins

Type: String

Default: exclude

Possible values: exclude, include

inlineStyles

Type: String

Default: include

Possible values: exclude, include

waitUntil

Type: String

Default: networkidle0

Can be any value as provided by the Puppeteer docs.

Related