npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

smoshit-webpack-plugin

v1.0.3

Published

Webpack plugin to compress images

Downloads

3

Readme

SmoshIt plugin for Webpack

npm js-standard-style

This is a simple plugin that uses smosh to compress all images in your project.

Install

npm install smoshit-webpack-plugin

Requires node >=4.0.0

Example Usage

var SmoshItPlugin = require('smoshit-webpack-plugin').default
// Or if using ES2015:
// import SmoshItPlugin from 'smoshit-webpack-plugin'

module.exports = {
  plugins: [
    // Make sure that the plugin is after any plugins that add images
    new SmoshItPlugin({
      disable: process.env.NODE_ENV !== 'production', // Disable during development
    })
  ]
}

Working with copy-webpack-plugin:

module.exports = {
  plugins: [
    // Copy the images folder and optimize all the images
    new CopyWebpackPlugin([{
      from: 'images/'
    }]),
    new SmoshItPlugin({ test: /\.(jpe?g|png|gif|svg)$/i })
  ]
}

Note the order of the plugins matters. SmoshItPlugin must be placed after CopyWebpackPlugin (or any other plugins that deal with images) in plugins array.

API

new SmoshItPlugin(options)

options.disable

type: Boolean default: false

When set to true it will disable the plugin entirely. This is useful for disabling the plugin during development, and only enabling it during production

options.test

type: RegExp or String or Array default: /.*/

This plugin will only run on files that match this test. This is similar to the webpack loader test option (but is not using the same implementation, so there might be major differences!). This can either be a RegExp object, a minimatch glob, a function which gets the filename and returns true if the file should be minified, or an array of any of them.

This can allow you to only run the plugin on specific files, or even include the plugin multiple times for different sets of images and apply different imagemin settings to each.

This will overwrite everything, including the externalImages option!

Example:

import SmoshItPlugin from 'smoshit-webpack-plugin'

module.exports = {
  plugins: [
    // Use the default settings for everything in /images/*
    new SmoshItPlugin({ test: 'images/**' }),
    // bump up the optimization level for all the files in my `bigpngs` directory
    new SmoshItPlugin({
      test: 'bigpngs/**',
    })
  ]
}

options.externalImages

type: Object default: { context: '.', sources: [], destination: null, fileName: null }

Include any external images (those not included in webpack's compilation assets) that you want to be parsed by SmoshIt. If a destination value is not supplied the files are optimized in place. You can optionally set either of these to a function which will be invoked at the last possible second before optimization to grab files that might not exist at the time of writing the config (see #37 for more info).

The paths will work based on the webpack's (and this plugin's) context option, so in the following example, the files will be read from ./src/images/**/*.png and will be written to .src/public/images/**/*.png Context only applies to the sources array.

Example:

import SmoshItPlugin from 'smoshit-webpack-plugin'
import glob from 'glob'

module.exports = {
  plugins: [
    new SmoshItPlugin({
      externalImages: {
        context: 'src', // Important! This tells the plugin where to "base" the paths at
        sources: glob.sync('src/images/**/*.png'),
        destination: 'src/public/images',
        fileName: '[path][name].[ext]' // (filePath) => filePath.replace('jpg', 'webp') is also possible
      }
    })
  ]
}

options.minFileSize

type: Integer default: 0

Only apply to images that are larger than this value in bytes.

options.maxFileSize

type: Integer default: Infinity

Only apply to images that are smaller than or equal-to this value in bytes.

This and minFileSize together can be used to include WebpackSmoshItPlugin multiple times with multiple configs on different file sizes.

Example:

import SmoshItPlugin from 'smoshit-webpack-plugin'
import glob from 'glob'

module.exports = {
  plugins: [
    new SmoshItPlugin({
      maxFileSize: 10000, // Only apply this one to files equal to or under 10kb
    }),
    new SmoshItPlugin({
      minFileSize: 10000, // Only apply this one to files over 10kb
    })
  ]
}

options.cacheFolder

type: String default: ''

Cache already minified images into a cacheFolder. On next run plugin will check for the cached images first. If cached image exists it will simply use that one. Otherwise image will be optimised and written to the cacheFolder for later builds.

Note: This is a very simple cache implementation, it WILL NOT intelligently clear the cache if you update the options in this plugin. There also might be significantly more files in the cache than you have images, this is normal, and a side-effect of how I'm deferring to smoshit to determine if a file is an image or not. It can be prevented by setting a good test regex.

Example:

import resolve from 'path'
import SmoshItPlugin from 'smoshit-webpack-plugin'

module.exports = {
  plugins: [
    new SmoshItPlugin({
      cacheFolder: resolve('./cache'), // use existing folder called cache in the current dir
    })
  ]
}

options.onlyUseIfSmaller

type: Boolean default: false

If set to true, this plugin will use the original image if the optimization process makes it larger.

true used to be the default behavior in version 2 of this plugin!

License

MIT Copyright (c) Kuzmin Pavel