Skip to content

Commit

Permalink
feat: add @pkgr/imagemin package 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Sep 29, 2019
1 parent 8c31ac4 commit 3aba38a
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/imagemin/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
module.exports = require('esm')(module)('./lib/cli')
1 change: 1 addition & 0 deletions packages/imagemin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('esm')(module)('./lib')
33 changes: 33 additions & 0 deletions packages/imagemin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@pkgr/imagemin",
"version": "0.0.0",
"description": "Seamless imagemin API wrapper to minify images, working perfectly with lint-staged also",
"repository": "git@github.com:rx-ts/pkgr.git",
"homepage": "https://github.com/rx-ts/pkgr/blob/master/packages/imagemin",
"author": "JounQin <admin@1stg.me>",
"license": "MIT",
"engines": {
"node": ">=8.0.0"
},
"bin": {
"i": "cli.js"
},
"es2015": "lib/es2015",
"fesm5": "lib/esm",
"types": "lib",
"dependencies": {
"esm": "^3.2.25",
"imagemin": "^7.0.0",
"imagemin-gifsicle": "^6.0.1",
"imagemin-jpegtran": "^6.0.0",
"imagemin-mozjpeg": "^8.0.0",
"imagemin-optipng": "^7.0.0",
"imagemin-pngquant": "^8.0.0",
"imagemin-svgo": "^7.0.0",
"imagemin-upng": "^1.3.0",
"imagemin-webp": "^5.1.0"
},
"publishConfig": {
"access": "public"
}
}
6 changes: 6 additions & 0 deletions packages/imagemin/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import imagemin from '.'

Promise.all(process.argv.slice(2).map(imagemin)).catch(e => {
console.error(e)
process.exitCode = 1
})
62 changes: 62 additions & 0 deletions packages/imagemin/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import fs from 'fs'
import { promisify } from 'util'

const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile)

const plugins = [
[
'gifsicle',
{
interlaced: true,
},
],
[
'jpegtran',
{
progressive: true,
},
],
['mozjpeg'],
[
'optipng',
{
optimizationLevel: 5,
},
],
[
'pngquant',
{
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
quality: [0.6, 0.8],
},
],
['upng'],
[
'svgo',
{
plugins: [
{
removeViewBox: false,
},
],
},
],
[
'webp',
{
plugins: [
{
removeViewBox: false,
},
],
},
],
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
].map(([name, opts]) => require(`imagemin-${name}`)(opts))

export default (filename: string) =>
[...plugins, (it: string) => writeFile(filename, it)].reduce(
(acc: Promise<void>, it: () => Promise<void>) => acc.then(it),
readFile(filename),
)
7 changes: 7 additions & 0 deletions packages/imagemin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@1stg/tsconfig/composite",
"compilerOptions": {
"rootDir": "src",
"outDir": "lib"
}
}
2 changes: 1 addition & 1 deletion packages/rollup/cli.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env node
module.exports = require('esm')(module)('./lib/cjs')
module.exports = require('esm')(module)('./lib/cli')
3 changes: 1 addition & 2 deletions packages/rollup/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,5 @@ Promise.all(
),
).catch(e => {
console.error(e)
// eslint-disable-next-line no-process-exit
process.exit(1)
process.exitCode = 1
})
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"noEmit": true
},
"references": [
{
"path": "packages/imagemin"
},
{
"path": "packages/rollup"
},
Expand Down

0 comments on commit 3aba38a

Please sign in to comment.