Skip to content

Commit 1b4baf6

Browse files
committedAug 12, 2021
Require Node.js 12.20 and move to ESM
1 parent ed39463 commit 1b4baf6

File tree

7 files changed

+68
-93
lines changed

7 files changed

+68
-93
lines changed
 

‎.github/funding.yml

-3
This file was deleted.

‎.github/workflows/main.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13-
- 14
14-
- 12
15-
- 10
13+
- 16
1614
steps:
1715
- uses: actions/checkout@v2
18-
- uses: actions/setup-node@v1
16+
- uses: actions/setup-node@v2
1917
with:
2018
node-version: ${{ matrix.node-version }}
2119
- run: npm install

‎index.js

+32-37
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,32 @@
1-
const path = require('path');
2-
const log = require('fancy-log');
3-
const PluginError = require('plugin-error');
4-
const through = require('through2-concurrent');
5-
const prettyBytes = require('pretty-bytes');
6-
const chalk = require('chalk');
7-
const imagemin = require('imagemin');
8-
const plur = require('plur');
1+
import {createRequire} from 'node:module';
2+
import path from 'node:path';
3+
import process from 'node:process';
4+
import log from 'fancy-log';
5+
import PluginError from 'plugin-error';
6+
import through from 'through2-concurrent';
7+
import prettyBytes from 'pretty-bytes';
8+
import chalk from 'chalk';
9+
import imagemin from 'imagemin';
10+
import plur from 'plur';
11+
12+
const require = createRequire(import.meta.url);
913

1014
const PLUGIN_NAME = 'gulp-imagemin';
1115
const defaultPlugins = ['gifsicle', 'mozjpeg', 'optipng', 'svgo'];
1216

1317
const loadPlugin = (plugin, ...args) => {
1418
try {
1519
return require(`imagemin-${plugin}`)(...args);
16-
} catch (_) {
17-
log(`${PLUGIN_NAME}: Couldn't load default plugin "${plugin}"`);
20+
} catch {
21+
log(`${PLUGIN_NAME}: Could not load default plugin \`${plugin}\``);
1822
}
1923
};
2024

2125
const exposePlugin = plugin => (...args) => loadPlugin(plugin, ...args);
2226

23-
const getDefaultPlugins = () =>
24-
defaultPlugins.reduce((plugins, plugin) => {
25-
const instance = loadPlugin(plugin);
27+
const getDefaultPlugins = () => defaultPlugins.flatMap(plugin => loadPlugin(plugin));
2628

27-
if (!instance) {
28-
return plugins;
29-
}
30-
31-
return plugins.concat(instance);
32-
}, []);
33-
34-
module.exports = (plugins, options) => {
29+
export default function gulpImagemin(plugins, options) {
3530
if (typeof plugins === 'object' && !Array.isArray(plugins)) {
3631
options = plugins;
3732
plugins = undefined;
@@ -41,17 +36,17 @@ module.exports = (plugins, options) => {
4136
// TODO: Remove this when Gulp gets a real logger with levels
4237
silent: process.argv.includes('--silent'),
4338
verbose: process.argv.includes('--verbose'),
44-
...options
39+
...options,
4540
};
4641

47-
const validExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.svg'];
42+
const validExtensions = new Set(['.jpg', '.jpeg', '.png', '.gif', '.svg']);
4843

4944
let totalBytes = 0;
5045
let totalSavedBytes = 0;
5146
let totalFiles = 0;
5247

5348
return through.obj({
54-
maxConcurrency: 8
49+
maxConcurrency: 8,
5550
}, (file, encoding, callback) => {
5651
if (file.isNull()) {
5752
callback(null, file);
@@ -63,7 +58,7 @@ module.exports = (plugins, options) => {
6358
return;
6459
}
6560

66-
if (!validExtensions.includes(path.extname(file.path).toLowerCase())) {
61+
if (!validExtensions.has(path.extname(file.path).toLowerCase())) {
6762
if (options.verbose) {
6863
log(`${PLUGIN_NAME}: Skipping unsupported image ${chalk.blue(file.relative)}`);
6964
}
@@ -77,14 +72,14 @@ module.exports = (plugins, options) => {
7772
(async () => {
7873
try {
7974
const data = await imagemin.buffer(file.contents, {
80-
plugins: localPlugins
75+
plugins: localPlugins,
8176
});
8277
const originalSize = file.contents.length;
8378
const optimizedSize = data.length;
8479
const saved = originalSize - optimizedSize;
8580
const percent = originalSize > 0 ? (saved / originalSize) * 100 : 0;
86-
const savedMsg = `saved ${prettyBytes(saved)} - ${percent.toFixed(1).replace(/\.0$/, '')}%`;
87-
const msg = saved > 0 ? savedMsg : 'already optimized';
81+
const savedMessage = `saved ${prettyBytes(saved)} - ${percent.toFixed(1).replace(/\.0$/, '')}%`;
82+
const message = saved > 0 ? savedMessage : 'already optimized';
8883

8984
if (saved > 0) {
9085
totalBytes += originalSize;
@@ -93,7 +88,7 @@ module.exports = (plugins, options) => {
9388
}
9489

9590
if (options.verbose) {
96-
log(`${PLUGIN_NAME}:`, chalk.green('✔ ') + file.relative + chalk.gray(` (${msg})`));
91+
log(`${PLUGIN_NAME}:`, chalk.green('✔ ') + file.relative + chalk.gray(` (${message})`));
9792
}
9893

9994
file.contents = data;
@@ -105,20 +100,20 @@ module.exports = (plugins, options) => {
105100
}, callback => {
106101
if (!options.silent) {
107102
const percent = totalBytes > 0 ? (totalSavedBytes / totalBytes) * 100 : 0;
108-
let msg = `Minified ${totalFiles} ${plur('image', totalFiles)}`;
103+
let message = `Minified ${totalFiles} ${plur('image', totalFiles)}`;
109104

110105
if (totalFiles > 0) {
111-
msg += chalk.gray(` (saved ${prettyBytes(totalSavedBytes)} - ${percent.toFixed(1).replace(/\.0$/, '')}%)`);
106+
message += chalk.gray(` (saved ${prettyBytes(totalSavedBytes)} - ${percent.toFixed(1).replace(/\.0$/, '')}%)`);
112107
}
113108

114-
log(`${PLUGIN_NAME}:`, msg);
109+
log(`${PLUGIN_NAME}:`, message);
115110
}
116111

117112
callback();
118113
});
119-
};
114+
}
120115

121-
module.exports.gifsicle = exposePlugin('gifsicle');
122-
module.exports.mozjpeg = exposePlugin('mozjpeg');
123-
module.exports.optipng = exposePlugin('optipng');
124-
module.exports.svgo = exposePlugin('svgo');
116+
export const gifsicle = exposePlugin('gifsicle');
117+
export const mozjpeg = exposePlugin('mozjpeg');
118+
export const optipng = exposePlugin('optipng');
119+
export const svgo = exposePlugin('svgo');

‎license

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
3+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66

‎package.json

+17-15
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
"author": {
99
"name": "Sindre Sorhus",
1010
"email": "sindresorhus@gmail.com",
11-
"url": "sindresorhus.com"
11+
"url": "https://sindresorhus.com"
1212
},
13+
"type": "module",
14+
"exports": "./index.js",
1315
"engines": {
14-
"node": ">=10"
16+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
1517
},
1618
"scripts": {
1719
"test": "xo && ava"
@@ -36,26 +38,26 @@
3638
"svg"
3739
],
3840
"dependencies": {
39-
"chalk": "^3.0.0",
40-
"fancy-log": "^1.3.2",
41-
"imagemin": "^7.0.0",
41+
"chalk": "^4.1.2",
42+
"fancy-log": "^1.3.3",
43+
"imagemin": "^8.0.1",
4244
"plugin-error": "^1.0.1",
43-
"plur": "^3.0.1",
44-
"pretty-bytes": "^5.3.0",
45+
"plur": "^4.0.0",
46+
"pretty-bytes": "^5.6.0",
4547
"through2-concurrent": "^2.0.0"
4648
},
4749
"devDependencies": {
48-
"ava": "^2.3.0",
49-
"get-stream": "^5.1.0",
50-
"imagemin-pngquant": "^8.0.0",
51-
"vinyl": "^2.2.0",
52-
"xo": "^0.25.3"
50+
"ava": "^3.15.0",
51+
"get-stream": "^6.0.1",
52+
"imagemin-pngquant": "^9.0.2",
53+
"vinyl": "^2.2.1",
54+
"xo": "^0.44.0"
5355
},
5456
"optionalDependencies": {
5557
"imagemin-gifsicle": "^7.0.0",
56-
"imagemin-mozjpeg": "^8.0.0",
57-
"imagemin-optipng": "^7.0.0",
58-
"imagemin-svgo": "^7.0.0"
58+
"imagemin-mozjpeg": "^9.0.0",
59+
"imagemin-optipng": "^8.0.0",
60+
"imagemin-svgo": "^9.0.0"
5961
},
6062
"peerDependencies": {
6163
"gulp": ">=4"

‎readme.md

+3-20
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ $ npm install --save-dev gulp-imagemin
1515
### Basic
1616

1717
```js
18-
const gulp = require('gulp');
19-
const imagemin = require('gulp-imagemin');
18+
import gulp from 'gulp';
19+
import imagemin from 'gulp-imagemin';
2020

21-
exports.default = () => (
21+
export default () => (
2222
gulp.src('src/images/*')
2323
.pipe(imagemin())
2424
.pipe(gulp.dest('dist/images'))
@@ -43,23 +43,6 @@ exports.default = () => (
4343
//
4444
```
4545

46-
Note that you may come across an older, implicit syntax. In versions < 3, the same was written like this:
47-
48-
```js
49-
//
50-
.pipe(imagemin({
51-
interlaced: true,
52-
progressive: true,
53-
optimizationLevel: 5,
54-
svgoPlugins: [
55-
{
56-
removeViewBox: true
57-
}
58-
]
59-
}))
60-
//
61-
```
62-
6346
### Custom plugin options and custom `gulp-imagemin` options
6447

6548
```js

‎test.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import {promisify} from 'util';
2-
import fs from 'fs';
3-
import path from 'path';
1+
import {promises as fs} from 'node:fs';
2+
import path from 'node:path';
3+
import {fileURLToPath} from 'node:url';
44
import imageminPngquant from 'imagemin-pngquant';
55
import Vinyl from 'vinyl';
66
import getStream from 'get-stream';
77
import test from 'ava';
8-
import gulpImagemin from '.';
8+
import gulpImagemin, {mozjpeg, svgo} from './index.js';
99

10-
const readFile = promisify(fs.readFile);
10+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1111

1212
const createFixture = async (plugins, file = 'fixture.png') => {
13-
const buffer = await readFile(path.join(__dirname, file));
13+
const buffer = await fs.readFile(path.join(__dirname, file));
1414
const stream = gulpImagemin(plugins);
1515

1616
stream.end(new Vinyl({
1717
path: path.join(__dirname, file),
18-
contents: buffer
18+
contents: buffer,
1919
}));
2020

2121
return {buffer, stream};
@@ -32,9 +32,9 @@ test('minify JPEG with custom settings', async t => {
3232
const mozjpegOptions = {
3333
quality: 30,
3434
progressive: false,
35-
smooth: 45
35+
smooth: 45,
3636
};
37-
const {buffer, stream} = await createFixture([gulpImagemin.mozjpeg(mozjpegOptions)], 'fixture.jpg');
37+
const {buffer, stream} = await createFixture([mozjpeg(mozjpegOptions)], 'fixture.jpg');
3838
const file = await getStream.array(stream);
3939

4040
t.true(file[0].contents.length < buffer.length);
@@ -50,13 +50,13 @@ test('use custom plugins', async t => {
5050
});
5151

5252
test('use custom svgo settings', async t => {
53-
const svgoOpts = {
53+
const svgoOptions = {
5454
js2svg: {
5555
indent: 2,
56-
pretty: true
57-
}
56+
pretty: true,
57+
},
5858
};
59-
const {stream} = await createFixture([gulpImagemin.svgo(svgoOpts)], 'fixture-svg-logo.svg');
59+
const {stream} = await createFixture([svgo(svgoOptions)], 'fixture-svg-logo.svg');
6060
const compareStream = (await createFixture(null, 'fixture-svg-logo.svg')).stream;
6161
const file = await getStream.array(stream);
6262
const compareFile = await getStream.array(compareStream);

0 commit comments

Comments
 (0)
Please sign in to comment.