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

broccoli-sass-image-vars

v0.1.1

Published

Sass variables for images

Downloads

4

Readme

broccoli-sass-image-vars

Build Status Dependencies Status Coverage Status

This is a simple broccoli plugin for generating Sass variables for your images.

Installation

npm install --save-dev broccoli-sass-image-vars

Usage examples

Plugin's first argument may be either a string (path to the directory with images) or a single broccoli tree, or even an array of paths and broccoli trees.

In the first case, you don't even need to specify the url_prefix option — the passed string will be the prefix of image URLs (you can also specify the image_root option to modify this prefix):

Brocfile.js

var imageVars = require( 'broccoli-sass-image-vars' );

var imagesTree = imageVars( 'webpub/images', {
    // Select only png and svg files under webpub/images directory
    input: [ '**/*.png', '**/*.svg' ],

    // Include data URI for all svg images and preloader.png:
    inline: [ '**/*.svg', 'preloader.png' ],

    // Specify the output Sass file:
    output: 'scss_compiled/_images.scss',

    // This prefix will be removed from the each image URL
    image_root: 'webpub',
});

In your Sass file you can import compiled variables as follows:

@import "scss_compiled/images";

.preloader:after{
    ...
    // preloader.png variables:
    width: $preloader_width;
    height: $preloader_height;
    content: $preloader_data_url // url('data:image/png;base64,....')
}

.some-resizable-bg{
    display: block;
    height: 0;
    background: $resizable_url no-repeat; // url('/images/resizable.png')
    background-size: 100%;
    padding-bottom: $resizable_padding
}

The full list of variables for the one image file:

  • $filename_path — an URL string with path to the image
  • $filename_url — value for css background-image property.
  • $filename_data_url — the same as $filename_url, but includes data URI of image (note: variable exists only for the images are set in options.inline).
  • $filename_width — the width of the image in pixels.
  • $filename_height — the height of the image in pixels.
  • $filename_padding — the padding-bottom css property value for the resizable backgrounds (see example above).

In the case, when you pass to the plugin a broccoli tree, created by another plugin (broccoli-imagemin, for example), you must specify the URL prefix with the option url_prefix:

Brocfile.js

var imageMin = require( 'broccoli-imagemin' );
var imageVars = require( 'broccoli-sass-image-vars' );

var optimizedImagesTree = imageMin( 'webpub/unoptimized', {
    interlaced: true,
    optimizationLevel: 3,
    progressive: true,
    lossyPNG: false
});

// By default, the "input" option is set to '*.*':
var imagesTree = imageVars( [ 'webpub/images', optimizedImagesTree ], {
    // Specify the output Sass file:
    output: 'scss_compiled/_images.scss',

    // Specify the URL prefix for images:
    url_prefix: '/images',
});

You can also setup any of plugin's options before creating trees:

Brocfile.js

var imageVars = require( 'broccoli-sass-image-vars' );

// This prefix will be removed from the each image URL:
imageVars.image_root = 'webpub';
// Turn on the query string cache buster for the each image URL:
imageVars.cache_buster = true;
// Specify the output Sass file:
imageVars.output = 'scss_compiled/_images.scss';

var imagesTree = imageVars( 'webpub/images' );

Documentation

imageVars( inputTree, options )

imageVars( [ tree1, tree2, ... ], options )


options.output {String}

Location of the output file with Sass variables.


[options.input] {Array|String}

An array of globs or a simple glob string for image files, for which sass variables must be created.

Default value: '*.*'


[options.inline] {Array|String}

An array of globs or a simple glob string for images, for which variables with data URI must be added.

Default value: []


[options.url_prefix] {String|null}

A prefix for the image URLs in Sass variables. Required when among the inputTree arguments is present a broccoli tree, returned by another plugin.

Default value: null


[options.image_root] {String|null}

A prefix which must be cut out from the image URLs. Used only when the all inputTree arguments are strings.

Default value: null


[options.cache_buster] {Boolean}

If is set to true, the cache busting via URL query string will be used (will append to the each image URL a string of form ?%ctime%, where %ctime% is a timestamp of the file last modification date).

Default value: false

Tests

npm install
npm test

License

This project is distributed under the MIT license.