schnee effeckt und fehler Korektur
This commit is contained in:
191
node_modules/imagemin/index.js
generated
vendored
191
node_modules/imagemin/index.js
generated
vendored
@@ -1,137 +1,76 @@
|
||||
'use strict';
|
||||
var bufferToVinyl = require('buffer-to-vinyl');
|
||||
var concatStream = require('concat-stream');
|
||||
var optional = require('optional');
|
||||
var PassThrough = require('readable-stream/passthrough');
|
||||
var streamCombiner = require('stream-combiner2');
|
||||
var vinylFs = require('vinyl-fs');
|
||||
import {Buffer} from 'node:buffer';
|
||||
import {promises as fsPromises} from 'node:fs';
|
||||
import {promisify} from 'node:util';
|
||||
import path from 'node:path';
|
||||
import fs from 'graceful-fs';
|
||||
import FileType from 'file-type';
|
||||
import {globby} from 'globby';
|
||||
import pPipe from 'p-pipe';
|
||||
import replaceExt from 'replace-ext';
|
||||
import junk from 'junk';
|
||||
import convertToUnixPath from 'slash';
|
||||
|
||||
/**
|
||||
* Initialize Imagemin
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
const readFile = promisify(fs.readFile);
|
||||
const writeFile = promisify(fs.writeFile);
|
||||
|
||||
function Imagemin() {
|
||||
if (!(this instanceof Imagemin)) {
|
||||
return new Imagemin();
|
||||
const handleFile = async (sourcePath, {destination, plugins = []}) => {
|
||||
if (plugins && !Array.isArray(plugins)) {
|
||||
throw new TypeError('The `plugins` option should be an `Array`');
|
||||
}
|
||||
|
||||
this.streams = [];
|
||||
let data = await readFile(sourcePath);
|
||||
data = await (plugins.length > 0 ? pPipe(...plugins)(data) : data);
|
||||
|
||||
const {ext} = await FileType.fromBuffer(data) || {ext: path.extname(sourcePath)};
|
||||
let destinationPath = destination ? path.join(destination, path.basename(sourcePath)) : undefined;
|
||||
destinationPath = ext === 'webp' ? replaceExt(destinationPath, '.webp') : destinationPath;
|
||||
|
||||
const returnValue = {
|
||||
data,
|
||||
sourcePath,
|
||||
destinationPath,
|
||||
};
|
||||
|
||||
if (!destinationPath) {
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
await fsPromises.mkdir(path.dirname(returnValue.destinationPath), {recursive: true});
|
||||
await writeFile(returnValue.destinationPath, returnValue.data);
|
||||
|
||||
return returnValue;
|
||||
};
|
||||
|
||||
export default async function imagemin(input, {glob = true, ...options} = {}) {
|
||||
if (!Array.isArray(input)) {
|
||||
throw new TypeError(`Expected an \`Array\`, got \`${typeof input}\``);
|
||||
}
|
||||
|
||||
const unixFilePaths = input.map(path => convertToUnixPath(path));
|
||||
const filePaths = glob ? await globby(unixFilePaths, {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;
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or set the source files
|
||||
*
|
||||
* @param {Array|Buffer|String} file
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Imagemin.prototype.src = function (file) {
|
||||
if (!arguments.length) {
|
||||
return this._src;
|
||||
imagemin.buffer = async (input, {plugins = []} = {}) => {
|
||||
if (!Buffer.isBuffer(input)) {
|
||||
throw new TypeError(`Expected a \`Buffer\`, got \`${typeof input}\``);
|
||||
}
|
||||
|
||||
this._src = file;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get or set the destination folder
|
||||
*
|
||||
* @param {String} dir
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Imagemin.prototype.dest = function (dir) {
|
||||
if (!arguments.length) {
|
||||
return this._dest;
|
||||
if (plugins.length === 0) {
|
||||
return input;
|
||||
}
|
||||
|
||||
this._dest = dir;
|
||||
return this;
|
||||
return pPipe(...plugins)(input);
|
||||
};
|
||||
|
||||
/**
|
||||
* Add a plugin to the middleware stack
|
||||
*
|
||||
* @param {Function} plugin
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Imagemin.prototype.use = function (plugin) {
|
||||
this.streams.push(typeof plugin === 'function' ? plugin() : plugin);
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Optimize files
|
||||
*
|
||||
* @param {Function} cb
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Imagemin.prototype.run = function (cb) {
|
||||
cb = cb || function () {};
|
||||
|
||||
var stream = this.createStream();
|
||||
|
||||
stream.on('error', cb);
|
||||
stream.pipe(concatStream({encoding: 'object'}, cb.bind(null, null)));
|
||||
};
|
||||
|
||||
/**
|
||||
* Create stream
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Imagemin.prototype.createStream = function () {
|
||||
this.streams.unshift(this.getFiles());
|
||||
|
||||
if (this.streams.length === 1) {
|
||||
this.use(Imagemin.gifsicle());
|
||||
this.use(Imagemin.jpegtran());
|
||||
this.use(Imagemin.optipng());
|
||||
this.use(Imagemin.svgo());
|
||||
}
|
||||
|
||||
if (this.dest()) {
|
||||
this.streams.push(vinylFs.dest(this.dest()));
|
||||
}
|
||||
|
||||
return streamCombiner.obj(this.streams);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get files
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Imagemin.prototype.getFiles = function () {
|
||||
if (Buffer.isBuffer(this.src())) {
|
||||
return bufferToVinyl.stream(this.src());
|
||||
}
|
||||
|
||||
return vinylFs.src(this.src());
|
||||
};
|
||||
|
||||
/**
|
||||
* Module exports
|
||||
*/
|
||||
|
||||
module.exports = Imagemin;
|
||||
|
||||
[
|
||||
'gifsicle',
|
||||
'jpegtran',
|
||||
'optipng',
|
||||
'svgo'
|
||||
].forEach(function (plugin) {
|
||||
module.exports[plugin] = optional('imagemin-' + plugin) || function () {
|
||||
return function () {
|
||||
return new PassThrough({objectMode: true});
|
||||
};
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user