webpack-spritesmith icon indicating copy to clipboard operation
webpack-spritesmith copied to clipboard

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

Open echolove38 opened this issue 5 years ago • 2 comments

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

echolove38 avatar Dec 21 '19 03:12 echolove38

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.

mixtur avatar Dec 21 '19 12:12 mixtur

@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...

fuchao2012 avatar Nov 11 '21 08:11 fuchao2012