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

postcss-retina-bg-img

v5.0.1

Published

Automatically add retina background images

Downloads

2,470

Readme

postcss-retina-bg-img

CI npm version

Automatically add retina background images to CSS selectors

Install

With npm do:

npm install postcss-retina-bg-img --save

What does it do?

The idea of this plugin is to detect any background images in your CSS and, if a retina version of the same file is found, to automatically add that file within a retina-appropriate media query.

Input

.foo {
  background: url('/assets/images/foo.png');
}

.bar {
  background-image: url('/assets/images/bar.png');
}

Output

.foo {
  background: url('/assets/images/foo.png');
}

@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
  .foo {
    background-image: url('/assets/images/[email protected]');
  }
}

.bar {
  background-image: url('/assets/images/bar.png');
}

@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
  .bar {
    background-image: url('/assets/images/[email protected]');
  }
}

API

const postcss = require('postcss');
const retinaBgImage = require('postcss-retina-bg-img');

const options = {
  retinaSuffix: '...',
  mediaQuery: '...',
  assetDirectory: '...',
  includeFileExtensions: [ '...' ],
  logMissingImages: true,
};

return postcss([ retinaBgImage(options) ]).process(input);

options

retinaSuffix (optional)

Type: string || string[] Default: @2x

The possible retina suffixes to find a retina image with. For example, if the background image is foo.png, and you've specified @2x as the retinaSuffix, then the file [email protected] would be used as the retina version. Note that the retina media query is only added if the file actually exists; if you don't have a [email protected] then this plugin won't change a thing.

If an array of suffixes is provided, then any of the given strings will be matched against. If you have a situation where multiple files could satisfy the settings, such as retinaSuffix: ['@2x', '_2x'] with the following files:

foo.png
[email protected]
foo_2x.png

Then the first one in the retinaSuffix array will be used.

mediaQuery (optional)

Type: string Default: (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi)

The media query to use to target retina displays. The default should be sufficient, but if you need something custom, feel free to provide your own.

assetDirectory (required)

Type: string

The path to your asset directory. This should be the location on the filesystem that your files are served from.

For example, if your CSS links to an image like so:

.foo {
  background-image: url('/assets/images/foo.png');
}

then the assetDirectory should be the absolute path on the filesystem to wherever assets lives. This is necessary because this plugin will only add media queries for files that actually exist; to determine their existence, we must be able to actually find the file.

includeFileExtensions (optional)

Type: string[] Default ['png', 'jpg']

The file extensions to act on. Without a whitelist of file types, you'll end up checking for retina versions of svg files and the like, which you may not actually want to act on. If you need to check anything other than png or jpg files, simply define an array of file extensions here.

logMissingImages (optional)

Type: boolean Default true

By default the plugin will report warnings if it encountered an image without a corresponding retina version. If you wish to turn off logging you can set this option to false.

Usage

See the PostCSS documentation for examples for your environment.

Gotchas

A few things to note when using this plugin:

  • Media queries are only added if the retina version of the file is found. Make sure your retina images follow some kind of pattern so that they can be located based on the name of the non-retina file.

  • Selectors with a background image that are inside a media query already will create a new, combined media query to target both the existing one and retina displays. For example, the following should happen:

    /* Input */
    .foo {
      background: url('/assets/images/foo-mobile.png');
    }
    
    @media (min-width: 600px) {
      .foo {
        background: url('/assets/images/foo.png');
      }
    }
    /* Output */
    .foo {
      background: url('/assets/images/foo-mobile.png');
    }
    
    @media (min-width: 600px) {
      .foo {
        background: url('/assets/images/foo.png');
      }
    }
    
    @media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
      .foo {
        background-image: url('/assets/images/[email protected]');
      }
    }
    
    @media (-webkit-min-device-pixel-ratio: 2) and (min-width: 600px), (min-resolution: 192dpi) and (min-width: 600px) {
      .foo {
        background-image: url('/assets/images/[email protected]');
      }
    }

    If this does not seem to be working correctly, please file a ticket so I can make the rule combination more robust.

Contributing

Pull requests are welcome. If you add functionality, then please add unit tests to cover it.

License

MIT © Alex LaFroscia