This commit is contained in:
2022-04-07 18:09:31 +05:30
parent 36ea86d8e6
commit 1c779ef816
14095 changed files with 1769361 additions and 47 deletions
+73
View File
@@ -0,0 +1,73 @@
'use strict';
const {promisify} = require('util');
const path = require('path');
const fs = require('graceful-fs');
const fileType = require('file-type');
const globby = require('globby');
const makeDir = require('make-dir');
const pPipe = require('p-pipe');
const replaceExt = require('replace-ext');
const junk = require('junk');
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
const handleFile = async (sourcePath, {destination, plugins = []}) => {
if (plugins && !Array.isArray(plugins)) {
throw new TypeError('The `plugins` option should be an `Array`');
}
let data = await readFile(sourcePath);
data = await (plugins.length > 0 ? pPipe(...plugins)(data) : data);
let destinationPath = destination ? path.join(destination, path.basename(sourcePath)) : undefined;
destinationPath = (fileType(data) && fileType(data).ext === 'webp') ? replaceExt(destinationPath, '.webp') : destinationPath;
const returnValue = {
data,
sourcePath,
destinationPath
};
if (!destinationPath) {
return returnValue;
}
await makeDir(path.dirname(returnValue.destinationPath));
await writeFile(returnValue.destinationPath, returnValue.data);
return returnValue;
};
module.exports = async (input, {glob = true, ...options} = {}) => {
if (!Array.isArray(input)) {
throw new TypeError(`Expected an \`Array\`, got \`${typeof input}\``);
}
const filePaths = glob ? await globby(input, {onlyFiles: true}) : input;
return Promise.all(
filePaths
.filter(filePath => junk.not(path.basename(filePath)))
.map(async filePath => {
try {
return await handleFile(filePath, options);
} catch (error) {
error.message = `Error occurred when handling file: ${input}\n\n${error.stack}`;
throw error;
}
})
);
};
module.exports.buffer = async (input, {plugins = []} = {}) => {
if (!Buffer.isBuffer(input)) {
throw new TypeError(`Expected a \`Buffer\`, got \`${typeof input}\``);
}
if (plugins.length === 0) {
return input;
}
return pPipe(...plugins)(input);
};
+9
View File
@@ -0,0 +1,9 @@
MIT License
Copyright (c) Imagemin (github.com/imagemin)
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+46
View File
@@ -0,0 +1,46 @@
{
"name": "imagemin",
"version": "7.0.1",
"description": "Minify images seamlessly",
"license": "MIT",
"repository": "imagemin/imagemin",
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"minify",
"compress",
"image",
"images",
"jpeg",
"jpg",
"png",
"gif",
"svg"
],
"dependencies": {
"file-type": "^12.0.0",
"globby": "^10.0.0",
"graceful-fs": "^4.2.2",
"junk": "^3.1.0",
"make-dir": "^3.0.0",
"p-pipe": "^3.0.0",
"replace-ext": "^1.0.0"
},
"devDependencies": {
"ava": "^2.1.0",
"del": "^4.1.1",
"imagemin-jpegtran": "^6.0.0",
"imagemin-svgo": "^7.0.0",
"imagemin-webp": "^5.0.0",
"is-jpg": "^2.0.0",
"tempy": "^0.3.0",
"xo": "^0.24.0"
}
}
+119
View File
@@ -0,0 +1,119 @@
# imagemin [![Build Status](https://travis-ci.org/imagemin/imagemin.svg?branch=master)](https://travis-ci.org/imagemin/imagemin)
> Minify images seamlessly
---
<div align="center">
<sup>Gumlet is helping make open source sustainable by sponsoring Sindre Sorhus.</sup>
<a href="https://www.gumlet.com">
<div>
<img src="https://sindresorhus.com/assets/thanks/gumlet-logo.svg" width="300"/>
</div>
<sup><b>Optimised Image Delivery made simple</b></sup>
</a>
</div>
---
## Install
```
$ npm install imagemin
```
## Usage
```js
const imagemin = require('imagemin');
const imageminJpegtran = require('imagemin-jpegtran');
const imageminPngquant = require('imagemin-pngquant');
(async () => {
const files = await imagemin(['images/*.{jpg,png}'], {
destination: 'build/images',
plugins: [
imageminJpegtran(),
imageminPngquant({
quality: [0.6, 0.8]
})
]
});
console.log(files);
//=> [{data: <Buffer 89 50 4e …>, destinationPath: 'build/images/foo.jpg'}, …]
})();
```
## API
### imagemin(input, options?)
Returns `Promise<object[]>` in the format `{data: Buffer, sourcePath: string, destinationPath: string}`.
#### input
Type: `string[]`
File paths or [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
#### options
Type: `object`
##### destination
Type: `string`
Set the destination folder to where your files will be written. If no destination is specified, no files will be written.
##### plugins
Type: `Array`
[Plugins](https://www.npmjs.com/browse/keyword/imageminplugin) to use.
##### glob
Type: `boolean`<br>
Default: `true`
Enable globbing when matching file paths.
### imagemin.buffer(buffer, options?)
Returns `Promise<Buffer>`.
#### buffer
Type: `Buffer`
Buffer to optimize.
#### options
Type: `object`
##### plugins
Type: `Array`
[Plugins](https://www.npmjs.com/browse/keyword/imageminplugin) to use.
## Hosted API
We also provide a hosted API for imagemin which may simplify your use case.
<a href="https://imagemin.saasify.sh">
<img src="https://badges.saasify.sh?text=View%20Hosted%20API" height="40"/>
</a>
## Related
- [imagemin-cli](https://github.com/imagemin/imagemin-cli) - CLI for this module
- [imagemin-app](https://github.com/imagemin/imagemin-app) - GUI app for this module
- [gulp-imagemin](https://github.com/sindresorhus/gulp-imagemin) - Gulp plugin
- [grunt-contrib-imagemin](https://github.com/gruntjs/grunt-contrib-imagemin) - Grunt plugin