-
Notifications
You must be signed in to change notification settings - Fork 214
Imagemin middleware #459
Imagemin middleware #459
Changes from 12 commits
d79f2da
1f22c4f
d1c1166
7d4b6b3
e60a562
3a5e661
56470fa
6433573
88dd78f
eecea98
40a3c9e
36db012
42d9d70
ff798d4
62f4aba
4fed652
1b29460
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/test/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# Neutrino Imagemin Middleware | ||
|
||
`@neutrinojs/imagemin` is Neutrino middleware for optimizing images with imagemin. | ||
|
||
[![NPM version][npm-image]][npm-url] | ||
[![NPM downloads][npm-downloads]][npm-url] | ||
[![Join the Neutrino community on Spectrum][spectrum-image]][spectrum-url] | ||
|
||
## Requirements | ||
|
||
- Node.js v6.10+ | ||
- Yarn or npm client | ||
- Neutrino v7 | ||
|
||
## Installation | ||
|
||
`@neutrinojs/imagemin` can be installed via the Yarn or npm clients. | ||
|
||
#### Yarn | ||
|
||
```bash | ||
❯ yarn add @neutrinojs/imagemin | ||
``` | ||
|
||
#### npm | ||
|
||
```bash | ||
❯ npm install --save @neutrinojs/imagemin | ||
``` | ||
|
||
## Usage | ||
|
||
`@neutrinojs/imagemin` can be consumed from the Neutrino API, middleware, or presets. Require this package | ||
and plug it into Neutrino: | ||
|
||
```js | ||
// Using function middleware format | ||
const images = require('@neutrinojs/image-loader'); | ||
const imagemin = require('@neutrinojs/imagemin'); | ||
|
||
// Use with default options | ||
neutrino.use(images); | ||
neutrino.use(imagemin); | ||
|
||
// Usage showing default options | ||
neutrino.use(imagemin, { | ||
imagemin: { | ||
plugins: [ | ||
gifsicle(), | ||
svgo(), | ||
pngquant(), | ||
mozjpeg(), | ||
webp() | ||
] | ||
}, | ||
plugin: { | ||
name: '[path][name].[ext]', | ||
test: /\.(png|jpg|jpeg|gif|webp)$/ | ||
}, | ||
pluginId: 'imagemin', | ||
useId: 'imagemin', | ||
rules: ['svg', 'img'] | ||
}); | ||
``` | ||
|
||
```js | ||
// Using object or array middleware format | ||
|
||
// Use with default options | ||
module.exports = { | ||
use: ['@neutrinojs/imagemin'] | ||
}; | ||
|
||
// Usage showing default options | ||
module.exports = { | ||
use: [ | ||
['@neutrinojs/imagemin', { | ||
imagemin: {}, | ||
plugin: { | ||
name: '[path][name].[ext]', | ||
test: /\.(png|jpg|jpeg|gif|webp)$/ | ||
}, | ||
rules: ['svg', 'img'] | ||
}] | ||
] | ||
}; | ||
``` | ||
|
||
- `imagemin`: Set options for [imagemin](https://github.com/imagemin/imagemin#options). | ||
- `plugin`: Set options for [imagemin-webpack](https://github.com/itgalaxy/imagemin-webpack#standalone-plugin)'s ImageminWebpackPlugin. | ||
- `rules`: Specify rules to apply imagemin to | ||
|
||
## Customization | ||
|
||
`@neutrinojs/imagemin` creates some conventions to make overriding the configuration easier once you are | ||
ready to make changes. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like we are missing the table of conventions here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a Rules and Plugins table. |
||
|
||
## Contributing | ||
|
||
This middleware is part of the [neutrino-dev](https://github.com/mozilla-neutrino/neutrino-dev) repository, a monorepo | ||
containing all resources for developing Neutrino and its core presets and middleware. Follow the | ||
[contributing guide](https://neutrino.js.org/contributing) for details. | ||
|
||
[npm-image]: https://img.shields.io/npm/v/@neutrinojs/imagemin.svg | ||
[npm-downloads]: https://img.shields.io/npm/dt/@neutrinojs/imagemin.svg | ||
[npm-url]: https://npmjs.org/package/@neutrinojs/imagemin | ||
[spectrum-image]: https://withspectrum.github.io/badge/badge.svg | ||
[spectrum-url]: https://spectrum.chat/neutrino |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const merge = require('deepmerge'); | ||
const ImageminWebpackPlugin = require('imagemin-webpack/ImageminWebpackPlugin'); | ||
const gifsicle = require('imagemin-gifsicle'); | ||
const svgo = require('imagemin-svgo'); | ||
const pngquant = require('imagemin-pngquant'); | ||
const mozjpeg = require('imagemin-mozjpeg'); | ||
const webp = require('imagemin-webp'); | ||
|
||
const imageminLoader = require.resolve('imagemin-webpack/imagemin-loader'); | ||
|
||
module.exports = (neutrino, opts = {}) => { | ||
const options = merge({ | ||
imagemin: { | ||
plugins: [ | ||
gifsicle(), | ||
svgo(), | ||
pngquant(), | ||
mozjpeg(), | ||
webp() | ||
] | ||
}, | ||
plugin: { | ||
name: '[path][name].[ext]' | ||
}, | ||
pluginId: 'imagemin', | ||
useId: 'imagemin', | ||
rules: ['svg', 'img'] | ||
}, opts); | ||
|
||
const test = options.rules.map((ruleId) => { | ||
neutrino.config.module | ||
.rule(ruleId) | ||
.use(options.useId) | ||
.loader(imageminLoader) | ||
.options(options.imagemin); | ||
|
||
return neutrino.config.module.rule(ruleId).get('test'); | ||
}) | ||
.join('|'); | ||
|
||
options.plugin = merge({ | ||
imageminOptions: options.imagemin, | ||
test | ||
}, options.plugin); | ||
|
||
neutrino.config | ||
.plugin(options.pluginId) | ||
.use(ImageminWebpackPlugin, [options.plugin]); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "@neutrinojs/imagemin", | ||
"version": "7.3.2", | ||
"description": "Neutrino middleware for optimizing images with imagemin", | ||
"main": "index.js", | ||
"keywords": [ | ||
"neutrino", | ||
"neutrino-middleware", | ||
"image", | ||
"imagemin" | ||
], | ||
"author": "Tim Kelty <timkelty@gmail.com>", | ||
"license": "MPL-2.0", | ||
"repository": "https://github.com/mozilla-neutrino/neutrino-dev/tree/master/packages/imagemin", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this repo format actually work on npm? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm not sure - I was just following suit with the rest of the middlewares: https://github.com/mozilla-neutrino/neutrino-dev/blob/master/packages/web/package.json#L14 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wtf. When I was viewing this diff, it showed a different format...don't mind me. |
||
"homepage": "https://neutrino.js.org", | ||
"bugs": "https://github.com/mozilla-neutrino/neutrino-dev/issues", | ||
"dependencies": { | ||
"deepmerge": "^2.0.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use deepmerge |
||
"imagemin-gifsicle": "5.2.0", | ||
"imagemin-mozjpeg": "^6.0.0", | ||
"imagemin-pngquant": "^5.0.0", | ||
"imagemin-svgo": "^6.0.0", | ||
"imagemin-webp": "^4.0.0", | ||
"imagemin-webpack": "^1.1.2" | ||
}, | ||
"peerDependencies": { | ||
"neutrino": "^7.0.0" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import test from 'ava'; | ||
import { Neutrino } from 'neutrino'; | ||
|
||
const mw = () => require('..'); | ||
const options = { limit: 1024, img: { limit: 2048 }, svg: { limit: 4096 }, ico: { limit: 8192 } }; | ||
|
||
test('loads middleware', t => { | ||
t.notThrows(mw); | ||
}); | ||
|
||
test('uses middleware', t => { | ||
t.notThrows(() => Neutrino().use(mw())); | ||
}); | ||
|
||
test('uses with options', t => { | ||
t.notThrows(() => Neutrino().use(mw(), options)); | ||
}); | ||
|
||
test('instantiates', t => { | ||
const api = Neutrino(); | ||
|
||
api.use(mw()); | ||
|
||
t.notThrows(() => api.config.toConfig()); | ||
}); | ||
|
||
test('instantiates with options', t => { | ||
const api = Neutrino(); | ||
|
||
api.use(mw(), options); | ||
|
||
t.notThrows(() => api.config.toConfig()); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -166,16 +166,6 @@ module.exports = (neutrino, opts = {}) => { | |
neutrino.use(devServer, options.devServer); | ||
config.when(options.hot, () => neutrino.use(hot)); | ||
}) | ||
.when(process.env.NODE_ENV === 'production', () => { | ||
neutrino.use(chunk); | ||
|
||
if (options.minify) { | ||
neutrino.use(minify, options.minify); | ||
} | ||
|
||
neutrino.config.plugin('module-concat') | ||
.use(optimize.ModuleConcatenationPlugin); | ||
}) | ||
.when(neutrino.options.command === 'build', (config) => { | ||
neutrino.use(clean, { paths: [neutrino.options.output] }); | ||
neutrino.use(copy, { | ||
|
@@ -192,5 +182,15 @@ module.exports = (neutrino, opts = {}) => { | |
} | ||
|
||
config.output.filename('[name].[chunkhash].js'); | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this was moved without modification, so we should probably reset this file to what is in master. |
||
.when(process.env.NODE_ENV === 'production', () => { | ||
neutrino.use(chunk); | ||
|
||
if (options.minify) { | ||
neutrino.use(minify, options.minify); | ||
} | ||
|
||
neutrino.config.plugin('module-concat') | ||
.use(optimize.ModuleConcatenationPlugin); | ||
}); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation here seems off.