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-bgimage

v2.1.3

Published

Plugin for PoscCSS to remove or keep only images by url() to optimize page loading

Downloads

1,078

Readme

postcss-bgimage

npm version Dependency Status Travis Build Status Appveyor Build Status Codacy Badge

PostCSS plugin which removes background-image properties with url() values or leaves only its. It allows to separate your layouts CSS from the images CSS to boost a speed of showing a page.

:boom: Note! The plugin only removes CSS declarations. Do not expect cleaning empty rules after that. Use special plugins for it (csso, cssnano and other).

Installation

$ npm install postcss-bgimage --save-dev

or

$ yarn add postcss-bgimage --dev

Usage

Any way of using PostCSS. For example, Gulp PostCSS:

const gulp = require('gulp');
const postcss = require('gulp-postcss');
const rename = require('gulp-rename');
const bgImage = require('postcss-bgimage');

function noBackgrounds() {
  return gulp.src('css/style.css')
    .pipe(postcss([bgImage({ mode: 'cutter' })]))
    .pipe(rename({ suffix: '.top' }))
    .pipe(gulp.dest('compiled/css'));
}

function onlyBackgrounds() {
  return gulp.src('css/style.css')
    .pipe(postcss([bgImage({ mode: 'cutterInvertor' })]))
    .pipe(rename({ suffix: '.bottom' }))
    .pipe(gulp.dest('compiled/css'));
}

exports.compile = gulp.parallel(noBackgrounds, onlyBackgrounds);

Result

Common usage

Input

/* style.css */

body {
    background-image: url(/path/to/img.png);
    font-family: Arial;
    padding: 20px 10px;
}

Output

/* style.top.css */

body {
    font-family: Arial;
    padding: 20px 10px;
}
/* style.bottom.css */

body {
    background-image: url(/path/to/img.png);
}

Usage with shortcut background

Input

/* style.css */

#some {
    display: flex;
    background: #f30 url(/path/to/img.png) 50% no-repeat;
    color: #fff;
}

Output

/* style.top.css */

#some {
    display: inline-block;
    background: #f30 50% no-repeat;
    color: #fff;
}
/* style.bottom.css */

#some {
    background-image: url(/path/to/img.png);
}

Using in nested at-rules

Input

/* style.css */

@media (min-width: 320px) {
    .title + .list > li {
        background: url(/path/to/img.png);
    }

    @supports (display: flex) {
        .panel {
            display: flex;
            background: url(/path/to/img.png);
        }
    }

    .panel {
        display: block;
    }
}

Output

/* style.top.css */

@media (min-width: 320px) {
    .title + .list > li {
    }

    @supports (display: flex) {
        .panel {
            display: flex;
        }
    }

    .panel {
        display: block;
    }
}
/* style.bottom.css */

@media (min-width: 320px) {
    .title + .list > li {
        background: url(/path/to/img.png);
    }

    @supports (display: flex) {
        .panel {
            background: url(/path/to/img.png);
        }
    }

    .panel {
    }
}

Ignore rules

To ignore some CSS rules use comment /* bgImage: ignore */. For example:

Input

/* style.css */

.some-rule {
    display: inline-block;
    /* bgImage: ignore */
    background: url(/path/to/img2.png);
}

@media (min-width: 320px) {
    /* bgImage: ignore */
    @supports (--color: red) {
        .some-rule {
            background: url(/path/to/img2.png);
            color: var(--color);
        }
    }

    .some-rule {
        display: inline-block;
        background: url(/path/to/img2.png);
    }
}

Output

/* style.top.css */

.some-rule {
    display: inline-block;
    /* bgImage: ignore */
    background: url(/path/to/img2.png);
}

@media (min-width: 320px) {
    /* bgImage: ignore */
    @supports (--color: red) {
        .some-rule {
            background: url(/path/to/img2.png);
            color: var(--color);
        }
    }

    .some-rule {
        display: inline-block;
        background: url(/path/to/img2.png);
    }
}
/* style.bottom.css */

.some-rule {
}

@media (min-width: 320px) {
    /* bgImage: ignore */
    @supports (--color: red) {
        .some-rule {
        }
    }

    .some-rule {
    }
}

Using of the resulting files in HTML

<!doctype html>

<html>
    <head>
        <title>postcss-bgimage test</title>
        <link rel="stylesheet" href="/compiled/css/style.top.css">
    </head>

    <body>
        <h1>postcss-bgimage test</h1>
        <p>Page content</p>
    </body>
</html>
<link rel="stylesheet" href="/compiled/css/style.bottom.css">

Or (in a case of small size) you can inject top CSS in <head> with <style> to decrease the number of blocking HTTP requests.

<!doctype html>

<html>
    <head>
        <title>postcss-bgimage test</title>
        <style>/* Content of your /compiled/css/style.top.css */</style>
    </head>

    <body>
        <h1>postcss-bgimage test</h1>
        <p>Page content</p>
    </body>
</html>
<link rel="stylesheet" href="/compiled/css/style.bottom.css">

Options

mode

(required) Mode of the plugin.

  • cutter - Removes background-image: properties with external references through url() from source CSS.
  • cutterInvertor - Removes all CSS rules without background-image and leaves only this property for other ones.

Test

$ npm test

or

$ yarn test

License

MIT © Alexander Antonov [email protected]