Skip to content

Commit fcf2293

Browse files
committedMar 14, 2017
rewrite for webpack 2
1 parent b00d2d6 commit fcf2293

File tree

6 files changed

+133
-148
lines changed

6 files changed

+133
-148
lines changed
 

‎.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
language: node_js
22
node_js:
3-
- '4.1'
3+
- '4.8'
44
- stable

‎CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 2.0.0
4+
5+
Require Node 4. Support only webpack 2. Switch from jpegtran to mozjpeg.
6+
Switch to `enabled` option instead of detecting `minimize` from UglifyJSPlugin.
7+
38
## 1.3.1
49

510
Updated dependencies

‎README.md

+53-90
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# img-loader
22

3-
[![Greenkeeper badge](https://badges.greenkeeper.io/thetalecrafter/img-loader.svg)](https://greenkeeper.io/)
4-
53
[![npm Version][npm-image]][npm]
4+
[![Greenkeeper badge][greenkeeper-image]][greenkeeper]
65
[![Build Status][build-image]][build]
6+
77
[![JS Standard Style][style-image]][style]
88
[![MIT License][license-image]][LICENSE]
99

10-
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)
10+
Image minimizing loader for webpack 2, 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)
1111

1212
> Minify PNG, JPEG, GIF and SVG images with [imagemin](https://github.com/imagemin/imagemin)
1313
@@ -16,7 +16,7 @@ Image minimizing loader for webpack, meant to be used with [url-loader](https://
1616
Comes with the following optimizers:
1717

1818
- [gifsicle](https://github.com/imagemin/imagemin-gifsicle)*Compress GIF images*
19-
- [jpegtran](https://github.com/imagemin/imagemin-jpegtran)*Compress JPEG images*
19+
- [mozjpeg](https://github.com/imagemin/imagemin-mozjpeg)*Compress JPEG images*
2020
- [optipng](https://github.com/imagemin/imagemin-optipng)*Compress PNG images*
2121
- [pngquant](https://github.com/imagemin/imagemin-pngquant)*Compress PNG images*
2222
- [svgo](https://github.com/imagemin/imagemin-svgo)*Compress SVG images*
@@ -33,116 +33,79 @@ $ npm install img-loader --save-dev
3333

3434
[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html)
3535

36-
``` javascript
37-
var url = require('file!img!./file.png');
38-
```
39-
40-
By default the minification is run when webpack is run in production mode (or whenever the UglifyJsPlugin is used).
41-
42-
The default minification includes:
43-
44-
- `gifsicle` with `interlaced: false`
45-
- `jpegtran` with `progressive: false`
46-
- `optipng` with `optimizationLevel: 2`
47-
- `svgo` with default plugins
48-
49-
`pngquant` is disabled by default, and can be enabled by configuring it in the advanced options.
50-
51-
52-
### Query Parameters
53-
54-
These are common options you can specify in the `require` or `loaders` config.
55-
56-
* `minimize` - Minify regardless of webpack mode.
57-
* `optimizationLevel` Select an optipng optimization level between `0` and `7`.
58-
* `progressive` Convert to jpg and gif to progressive.
59-
60-
``` javascript
61-
require('img?optimizationLevel=5');
62-
require('img?progressive=true');
63-
require('img?minimize&optimizationLevel=5&progressive=true');
64-
require('img?-minimize'); // makes the loader a simple passthrough
65-
```
66-
67-
``` javascript
68-
{
69-
module: {
70-
loaders: [
71-
{ test: /\.(jpe?g|png|gif|svg)$/i, loader: 'url?limit=10000!img?progressive=true' }
72-
]
73-
}
36+
```javascript
37+
module: {
38+
rules: [
39+
{
40+
test: /\.(jpe?g|png|gif|svg)$/i,
41+
use: [
42+
'url-loader?limit=10000',
43+
'img-loader'
44+
]
45+
}
46+
]
7447
}
7548
```
7649

77-
#### optimizationLevel *(png)*
78-
79-
Shortcut for [optipng `optimizationLevel`](https://github.com/imagemin/imagemin-optipng), which determines the number of trials.
50+
The default minification includes: `gifsicle`, `mozjpeg`, `optipng`, & `svgo`. Each with their default settings.
8051

81-
#### progressive *(jpg, gif)*
52+
`pngquant` can be enabled by configuring it in the options.
8253

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

55+
### Options
8556

86-
### Advanced Options
57+
Options can also be passed by specifying properties matching each optimizer in your rule options. `false` or `null` can be used to disable one of the default optimizers.
8758

88-
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.
89-
90-
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).
59+
For more details on each plugin's options, see their documentation on [Github](https://github.com/imagemin).
9160

9261
``` javascript
9362
{
9463
module: {
95-
loaders: [
96-
{ test: /\.(jpe?g|png|gif|svg)$/i, loaders: [ 'url?limit=10000', 'img?minimize' ] }
64+
rules: [
65+
{
66+
test: /\.(jpe?g|png|gif|svg)$/i,
67+
use: [
68+
'url-loader?limit=10000',
69+
{
70+
loader: 'img-loader',
71+
options: {
72+
enabled: process.env.NODE_ENV === 'production',
73+
gifsicle: {
74+
interlaced: false
75+
},
76+
mozjpeg: {
77+
progressive: true,
78+
arithmetic: false
79+
},
80+
optipng: false, // disabled
81+
pngquant: {
82+
floyd: 0.5,
83+
speed: 2
84+
},
85+
svgo: {
86+
plugins: [
87+
{ removeTitle: true },
88+
{ convertPathData: false }
89+
]
90+
}
91+
}
92+
}
93+
]
94+
}
9795
]
98-
},
99-
imagemin: {
100-
gifsicle: { interlaced: false },
101-
jpegtran: {
102-
progressive: true,
103-
arithmetic: false
104-
},
105-
optipng: { optimizationLevel: 5 },
106-
pngquant: {
107-
floyd: 0.5,
108-
speed: 2
109-
},
110-
svgo: {
111-
plugins: [
112-
{ removeTitle: true },
113-
{ convertPathData: false }
114-
]
115-
}
11696
}
11797
}
11898
```
11999

120-
If you need to define two different loader configs, you can also change the config's property name via `img?config=otherConfig`:
121-
122-
``` javascript
123-
{
124-
module: {
125-
loaders: [
126-
{ test: /\.svg$/i, loaders: [ 'url?limit=10000', 'img?config=svgOpts' ] }
127-
]
128-
},
129-
svgOpts: {
130-
svgo: {
131-
plugins: [
132-
{ removeTitle: true },
133-
{ convertPathData: false }
134-
]
135-
}
136-
}
137-
}
138-
```
139100

140101
## License
141102

142103
This software is free to use under the MIT license. See the [LICENSE-MIT file][LICENSE] for license text and copyright information.
143104

144105
[npm]: https://www.npmjs.org/package/img-loader
145106
[npm-image]: https://img.shields.io/npm/v/img-loader.svg
107+
[greenkeeper-image]: https://badges.greenkeeper.io/thetalecrafter/img-loader.svg
108+
[greenkeeper]: https://greenkeeper.io/
146109
[build]: https://travis-ci.org/thetalecrafter/img-loader
147110
[build-image]: https://img.shields.io/travis/thetalecrafter/img-loader.svg
148111
[style]: https://github.com/feross/standard

‎__tests__/index.spec.js

+42-20
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,49 @@
44
var assert = require('assert')
55
var loader = require('..')
66

7-
describe('img-loader', function () {
8-
it('passes content through unmodified without minimize option', function () {
9-
var img = new Buffer('<svg></svg>', 'utf8')
10-
var context = { loader: loader, options: {} }
11-
context.async = function () {
12-
assert.fail('should not call async')
13-
}
14-
var buffer = context.loader(img)
15-
assert.equal(buffer, img)
16-
})
7+
describe('img-loader', () => {
8+
describe('svgo', () => {
9+
it('optimizes svg images by default', (done) => {
10+
var img = new Buffer('<svg><g><path d="M0 0" /></g></svg>', 'utf8')
11+
var context = {
12+
loader,
13+
async () {
14+
return (error, buffer) => {
15+
if (error) return done(error)
16+
assert.equal(buffer.toString(), '<svg/>')
17+
done()
18+
}
19+
}
20+
}
21+
context.loader(img)
22+
})
23+
24+
it('passes content through when whole loader disabled', () => {
25+
var img = new Buffer('<svg></svg>', 'utf8')
26+
var context = {
27+
loader,
28+
query: { enabled: false },
29+
async () {
30+
assert.fail('should not call async')
31+
}
32+
}
33+
assert.equal(context.loader(img), img)
34+
})
1735

18-
it('optimizes svg images', function (done) {
19-
var img = new Buffer('<svg><g><path d="M0 0" /></g></svg>', 'utf8')
20-
var context = { loader: loader, options: {}, minimize: true }
21-
context.async = function () {
22-
return function (error, buffer) {
23-
if (error) return done(error)
24-
assert.equal(buffer.toString(), '<svg/>')
25-
done()
36+
it('does not optimize when plugin disabled', (done) => {
37+
var img = new Buffer('<svg></svg>', 'utf8')
38+
var context = {
39+
loader,
40+
query: { svgo: false },
41+
async () {
42+
return (error, buffer) => {
43+
if (error) return done(error)
44+
assert.equal(buffer.toString(), '<svg></svg>')
45+
done()
46+
}
47+
}
2648
}
27-
}
28-
context.loader(img)
49+
context.loader(img)
50+
})
2951
})
3052
})

‎index.js

+25-29
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,47 @@
22

33
var imagemin = require('imagemin')
44
var imageminGifsicle = require('imagemin-gifsicle')
5-
var imageminJpegtran = require('imagemin-jpegtran')
5+
var imageminMozjpeg = require('imagemin-mozjpeg')
66
var imageminOptipng = require('imagemin-optipng')
77
var imageminPngquant = require('imagemin-pngquant')
88
var imageminSvgo = require('imagemin-svgo')
99
var loaderUtils = require('loader-utils')
10-
var assign = require('object-assign')
10+
11+
var defaults = {
12+
enabled: true,
13+
gifsicle: {},
14+
mozjpeg: {},
15+
optipng: {},
16+
svgo: {}
17+
}
1118

1219
module.exports = function (content) {
1320
this.cacheable && this.cacheable()
1421

15-
var options = loaderUtils.getLoaderConfig(this, 'imagemin')
16-
var minimize = ('minimize' in options) ? options.minimize : this.minimize
17-
if (!minimize) {
22+
var options = Object.assign(
23+
Object.create(defaults),
24+
loaderUtils.getOptions(this)
25+
)
26+
if (!options.enabled) {
1827
return content
1928
}
2029

21-
var progressive = options.progressive || false
22-
var optimizationLevel = options.optimizationLevel || 2
23-
2430
var use = [
25-
imageminGifsicle(assign({
26-
interlaced: progressive
27-
}, options.gifsicle)),
28-
imageminJpegtran(assign({
29-
progressive: progressive
30-
}, options.jpegtran)),
31-
imageminOptipng(assign({
32-
optimizationLevel: optimizationLevel
33-
}, options.optipng)),
34-
imageminSvgo(assign({
35-
}, options.svgo))
36-
]
37-
if (options.pngquant) {
38-
use.push(imageminPngquant(assign({
39-
}, options.pngquant)))
31+
options.gifsicle && imageminGifsicle(options.gifsicle),
32+
options.mozjpeg && imageminMozjpeg(options.mozjpeg),
33+
options.optipng && imageminOptipng(options.optipng),
34+
options.svgo && imageminSvgo(options.svgo),
35+
options.pngquant && imageminPngquant(options.pngquant)
36+
].filter(Boolean)
37+
if (use.length === 0) {
38+
return content
4039
}
40+
4141
var callback = this.async()
4242
imagemin
4343
.buffer(content, { use: use })
44-
.then(function (buffer) {
45-
callback(null, buffer)
46-
})
47-
.catch(function (error) {
48-
callback(error)
49-
})
44+
.then(function (buffer) { callback(null, buffer) })
45+
.catch(function (error) { callback(error) })
5046
}
5147

5248
module.exports.raw = true

‎package.json

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "img-loader",
3-
"version": "1.3.1",
3+
"version": "2.0.0",
44
"description": "Image minimizing loader for webpack",
55
"keywords": [
66
"image",
@@ -25,14 +25,13 @@
2525
},
2626
"homepage": "https://github.com/thetalecrafter/img-loader",
2727
"dependencies": {
28-
"imagemin": "^5.0.0",
29-
"imagemin-gifsicle": "^5.0.0",
30-
"imagemin-jpegtran": "^5.0.0",
31-
"imagemin-optipng": "^5.0.0",
28+
"imagemin": "^5.2.0",
29+
"imagemin-gifsicle": "^5.1.0",
30+
"imagemin-mozjpeg": "^6.0.0",
31+
"imagemin-optipng": "^5.2.0",
3232
"imagemin-pngquant": "^5.0.0",
33-
"imagemin-svgo": "^5.0.0",
34-
"loader-utils": "^0.2.15",
35-
"object-assign": "^4.0.0"
33+
"imagemin-svgo": "^5.2.0",
34+
"loader-utils": "^1.0.4"
3635
},
3736
"devDependencies": {
3837
"mocha": "^3.0.0",

0 commit comments

Comments
 (0)
Please sign in to comment.