Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require Node.js 12.20 and move to ESM #29

Merged
merged 5 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
os:
- ubuntu-latest
- macos-latest
- windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: yarn install --frozen-lockfile
- run: yarn test
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

61 changes: 33 additions & 28 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#!/usr/bin/env node
'use strict';
const arrify = require('arrify');
const meow = require('meow');
const getStdin = require('get-stdin');
const imagemin = require('imagemin');
const ora = require('ora');
const plur = require('plur');
const stripIndent = require('strip-indent');
const pairs = require('lodash.pairs');

import {Buffer} from 'node:buffer';
import {createRequire} from 'node:module';
import process from 'node:process';
import arrify from 'arrify';
import meow from 'meow';
import getStdin from 'get-stdin';
import imagemin from 'imagemin';
import ora from 'ora';
import plur from 'plur';
import stripIndent from 'strip-indent';
import pairs from 'lodash.pairs';

const require = createRequire(import.meta.url);
Copy link
Contributor

Choose a reason for hiding this comment

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

require should be replaced by await import(…).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

require('name') searches for the module up the directory tree by diving into everynode_modules/<name>. Аnd then it will also check the name existence as global package. Dynamic import checks just a single path.

Copy link
Contributor

Choose a reason for hiding this comment

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

Dynamic import checks just a single path.

Are you sure about that? According to https://nodejs.org/api/esm.html#esm_resolver_algorithm_specification, static import statement searches up, so I would have assumed dynamic import does the same.

Copy link
Contributor Author

@antongolub antongolub Aug 6, 2021

Choose a reason for hiding this comment

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

Wow! My experience bases on early polyfills. It's great that it just works now.


const cli = meow(`
Usage
Expand All @@ -27,6 +32,7 @@ const cli = meow(`
$ imagemin foo.png --plugin.pngquant.quality={0.1,0.2} > foo-optimized.png
$ imagemin foo.png --plugin.webp.quality=95 --plugin.webp.preset=icon > foo-icon.webp
`, {
importMeta: import.meta,
flags: {
plugin: {
type: 'string',
Expand All @@ -36,20 +42,20 @@ const cli = meow(`
'gifsicle',
'jpegtran',
'optipng',
'svgo'
]
'svgo',
],
},
outDir: {
type: 'string',
alias: 'o'
}
}
alias: 'o',
},
},
});

const requirePlugins = plugins => plugins.map(([plugin, options]) => {
try {
return require(`imagemin-${plugin}`)(options);
} catch (_) {
} catch {
console.error(stripIndent(`
Unknown plugin: ${plugin}

Expand All @@ -63,13 +69,13 @@ const requirePlugins = plugins => plugins.map(([plugin, options]) => {
}
});

const normalizePluginOptions = plugin => {
return pairs(arrify(plugin).reduce((m, v) => {
return typeof v === 'object' ?
{...m, ...v} :
{[v]: {}, ...m};
}, {}));
};
const normalizePluginOptions = plugin =>
// eslint-disable-next-line unicorn/no-array-reduce
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be great if you could fix the lint instead of just ignoring it.

Copy link
Contributor Author

@antongolub antongolub Aug 6, 2021

Choose a reason for hiding this comment

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

I can replace it with any other type of loop, but imo reduce is exactly what we need here — converting an array into a map.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer a for-of loop. I generally, find that to be more readable.

pairs(arrify(plugin).reduce((m, v) =>
typeof v === 'object'
? {...m, ...v}
: {[v]: {}, ...m}
, {}));

const run = async (input, {outDir, plugin} = {}) => {
const pluginOptions = normalizePluginOptions(plugin);
Expand Down Expand Up @@ -118,10 +124,9 @@ if (cli.input.length === 0 && process.stdin.isTTY) {
}

(async () => {
if (cli.input.length > 0) {
await run(cli.input, cli.flags);
} else {
await run(await getStdin.buffer(), cli.flags);
}
await run(
cli.input.length > 0
? cli.input
: await getStdin.buffer(),
cli.flags);
})();

29 changes: 15 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
"license": "MIT",
"repository": "imagemin/imagemin-cli",
"funding": "https://github.com/imagemin/imagemin-cli?sponsor=1",
"type": "module",
"bin": {
"imagemin": "cli.js"
},
"engines": {
"node": ">=10"
"node": ">=12.20"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -31,26 +32,26 @@
"svg"
],
"dependencies": {
"arrify": "^2.0.1",
"get-stdin": "^8.0.0",
"imagemin": "^7.0.0",
"arrify": "^3.0.0",
"get-stdin": "^9.0.0",
"imagemin": "^8.0.0",
"lodash.pairs": "^3.0.1",
"meow": "^7.0.1",
"ora": "^4.0.3",
"meow": "^10.1.1",
"ora": "^5.4.1",
"plur": "^4.0.0",
"strip-indent": "^3.0.0"
"strip-indent": "^4.0.0"
},
"devDependencies": {
"ava": "^3.8.0",
"execa": "^4.0.0",
"imagemin-pngquant": "^8.0.0",
"xo": "^0.30.0"
"ava": "^3.15.0",
"execa": "^5.1.1",
"imagemin-pngquant": "^9.0.2",
"xo": "^0.43.0"
},
"optionalDependencies": {
"imagemin-gifsicle": "^7.0.0",
"imagemin-jpegtran": "^6.0.0",
"imagemin-optipng": "^7.0.0",
"imagemin-svgo": "^7.0.0"
"imagemin-jpegtran": "^7.0.0",
"imagemin-optipng": "^8.0.0",
"imagemin-svgo": "^9.0.0"
},
"xo": {
"rules": {
Expand Down
6 changes: 5 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ $ imagemin --help
$ imagemin foo.png > foo-optimized.png
$ cat foo.png | imagemin > foo-optimized.png
$ imagemin foo.png --plugin=pngquant > foo-optimized.png
$ imagemin foo.png --plugin.pngquant.quality={0.1,0.2} > foo-optimized.png
$ imagemin foo.png --plugin.pngquant.quality=0.5 --plugin.pngquant.quality=1 > foo-optimized.png
$ imagemin foo.png --plugin.webp.quality=95 --plugin.webp.preset=icon > foo-icon.webp
```

**macOS** users can also use the short CLI syntax for array arguments:
Copy link
Contributor

Choose a reason for hiding this comment

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

I think non-Windows would be more correct than macOS

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just a typo. It should be macOS/linux. But ok, non-Windows is also fine.

`--plugin.pngquant.quality={0.5,1}` equals
`--plugin.pngquant.quality=0.5 --plugin.pngquant.quality=1`

## Related

- [imagemin](https://github.com/imagemin/imagemin) - API for this module
36 changes: 22 additions & 14 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
const {promisify} = require('util');
const fs = require('fs');
const execa = require('execa');
const test = require('ava');
import {promisify} from 'node:util';
import fs from 'node:fs';
import {dirname} from 'node:path';
import {fileURLToPath} from 'node:url';
import {createRequire} from 'node:module';
import process from 'node:process';

import execa from 'execa';
import test from 'ava';

const __dirname = dirname(fileURLToPath(import.meta.url));
const require = createRequire(import.meta.url);

process.chdir(__dirname);

Expand All @@ -17,7 +25,7 @@ test('optimize a GIF', async t => {

const {stdout} = await execa('./cli.js', {
input,
encoding: 'buffer'
encoding: 'buffer',
});

t.true(stdout.length < input.length);
Expand All @@ -28,7 +36,7 @@ test('optimize a JPG', async t => {

const {stdout} = await execa('./cli.js', {
input,
encoding: 'buffer'
encoding: 'buffer',
});

t.true(stdout.length < input.length);
Expand All @@ -39,7 +47,7 @@ test('optimize a PNG', async t => {

const {stdout} = await execa('./cli.js', {
input,
encoding: 'buffer'
encoding: 'buffer',
});

t.true(stdout.length < input.length);
Expand All @@ -50,7 +58,7 @@ test('optimize a SVG', async t => {

const {stdout} = await execa('./cli.js', {
input,
encoding: 'buffer'
encoding: 'buffer',
});

t.true(stdout.length < input.length);
Expand All @@ -65,12 +73,12 @@ test('support plugins', async t => {

const {stdout: data} = await execa('./cli.js', ['--plugin=pngquant'], {
input,
encoding: 'buffer'
encoding: 'buffer',
});

const {stdout: compareData} = await execa('./cli.js', {
input,
encoding: 'buffer'
encoding: 'buffer',
});

t.true(data.length < compareData.length);
Expand All @@ -85,7 +93,7 @@ test('throw on missing plugins', async t => {
const input = await readFile('fixtures/test.png');
const error = await t.throwsAsync(execa('./cli.js', ['--plugin=unicorn'], {
input,
encoding: 'buffer'
encoding: 'buffer',
}));

t.regex(error.stderr.toString(), /Unknown plugin: unicorn/);
Expand All @@ -94,14 +102,14 @@ test('throw on missing plugins', async t => {
test('support plugin options', async t => {
const input = await readFile('fixtures/test.png');

const {stdout: data} = await execa('./cli.js', ['--plugin.pngquant.dithering=1'], {
const {stdout: data} = await execa('./cli.js', ['--plugin.pngquant.dithering=1', '--plugin.pngquant.quality=0.1', '--plugin.pngquant.quality=0.4'], {
input,
encoding: 'buffer'
encoding: 'buffer',
});

const {stdout: compareData} = await execa('./cli.js', {
input,
encoding: 'buffer'
encoding: 'buffer',
});

t.true(data.length < compareData.length);
Expand Down