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

styled-import

v0.3.5

Published

Extreme lightweight CSS parser for stealing rules from stylesheets (without adding the stylesheet to your bundle), to compose into Styled Components or anywhere else you might be doing CSS in JS.

Downloads

151

Readme

Styled Import

Extreme lightweight CSS parser for stealing rules from stylesheets (without adding the stylesheet to your bundle), to compose into Styled Components or anywhere else you might be doing CSS in JS.

npm package Babel Macro contributions welcome

Motivation

Working with global or 3rd party CSS creates constant challenges when implementing other CSS solutions. styled-import is meant to ease some of the pain by letting you steal styles from those stylesheets without needing to link or bundle or otherwise include the stylesheets themselves.

Note that this library currently operates as a Babel macro, replacing all references to styled-import calls at compile-time with the actual style declarations from the referenced stylesheets.

Installation

$ npm install -D styled-import

Dependencies

Styled Import currently runs only as a Babel macro, so be sure you have configured Babel to use babel-plugin-macros.

Here is one example of how to do that:

.babelrc

{
  "plugins": ["macros"]
}

NOTE: Macros are included in create-react-app 2 by default.

Use

./stylesheets/global.css

.button {
  color: blue;
}

./component.js

const styledImport = require('styled-import/macro')

const btnStyle = styledImport('./stylesheets/global.css', '.button')

console.log(btnStyle) // 'color: blue;'

Use with Styled Components

const styled = require('styled-components')
const styledImport = require('styled-import/macro')

const btnStyle = styledImport('./stylesheets/global.css', '.button')

const Button = styled.button`
  padding: 10px;
  ${btnStyle}
`

String composition works like inheritance/cascade:

const btnBlue = styledImport('./stylesheets/global.css', '.button-blue')

const Button = styled.button`
  color: green;
  padding: 10px;
  ${btnBlue}
`

// color: green is overridden by color: blue in btnBlue

Use with React or other CSS-in-JS

const btnStyle = styledImport.react('./stylesheets/global.css', '.button')

// btnStyle is now an object {'color': 'blue'} with camelCased properties, instead of a CSS string

Import from node_modules stylesheet

const btnStyle = styledImport('@org/stylesheets/global.css', '.button')

Import multiple styles

const [btnStyle, headerStyle] = styledImport('@org/styles/global.css', ['.button', '.header'])

const {button, header} = styledImport('@org/styles/global.css', {button: '.button', header: '.header'})

Import nested styles

const cardBtnStyle = styledImport('./stylesheets/global.css', '.card .button')

Search selectors with regular expressions

const cardBtnStyle = styledImport('./stylesheets/global.css', /\.button/gi)

// returns an array of declarations from selectors that matched the regex (omit
g flag to return just the first match)

Test

Make sure you're on the development branch and then:

$ npm test

NOTE: Tests will only run in a git cloned repo. They are disabled in the published npm module.

Restrictions

  • This currently only works with static values. Dynamic arguments can/will break it. Some dynamic support is coming soon.
  • Better error handling coming soon!
  • Selectors passed as arguments must match stylesheet selectors exactly. Partial matches/regex matches coming soon.
  • There is no de-duplication or other optimizations at this time. Currently styled-import just copies out the rules from the classes. It does not import the whole stylesheet into your bundle. Optimizations coming soon.
  • See the warning at top -- this is experimental and untested in many environments! Production-ready version...you guessed it...is coming soon.