mixtur / webpack-spritesmith

Webpack plugin that converts set of images into a spritesheet and SASS/LESS/Stylus mixins
498 stars 56 forks source link

how to set max size, sometimes the generated sprite.png is too big #93

Open echolove38 opened 4 years ago

echolove38 commented 4 years ago

how to set max size, sometimes the generated sprite.png is too big

mixtur commented 4 years ago

I think you can't. Not via options anyway. If you could though it would be in spritesmithOptions. You can split your set of source images though, and create multiple instances of the plugin.

fuchao2012 commented 2 years ago

@mixtur same question here, and find a solution, just see see.

this line

https://github.com/mixtur/webpack-spritesmith/blob/be950b3ce0d7f76ae6cc7b02a23dcf8578315926/src/Plugin.js#L94

add a filter function as options of src

// Plugin.js
const allSourceImages = Object.values(sourceImagesByFolder)
                .reduce((allFiles, files) => [ ...allFiles, ...files ], [])
                .filter(x => !x.endsWith(path.sep))
+              .filter(x=> typeof this.options.src.filter === 'function' ? this.options.src.filter(x): true)

then use it in configs like

// webpack.config.js
new Spritesmith({
        src: {
            cwd: path.resolve(globals.ImageDirectory, dir),
            glob: '*.png',
            filter: (f)=>fs.statSync(f).size < 10 * 1024 // limit to less than 10kb
        },
        target: {image:{/*....*/}}
})

just solve my problem. @echolove38 have a try try...