Giter Club home page Giter Club logo

image-sprite-webpack-plugin's Introduction

image-sprite-webpack-plugin

npm

A webpack plugin that generates spritesheets from your stylesheets.

Input

  • width, height and background properties are mandatory to create a sprite.
  • Shorthand properties are recommended.
h1.logo {
    width: 240px;
    height: 80px;
    background: url(./img/logo.png) no-repeat 0 0;
    font-size: 0;
}

span.itemPicture {
    display: inline-block;
    background-image: url(./img/item-picture.png);
    background-repeat: no-repeat;
    background-position: 0 0;
    width: 36px;
    height: 36px;
}

Output

h1.logo {
  width: 240px;
  height: 80px;
  background: url(/css/sprite.png) no-repeat 0 -20px;
  /* background: url(/img/logo.png) no-repeat 0 0 */
  font-size: 0;
}

span.itemPicture {
  display: inline-block;
  background-image: url(/css/sprite.png);
  /* background-image: url(/img/item-picture.png) */
  background-repeat: no-repeat;
  background-position: -100px -90px;
  /* background-position: 0 0 */
  width: 36px;
  height: 36px;
}

Install

npm install -D image-sprite-webpack-plugin

Example

Production build

const ImageSpritePlugin = require('image-sprite-webpack-plugin');

new ImageSpritePlugin({
    commentOrigin: false,
    compress: true,
    extensions: ['gif', 'png'],
    indent: '',
    log: true,
    //outputPath: './public',
    outputFilename: 'css/sprite-[hash].png',
    padding: 10,
    suffix: '' + Date.now() // do not need to use it with a outputFilename's [hash].
})
git clone this repository
cd examples/webpack4-cssloader-single-chunk
npm i
npm run build

Webpack dev server

const ImageSpritePlugin = require('image-sprite-webpack-plugin');

new ImageSpritePlugin({
    commentOrigin: true,
    compress: false,
    extensions: ['gif', 'png'],
    indent: '  ',
    log: true,
    //outputPath: './public',
    outputFilename: 'css/sprite.png',
    padding: 10,
    suffix: ''
})
git clone this repository
cd examples/webpack4-cssloader-single-chunk
npm i
npm start

More Examples

NPM Commands in Examples

Each example has the following command.

  • Creates a production build.

     npm run build
  • Starts webpack-dev-server

     npm start
  • Creates a production build with node inspector. (with chrome://inspect/#devices)

     npm run build-debug
  • Starts webpack-dev-server with node inspector. (with chrome://inspect/#devices)

     npm run start-debug

Dependency

  • This plugin requires css-loader to handle CssModule.
  • It is also recommended that you use a file-loader to automatically move and access image resource files.

Compatibility

Options

Name Type Default Description
commentOrigin {Boolean} false Shows the original image resource url with a comment. image
compress {Boolean} false Compress the output css
extensions {Array} ['png', 'jpg', 'jpeg', 'gif'] File extensions to be converted with the spritesheets
indent {String} ' ' (2 spaces) The indentation for css output source
log {Boolean} true Enable/disable message logging
outputPath {String} webpack config's outputPath Describes spritesheets file's output path
outputFilename {String} '/sprite/sprite-[hash].png' A sprite image filename
padding {Number} 0 Padding to use between images
suffix {String} '' A suffix for outputFilename

Bug Report

If you find a bug, please report to us posting issues on GitHub.

License

image-sprite-webpack-plugin is released under the MIT license.

Copyright (c) 2018 NAVER Corp.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

image-sprite-webpack-plugin's People

Contributors

hwshim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

image-sprite-webpack-plugin's Issues

automatic image size detection

I think a good idea when not need to define the element width and height, but this plugin detect the used images size automatically.

The extensions option does not work

Hello!

When I set the extensions option as extensions: ['png'] or empty array extensions: [], the plugin tries to process all image formats.

When:

    new ImageSpritePlugin({
      extensions: ['png'],
    }),

I get an error for jpg file:

 92% additional asset processing imageSpriteWebpackPlugin[image-sprite] ✖ 'url(../images/blocks/header/bg-image.jpg)' skipped. Please use 'background-position' as '0 0'
[image-sprite] ⚠ Shorthand properties preferred : background-image: url('../images/blocks/contacts/map.png')

package.json:

    "webpack": "^4.26.1",
    "image-sprite-webpack-plugin": "^0.2.4",

It would also be very convenient to be able to specify in the option a mask or a regular expression for the files to be processed. For example:

match: ['**/sprite/*.png'],

To process png files only from a specific directory.

Thanks, have a nice day.

deprecation warning in the log

Hi! I use this plugin with webpack 5 and works fine but I give a message in the log:

(node:32) [DEP_WEBPACK_DEPRECATION_ARRAY_TO_SET] DeprecationWarning: Compilation.modules was changed from Array to Set (using Array method 'some' is deprecated)
(Use `node --trace-deprecation ...` to show where the warning was created)

Can you help me?

use diferent input file for sprite

Hello, I'm trying to use your plugin, but I don't find a way for diferent input file(for example sprite_input.css where I'll have only the icons I want to be in the sprite) and the plugin generates me many errors becaouse it's start working with .svg images that I have in my main css.
My configuration is:
npm -v: 5.6.0
webpack -v: 4.5.0
webpack.config.js:

var path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const autoprefixer = require('autoprefixer');
const ImageSpritePlugin = require('image-sprite-webpack-plugin');
module.exports = {
	mode: 'development',
	entry: './account.index.js',
	output: {
		path: path.resolve(__dirname, 'js'),
		filename: 'webpack.bundle.js',
		publicPath: './js'
	},
	plugins: [
		new MiniCssExtractPlugin({
			filename: '../assets/account/styles/style_account.css'
		}),
		new ImageSpritePlugin({
			commentOrigin: true,
			compress: false,
			extensions: ['png', 'jpg', 'jpeg'],
			indent: '  ',
			log: true,
			outputFilename: 'css/sprite-[hash].png',
			padding: 10,
			suffix: ''
		})
	],
	module: {
		rules: [{
			test: /\.(scss)$/,
			use: [
				MiniCssExtractPlugin.loader,
				{
					loader: 'css-loader',
					options: {
						url: false,
						sourceMap: true
					}
				},
				{
					loader: 'postcss-loader',
					options: {
						sourceMap: true,
						plugins: [
							new autoprefixer
						]
					}
				}, {
					loader: 'sass-loader',
					options: {
						sourceMap: true,
						precision: 8,
						indentType: 'tab',
						indentWidth: 1,
						outputStyle: 'expanded'
					},
				}
			],
		}, {
          	  test: /\.(png|gif|jpg)$/,
          	  loader: 'file-loader',
          	  options: {
          	        name: '../assets/account/styles/sprite.png'
                   }
            }]
	},
	watch: true
};

p.s. Sorry if my question is stupid, but I'm new in webpack and there are many thing that I still don't know :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.