All Projects → mixtur → Webpack Spritesmith

mixtur / Webpack Spritesmith

Webpack plugin that converts set of images into a spritesheet and SASS/LESS/Stylus mixins

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Webpack Spritesmith

stylos
Webpack plugin to automatically generate and inject CSS utilities to your application
Stars: ✭ 60 (-87.18%)
Mutual labels:  webpack-plugin
Web Webpack Plugin
alternative for html-webpack-plugin
Stars: ✭ 318 (-32.05%)
Mutual labels:  webpack-plugin
Webpack Chrome Extension Reloader
🔥 Hot reloading while developing Chrome extensions with webpack 🔥
Stars: ✭ 365 (-22.01%)
Mutual labels:  webpack-plugin
Webpack Assets Manifest
This Webpack plugin will generate a JSON file that matches the original filename with the hashed version.
Stars: ✭ 269 (-42.52%)
Mutual labels:  webpack-plugin
Webpack Cdn Plugin
A webpack plugin that use externals of CDN urls for production and local node_modules for development
Stars: ✭ 306 (-34.62%)
Mutual labels:  webpack-plugin
Awesome Cms Core
Awesome CMS Core is an open source CMS built using ASP.Net Core & ReactJS with module seperation concern in mind and provide lastest trend of technology like .Net Core, React, Webpack, SASS, Background Job, Message Queue.
Stars: ✭ 352 (-24.79%)
Mutual labels:  webpack-plugin
swig-webpack-plugin
Render swig templates with webpack.
Stars: ✭ 12 (-97.44%)
Mutual labels:  webpack-plugin
Mini Css Extract Plugin
Lightweight CSS extraction plugin
Stars: ✭ 4,396 (+839.32%)
Mutual labels:  webpack-plugin
Filemanager Webpack Plugin
Copy, move, archive (zip/tar/tar.gz), delete files and directories before and after Webpack builds. Win32/Mac/*Nix supported
Stars: ✭ 310 (-33.76%)
Mutual labels:  webpack-plugin
Browser Sync Webpack Plugin
Easily use BrowserSync in your Webpack project.
Stars: ✭ 356 (-23.93%)
Mutual labels:  webpack-plugin
Webpack Virtual Modules
Webpack Virtual Modules is a webpack plugin that lets you create, modify, and delete in-memory files in a way that webpack treats them as if they were physically presented in the file system.
Stars: ✭ 286 (-38.89%)
Mutual labels:  webpack-plugin
Webpack Sentry Plugin
Webpack plugin to upload source maps to Sentry
Stars: ✭ 299 (-36.11%)
Mutual labels:  webpack-plugin
Webpack Extension Reloader
A upgrade from 🔥webpack-chrome-extension-reloader🔥, now on all browsers
Stars: ✭ 355 (-24.15%)
Mutual labels:  webpack-plugin
shared-library-webpack-plugin
Webpack plugin for library sharing at runtime between applications
Stars: ✭ 35 (-92.52%)
Mutual labels:  webpack-plugin
Moment Locales Webpack Plugin
Easily remove unused Moment.js locales with webpack
Stars: ✭ 396 (-15.38%)
Mutual labels:  webpack-plugin
ignore-emit-webpack-plugin
Prevents ignored files from being emitted during a Webpack build
Stars: ✭ 17 (-96.37%)
Mutual labels:  webpack-plugin
I18n Webpack Plugin
[DEPRECATED] Embed localization into your bundle
Stars: ✭ 320 (-31.62%)
Mutual labels:  webpack-plugin
Webpack Parallel Uglify Plugin
A faster uglifyjs plugin.
Stars: ✭ 456 (-2.56%)
Mutual labels:  webpack-plugin
Extract Text Webpack Plugin
[DEPRECATED] Please use https://github.com/webpack-contrib/mini-css-extract-plugin Extracts text from a bundle into a separate file
Stars: ✭ 4,071 (+769.87%)
Mutual labels:  webpack-plugin
Vuetify Loader
📦 A Webpack plugin for treeshaking Vuetify components and more
Stars: ✭ 356 (-23.93%)
Mutual labels:  webpack-plugin

npm

A webpack plugin that converts a set of images into a spritesheet and SASS/LESS/Stylus mixins, using spritesmith and spritesheet-templates

All ideas are shamelessly taken from gulp.spritesmith.

Example

Let's say you have the following folder structure

/
|-src
| |-ico
| | |-new.png
| | |-open.png
| | |-save.png
| | ...
| |-style.styl
| ...
|-webpack.config.js

Then you need to instantiate the plugin in the webpack config like this:

//webpack.config.js
var path = require('path');

var SpritesmithPlugin = require('webpack-spritesmith');

module.exports = {
    // ...
    module: {
        rules: [
            {test: /\.styl$/, use: [
                'style-loader',
                'css-loader',
                'stylus-loader'
            ]},
            {test: /\.png$/, use: [
                'file-loader?name=i/[hash].[ext]'
            ]}
        ]
    },
    resolve: {
        modules: ["node_modules", "spritesmith-generated"]
    },
    plugins: [
        new SpritesmithPlugin({
            src: {
                cwd: path.resolve(__dirname, 'src/ico'),
                glob: '*.png'
            },
            target: {
                image: path.resolve(__dirname, 'src/spritesmith-generated/sprite.png'),
                css: path.resolve(__dirname, 'src/spritesmith-generated/sprite.styl')
            },
            apiOptions: {
                cssImageRef: "~sprite.png"
            }
        })
    ]
    // ...
};


And then just use it


//style.styl
@import '~sprite.styl'

.close-button
    sprite($close)
.open-button
    sprite($open)

There are a few things to notice in config

  • file-loader used for generated image
  • resolve contains location of where generated image is
  • cssImageRef is specified as '~sprite.png'

So the way generated image is accessed from the generated API now must be specified manually.

Config

  • src - used to build a list of source images

    • cwd should be the closest common directory for all source images;
    • glob well... it is a glob
    • options - optional. These options are passed down to the packages that handle the globbing of images. (We use gaze, which passes them down to globule, which also passes them down to node-glob.)

    cwd and glob both will be passed directly to glob (and gaze in watch mode), then the resulting list of files will be used as a list of source images

  • target - generated files

    • image - the target image's filename. Can be interpolated with loader-utils. I would recommend to use file-loader for interpolation though.
    • css - can be one of the following
      • "full/path/to/spritesheet/api" - for example path.resolve(__dirname, 'src/spritesmith-generated/sprite.styl')

      • ["full/path/to/spritesheet/api1", "full/path/to/spritesheet/api2"],

      • ["full/path/to/spritesheet/api1", ["full/path/to/spritesheet/api2", spritesmithTemplatesOptions]] spritesmithTemplatesOptions - is the second argument here

        for example

            ...
            css: [
                path.resolve(__dirname, 'src/spritesmith-generated/sprite.styl'),
                [path.resolve(__dirname, 'src/spritesmith-generated/sprite.json'), {
                    format: 'json_texture'
                }]
            ]
        
  • apiOptions - optional

    • generateSpriteName - a function. Takes a full path to a source image file and expected to return name by which it will be referenced in API. Return value will be used as sprite.name for spritesheet-templates. Default behaviour is to use filename (without dirname and extension)
    • spritesheet_name, retina_spritesheet_name - passed to spritesheet-templates (retina_spritesheet_name only takes effect if apiOptions.retina is also specified)
    • cssImageRef - a path by which a generated image will be referenced in API. If target.image is interpolated, cssImageRef should be interpolated the same way too.
    • handlebarsHelpers - an object. Container for helpers to register to handlebars for our template
      • Each key-value pair is the name of a handlebars helper corresponding to its function
      • For example, {half: function (num) { return num/2; } will add a handlebars helper that halves numbers
      • Note that handlebarsHelpers is global. If you have multiple instances of SpritesmithPlugin, helpers defined later will override helpers defined earlier.
  • spritesmithOptions - optional. Options for spritesmith

  • retina - optional, when specified, uses retina capabilities of spritesheet-templates. Can be either a suffix string (like '@2x') or an object consisting of three fields:

    • classifier - Function that allows to say which source is for retina spritesheet and which is not. Will be called with full path to source file, and should return an object of this format -
          {
              type: String, // determines which kind of source is this. May take one of the two values: 'retina' and 'normal'
              normalName: String, //a full path to the corresponding normal source image
              retinaName: String, //a full path to the corresponding retina source image
          }
      
    • targetImage - a full path to the generated retina image
    • cssImageRef - a path by which generated image will be referenced in the API

    When used as a suffix string it applies to source files, a filename for retina spritesheet image and cssImageRef

    apiOptions.generateSpriteName will be applied to normalName returned by retina.classifier

  • customTemplates - optional. An object with keys and values corresponding to format names and template descriptions respectively. Template description can be either a path/to/handlebars/template/file or a template function

    You can use templates registered here as format in "target.css"

    For example you can write something like this

    //webpack.config.js
    var templateFunction = function (data) {
        var shared = '.ico { background-image: url(I) }'
            .replace('I', data.sprites[0].image);
    
        var perSprite = data.sprites.map(function (sprite) {
            return '.ico-N { width: Wpx; height: Hpx; background-position: Xpx Ypx; }'
                .replace('N', sprite.name)
                .replace('W', sprite.width)
                .replace('H', sprite.height)
                .replace('X', sprite.offset_x)
                .replace('Y', sprite.offset_y);
        }).join('\n');
    
        return shared + '\n' + perSprite;
    };
    
    module.exports = {
        ...
        plugins: [
            new SpritesmithPlugin({
                target: {
                    ...
                    css: [
                        [path.resolve(__dirname, 'src/spritesmith-generated/sprite-1.css'), {
                            format: 'function_based_template'
                        }],
                        [path.resolve(__dirname, 'src/spritesmith-generated/sprite-2.css'), {
                            format: 'handlebars_based_template'
                        }]
                    ]
                },
                customTemplates: {
                    'function_based_template': templateFunction,
                    'handlebars_based_template': path.resolve(__dirname, '../my_handlebars_template.handlebars')
                },
                ...
            })
        ]
    }
    
    
  • logCreatedFiles optional. When set to true will console.log a list of created files.

This scary readme file is a cry for help. If someone can improve it please do. Also the config itself is terrible, it could also use some improvement. I welcome any reasonable suggestions. Thank you.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].