Gathering detailed insights and metrics for webpack-spritesmith
Gathering detailed insights and metrics for webpack-spritesmith
Gathering detailed insights and metrics for webpack-spritesmith
Gathering detailed insights and metrics for webpack-spritesmith
spritesmith
Utility that takes images and creates a spritesheet with JSON sprite data
webpack-spritesmith-handlebars-helpers
Webpack plugin that converts set of images into a spritesheet and SASS/LESS/Stylus mixins(Fork version add cssHandlebarsHelpers)
grunt-spritesmith
Grunt task for converting a set of images into a spritesheet and corresponding CSS variables.
gulp.spritesmith
Convert a set of images into a spritesheet and CSS variables via gulp
npm install webpack-spritesmith
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
500 Stars
140 Commits
56 Forks
6 Watching
10 Branches
10 Contributors
Updated on 20 Nov 2024
Minified
Minified + Gzipped
JavaScript (96.65%)
Handlebars (2.69%)
Stylus (0.66%)
Cumulative downloads
Total Downloads
Last day
10.2%
2,527
Compared to previous day
Last week
1.2%
11,654
Compared to previous week
Last month
1.1%
49,922
Compared to previous month
Last year
-60.9%
505,797
Compared to previous year
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.
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:
1 2//webpack.config.js 3var path = require('path'); 4 5var SpritesmithPlugin = require('webpack-spritesmith'); 6 7module.exports = { 8 // ... 9 module: { 10 rules: [ 11 {test: /\.styl$/, use: [ 12 'style-loader', 13 'css-loader', 14 'stylus-loader' 15 ]}, 16 {test: /\.png$/, use: [ 17 'file-loader?name=i/[hash].[ext]' 18 ]} 19 ] 20 }, 21 resolve: { 22 modules: ["node_modules", "spritesmith-generated"] 23 }, 24 plugins: [ 25 new SpritesmithPlugin({ 26 src: { 27 cwd: path.resolve(__dirname, 'src/ico'), 28 glob: '*.png' 29 }, 30 target: { 31 image: path.resolve(__dirname, 'src/spritesmith-generated/sprite.png'), 32 css: path.resolve(__dirname, 'src/spritesmith-generated/sprite.styl') 33 }, 34 apiOptions: { 35 cssImageRef: "~sprite.png" 36 } 37 }) 38 ] 39 // ... 40}; 41 42
And then just use it
1 2//style.styl 3@import '~sprite.styl' 4 5.close-button 6 sprite($close) 7.open-button 8 sprite($open) 9
There are a few things to notice in config
resolve
contains location of where generated image isSo the way generated image is accessed from the generated API now must be specified manually.
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 globoptions
- 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
1 ... 2 css: [ 3 path.resolve(__dirname, 'src/spritesmith-generated/sprite.styl'), 4 [path.resolve(__dirname, 'src/spritesmith-generated/sprite.json'), { 5 format: 'json_texture' 6 }] 7 ]
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
{half: function (num) { return num/2; }
will add a handlebars helper that halves numbersspritesmithOptions
- 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 -
1 2 { 3 type: String, // determines which kind of source is this. May take one of the two values: 'retina' and 'normal' 4 normalName: String, //a full path to the corresponding normal source image 5 retinaName: String, //a full path to the corresponding retina source image 6 }
targetImage
- a full path to the generated retina imagecssImageRef
- a path by which generated image will be referenced in the APIWhen 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
1 2//webpack.config.js 3var templateFunction = function (data) { 4 var shared = '.ico { background-image: url(I) }' 5 .replace('I', data.sprites[0].image); 6 7 var perSprite = data.sprites.map(function (sprite) { 8 return '.ico-N { width: Wpx; height: Hpx; background-position: Xpx Ypx; }' 9 .replace('N', sprite.name) 10 .replace('W', sprite.width) 11 .replace('H', sprite.height) 12 .replace('X', sprite.offset_x) 13 .replace('Y', sprite.offset_y); 14 }).join('\n'); 15 16 return shared + '\n' + perSprite; 17}; 18 19module.exports = { 20 ... 21 plugins: [ 22 new SpritesmithPlugin({ 23 target: { 24 ... 25 css: [ 26 [path.resolve(__dirname, 'src/spritesmith-generated/sprite-1.css'), { 27 format: 'function_based_template' 28 }], 29 [path.resolve(__dirname, 'src/spritesmith-generated/sprite-2.css'), { 30 format: 'handlebars_based_template' 31 }] 32 ] 33 }, 34 customTemplates: { 35 'function_based_template': templateFunction, 36 'handlebars_based_template': path.resolve(__dirname, '../my_handlebars_template.handlebars') 37 }, 38 ... 39 }) 40 ] 41} 42
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.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/20 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
37 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More