Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: vanwagonet/img-loader
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 7a01460f1ac7eef26258e3e908caca9e6e8e13a7
Choose a base ref
...
head repository: vanwagonet/img-loader
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 25c4e7af11a7759647e3b846275b19c84b099e14
Choose a head ref

Commits on Jul 7, 2016

  1. add travis ci

    Andy VanWagoner committed Jul 7, 2016
    Copy the full SHA
    94000a8 View commit details
  2. be strict

    Andy VanWagoner committed Jul 7, 2016
    Copy the full SHA
    614d542 View commit details
  3. stinkin badges

    Andy VanWagoner committed Jul 7, 2016
    Copy the full SHA
    69b68d8 View commit details

Commits on Aug 1, 2016

  1. Copy the full SHA
    a1eb431 View commit details
  2. Merge pull request #8 from thetalecrafter/greenkeeper-mocha-3.0.0

    Update mocha to version 3.0.0 🚀
    vanwagonet authored Aug 1, 2016
    Copy the full SHA
    2c6d5ba View commit details

Commits on Aug 24, 2016

  1. Copy the full SHA
    e85505b View commit details

Commits on Aug 28, 2016

  1. Merge pull request #9 from thetalecrafter/greenkeeper-standard-8.0.0

    Update standard to version 8.0.0 🚀
    vanwagonet authored Aug 28, 2016
    Copy the full SHA
    cadfa4f View commit details

Commits on Jan 25, 2017

  1. Copy the full SHA
    1d84ffb View commit details

Commits on Jan 26, 2017

  1. Merge pull request #11 from thetalecrafter/greenkeeper/initial

    Add Greenkeeper badge 🌴
    vanwagonet authored Jan 26, 2017
    Copy the full SHA
    b6ff9fc View commit details

Commits on Feb 28, 2017

  1. Copy the full SHA
    2248170 View commit details

Commits on Mar 2, 2017

  1. Merge pull request #13 from thetalecrafter/greenkeeper/standard-9.0.0

    Update standard to the latest version 🚀
    vanwagonet authored Mar 2, 2017
    Copy the full SHA
    b00d2d6 View commit details

Commits on Mar 14, 2017

  1. rewrite for webpack 2

    vanwagonet committed Mar 14, 2017
    Copy the full SHA
    fcf2293 View commit details

Commits on Apr 7, 2017

  1. use safe-buffer in tests

    vanwagonet committed Apr 7, 2017
    Copy the full SHA
    976f6fc View commit details
  2. Copy the full SHA
    c52bc44 View commit details
  3. Merge pull request #16 from thetalecrafter/greenkeeper/standard-10.0.1

    chore(package): update standard to version 10.0.1
    vanwagonet authored Apr 7, 2017
    Copy the full SHA
    7c12eb6 View commit details

Commits on Oct 3, 2017

  1. Copy the full SHA
    0ded784 View commit details

Commits on Nov 1, 2017

  1. Copy the full SHA
    475901d View commit details

Commits on Nov 2, 2017

  1. Merge pull request #23 from thetalecrafter/greenkeeper/imagemin-svgo-…

    …6.0.0
    
    Update imagemin-svgo to the latest version 🚀
    vanwagonet authored Nov 2, 2017
    Copy the full SHA
    8eec78e View commit details
  2. Merge pull request #22 from thetalecrafter/greenkeeper/mocha-4.0.0

    Update mocha to the latest version 🚀
    vanwagonet authored Nov 2, 2017
    Copy the full SHA
    8176693 View commit details

Commits on Dec 27, 2017

  1. Copy the full SHA
    64abce0 View commit details
  2. Merge pull request #24 from thetalecrafter/greenkeeper/imagemin-mozjp…

    …eg-7.0.0
    
    Update imagemin-mozjpeg to the latest version 🚀
    vanwagonet authored Dec 27, 2017
    Copy the full SHA
    041a7b6 View commit details

Commits on Jan 18, 2018

  1. Copy the full SHA
    b02afff View commit details
  2. Merge pull request #25 from thetalecrafter/greenkeeper/mocha-5.0.0

    Update mocha to the latest version 🚀
    vanwagonet authored Jan 18, 2018
    Copy the full SHA
    3af843f View commit details

Commits on Feb 1, 2018

  1. Copy the full SHA
    bc8964c View commit details
  2. Copy the full SHA
    a8ae95f View commit details
  3. 2.0.1

    vanwagonet committed Feb 1, 2018
    Copy the full SHA
    807f84b View commit details

Commits on Feb 19, 2018

  1. Copy the full SHA
    d06c7c3 View commit details
  2. Merge pull request #27 from thetalecrafter/greenkeeper/standard-11.0.0

    Update standard to the latest version 🚀
    vanwagonet authored Feb 19, 2018
    Copy the full SHA
    412f0ce View commit details

Commits on Apr 26, 2018

  1. Copy the full SHA
    f0a2639 View commit details

Commits on Apr 27, 2018

  1. Copy the full SHA
    ab4dc2c View commit details

Commits on May 2, 2018

  1. Copy the full SHA
    b9dda91 View commit details
  2. Copy the full SHA
    77c4d67 View commit details
  3. Merge pull request #30 from thetalecrafter/next

    move imagemin to peer, plugin loading to client, and test with webpack 4
    vanwagonet authored May 2, 2018
    Copy the full SHA
    25c4e7a View commit details
Showing with 9,157 additions and 158 deletions.
  1. +4 −0 .travis.yml
  2. +13 −0 CHANGELOG.md
  3. +133 −84 README.md
  4. BIN __tests__/fixture.gif
  5. +1 −0 __tests__/fixture.svg
  6. +43 −21 __tests__/index.spec.js
  7. +39 −0 __tests__/run-webpack.js
  8. +11 −41 index.js
  9. +8,897 −0 package-lock.json
  10. +16 −12 package.json
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- 'lts/*'
- stable
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Changelog

## 3.0.0

**Breaking Change** By default no image optimizing is done. The options for the loader are passed directly to `imagemin.buffer`, so `options.plugins` should be passed as an array of configured imagemin plugins. If `plugins` is a function it will be called with the webpack loader context, and the plugin array should be returned.

## 2.0.1

Updated dependencies

## 2.0.0

Require Node 4. Support only webpack 2. Switch from jpegtran to mozjpeg.
Switch to `enabled` option instead of detecting `minimize` from UglifyJSPlugin.

## 1.3.1

Updated dependencies
217 changes: 133 additions & 84 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# img-loader

Image minimizing loader for webpack, meant to be used with [url-loader](https://github.com/webpack/url-loader), [file-loader](https://github.com/webpack/file-loader), or [raw-loader](https://github.com/webpack/raw-loader)
[![npm Version][npm-image]][npm]
[![Greenkeeper badge][greenkeeper-image]][greenkeeper]
[![Build Status][build-image]][build]

> Minify PNG, JPEG, GIF and SVG images with [imagemin](https://github.com/imagemin/imagemin)
[![JS Standard Style][style-image]][style]
[![MIT License][license-image]][LICENSE]

*Issues with the minimized output should be reported [to imagemin](https://github.com/imagemin/imagemin/issues).*
Image minimizing loader for webpack 4, meant to be used with [url-loader](https://github.com/webpack/url-loader), [file-loader](https://github.com/webpack/file-loader), or [raw-loader](https://github.com/webpack/raw-loader)

Comes with the following optimizers:
> Minify PNG, JPEG, GIF and SVG images with [imagemin](https://github.com/imagemin/imagemin) [plugins](https://www.npmjs.com/search?q=keywords:imageminplugin)
- [gifsicle](https://github.com/imagemin/imagemin-gifsicle)*Compress GIF images*
- [jpegtran](https://github.com/imagemin/imagemin-jpegtran)*Compress JPEG images*
- [optipng](https://github.com/imagemin/imagemin-optipng)*Compress PNG images*
- [pngquant](https://github.com/imagemin/imagemin-pngquant)*Compress PNG images*
- [svgo](https://github.com/imagemin/imagemin-svgo)*Compress SVG images*
img-loader has a peer dependency on `imagemin`, so you will need to make sure to include that, along with any imagemin plugins you will use.


## Install
@@ -24,112 +23,162 @@ $ npm install img-loader --save-dev

## Usage

[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html)
[Documentation: Using loaders](https://webpack.js.org/concepts/loaders/)

``` javascript
var url = require('file!img!./file.png');
```javascript
module: {
rules: [
{
test: /\.(jpe?g|png|gif|svg)$/i,
use: [
'url-loader?limit=10000',
'img-loader'
]
}
]
}
```

By default the minification is run when webpack is run in production mode (or whenever the UglifyJsPlugin is used).

The default minification includes:
By default the loader simply passes along the image unmodified.

- `gifsicle` with `interlaced: false`
- `jpegtran` with `progressive: false`
- `optipng` with `optimizationLevel: 2`
- `svgo` with default plugins

`pngquant` is disabled by default, and can be enabled by configuring it in the advanced options.
### Options

Options are forwarded to `imagemin.buffer(image, options)`, so any plugins you would like to use for optimizing the images are passed as the `plugins` property.

### Query Parameters
For more details on each plugin's options, see their documentation on [Github](https://github.com/imagemin).

These are common options you can specify in the `require` or `loaders` config.

* `minimize` - Minify regardless of webpack mode.
* `optimizationLevel` Select an optipng optimization level between `0` and `7`.
* `progressive` Convert to jpg and gif to progressive.

``` javascript
require('img?optimizationLevel=5');
require('img?progressive=true');
require('img?minimize&optimizationLevel=5&progressive=true');
require('img?-minimize'); // makes the loader a simple passthrough
```js
{
module: {
rules: [
{
test: /\.(jpe?g|png|gif|svg)$/i,
use: [
'url-loader?limit=10000',
{
loader: 'img-loader',
options: {
plugins: [
require('imagemin-gifsicle')({
interlaced: false
}),
require('imagemin-mozjpeg')({
progressive: true,
arithmetic: false
}),
require('imagemin-pngquant')({
floyd: 0.5,
speed: 2
}),
require('imagemin-svgo')({
plugins: [
{ removeTitle: true },
{ convertPathData: false }
]
})
]
}
}
]
}
]
}
}
```

``` javascript
`plugins` can also be a function, which will receive the [webpack loader context](https://webpack.js.org/api/loaders/#the-loader-context) and should return the plugins array.
```js
{
module: {
loaders: [
{ test: /\.(jpe?g|png|gif|svg)$/i, loader: 'url?limit=10000!img?progressive=true' }
rules: [
{
test: /\.(jpe?g|png|gif|svg)$/i,
use: [
'url-loader?limit=10000',
{
loader: 'img-loader',
options: {
plugins (context) {
if (process.env.NODE_ENV === 'production') return []
return [
require('imagemin-svgo')({
plugins: [
{ cleanupIDs: false },
{
prefixIds: {
prefix: path.basename(context.resourcePath, 'svg')
}
}
]
})
]
}
}
}
]
}
]
}
}
```

#### optimizationLevel *(png)*

Shortcut for [optipng `optimizationLevel`](https://github.com/imagemin/imagemin-optipng), which determines the number of trials.
If you only want to run imagemin in production builds, you can omit the `img-loader` or leave plugins empty in your production configuration file. If you don't keep a separate configuration for prod builds, something like the following also works:

#### progressive *(jpg, gif)*
```js
{
loader: 'img-loader',
options: {
plugins: process.env.NODE_ENV === 'production' && [
require('imagemin-svgo')({})
// etc.
]
}
}
```

Shortcut to enable [jpegtran `progressive`](https://github.com/imagemin/imagemin-jpegtran) and enable [gifsicle `interlace`](https://github.com/imagemin/imagemin-gifsicle) for progressive rendering.

## Migrating from 2.x

### Advanced Options
To get the default behavior from version `2.0.1`, you'll need to install these imagemin plugins:

Advanced options can also be passed by specifying an `imagemin` property on your webpack config object. Each optimizer will be passed the corresponding property on the `imagemin` object as options.
* [imagemin-gifsicle](https://github.com/imagemin/imagemin-gifsicle)
* [imagemin-mozjpeg](https://github.com/imagemin/imagemin-mozjpeg)
* [imagemin-optipng](https://github.com/imagemin/imagemin-optipng)
* [imagemin-svgo](https://github.com/imagemin/imagemin-svgo)

Any options specified this way override basic `optimizationLevel` and `progressive` options set. For more details on each plugin's options, see their documentation on [Github](https://github.com/imagemin).
Then use this loader setup in your webpack configuration file:

``` javascript
```js
{
module: {
loaders: [
{ test: /\.(jpe?g|png|gif|svg)$/i, loaders: [ 'url?limit=10000', 'img?minimize' ] }
loader: 'img-loader',
options: {
plugins: [
require('imagemin-gifsicle')({}),
require('imagemin-mozjpeg')({}),
require('imagemin-optipng')({}),
require('imagemin-svgo')({})
]
},
imagemin: {
gifsicle: { interlaced: false },
jpegtran: {
progressive: true,
arithmetic: false
},
optipng: { optimizationLevel: 5 },
pngquant: {
floyd: 0.5,
speed: 2
},
svgo: {
plugins: [
{ removeTitle: true },
{ convertPathData: false }
]
}
}
}
```

If you need to define two different loader configs, you can also change the config's property name via `img?config=otherConfig`:
The options object you had under a plugin's name property, should instead be passed directly to the plugin after you require it.

If you used the optional `pngquant` settings, then you will additionally need to install [imagemin-pngquant](https://github.com/imagemin/imagemin-pngquant), and add it to your plugins array as any other imagemin plugin.

``` javascript
{
module: {
loaders: [
{ test: /\.svg$/i, loaders: [ 'url?limit=10000', 'img?config=svgOpts' ] }
]
},
svgOpts: {
svgo: {
plugins: [
{ removeTitle: true },
{ convertPathData: false }
]
}
}
}
```

## License

This software is free to use under the MIT license. See the [LICENSE-MIT file](https://github.com/thetalecrafter/img-loader/blob/master/LICENSE-MIT) for license text and copyright information.
This software is free to use under the MIT license. See the [LICENSE-MIT file][LICENSE] for license text and copyright information.

[npm]: https://www.npmjs.org/package/img-loader
[npm-image]: https://img.shields.io/npm/v/img-loader.svg
[greenkeeper-image]: https://badges.greenkeeper.io/thetalecrafter/img-loader.svg
[greenkeeper]: https://greenkeeper.io/
[build]: https://travis-ci.org/thetalecrafter/img-loader
[build-image]: https://img.shields.io/travis/thetalecrafter/img-loader.svg
[style]: https://github.com/feross/standard
[style-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg
[license-image]: https://img.shields.io/npm/l/img-loader.svg
[LICENSE]: https://github.com/thetalecrafter/img-loader/blob/master/LICENSE-MIT
Binary file added __tests__/fixture.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions __tests__/fixture.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 43 additions & 21 deletions __tests__/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,51 @@
/* eslint-env mocha */
'use strict'

var assert = require('assert')
var loader = require('..')
var fs = require('fs')
var path = require('path')
var gifsicle = require('imagemin-gifsicle')
var svgo = require('imagemin-svgo')
var run = require('./run-webpack')

var fixtureGif = fs.readFileSync(path.resolve(__dirname, './fixture.gif'))
var fixtureSvg = fs.readFileSync(path.resolve(__dirname, './fixture.svg'))

describe('img-loader', () => {
it('passes the img though unchanged by default', function () {
return run('./fixture.gif').then(function (image) {
assert(image.equals(fixtureGif), 'gif should be unchanged')
})
})

it('can apply optimizations for gif', function () {
return run('./fixture.gif', {
plugins: [ gifsicle({}) ]
}).then(function (image) {
assert(!image.equals(fixtureGif), 'gif should be changed')
assert(image.length < fixtureGif.length, 'optimized gif should be smaller')
})
})

describe('img-loader', function () {
it('passes content through unmodified without minimize option', function () {
var img = new Buffer('<svg></svg>', 'utf8')
var context = { loader: loader, options: {} }
context.async = function () {
assert.fail('should not call async')
}
var buffer = context.loader(img)
assert.equal(buffer, img)
it('can apply optimizations for svg', function () {
return run('./fixture.svg', {
plugins: [ svgo({}) ]
}).then(function (image) {
assert(!image.equals(fixtureSvg), 'svg should be changed')
assert(image.length < fixtureSvg.length, 'optimized svg should be smaller')
assert.equal(image.toString('utf8'), '<svg/>')
})
})

it('optimizes svg images', function (done) {
var img = new Buffer('<svg><g><path d="M0 0" /></g></svg>', 'utf8')
var context = { loader: loader, options: {}, minimize: true }
context.async = function () {
return function (error, buffer) {
if (error) return done(error)
assert.equal(buffer.toString(), '<svg/>')
done()
it('can use a function for plugins', function () {
var context
return run('./fixture.svg', {
plugins: function (ctx) {
context = ctx
return [ svgo({}) ]
}
}
context.loader(img)
}).then(function (image) {
assert.equal(path.basename(context.resourcePath), 'fixture.svg')
assert(image.length < fixtureSvg.length, 'optimized svg should be smaller')
})
})
})
Loading