-
Notifications
You must be signed in to change notification settings - Fork 72
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
Changes from 4 commits
f1dac42
a5d87af
b720a9a
8ad37e4
6e068dc
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,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 |
This file was deleted.
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); | ||
|
||
const cli = meow(` | ||
Usage | ||
|
@@ -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', | ||
|
@@ -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} | ||
|
||
|
@@ -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 | ||
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. Would be great if you could fix the lint instead of just ignoring it. 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. I can replace it with any other type of loop, but imo 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. I would prefer a |
||
pairs(arrify(plugin).reduce((m, v) => | ||
typeof v === 'object' | ||
? {...m, ...v} | ||
: {[v]: {}, ...m} | ||
, {})); | ||
|
||
const run = async (input, {outDir, plugin} = {}) => { | ||
const pluginOptions = normalizePluginOptions(plugin); | ||
|
@@ -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); | ||
})(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
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. I think 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. Just a typo. It should be |
||
`--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 |
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.
require
should be replaced byawait import(…)
.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.
require('name')
searches for the module up the directory tree by diving into everynode_modules/<name>
. Аnd then it will also check thename
existence as global package. Dynamicimport
checks just a single path.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.
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.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.
Wow! My experience bases on early polyfills. It's great that it just works now.