Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Imagemin middleware #459

Merged
merged 17 commits into from
Nov 28, 2017
1 change: 1 addition & 0 deletions packages/imagemin/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/test/
108 changes: 108 additions & 0 deletions packages/imagemin/README.md
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: {},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation here seems off.

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we are missing the table of conventions here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
49 changes: 49 additions & 0 deletions packages/imagemin/index.js
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]);
};
29 changes: 29 additions & 0 deletions packages/imagemin/package.json
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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this repo format actually work on npm?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The 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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use deepmerge ^1.5.2.

"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"
}
}
33 changes: 33 additions & 0 deletions packages/imagemin/test/middleware_test.js
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());
});
20 changes: 10 additions & 10 deletions packages/web/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -192,5 +182,15 @@ module.exports = (neutrino, opts = {}) => {
}

config.output.filename('[name].[chunkhash].js');
})
Copy link
Member

Choose a reason for hiding this comment

The 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);
});
};