From 5b9f53ed4f695967b82ba13ae1c22a46a37f04fe Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 28 May 2019 13:37:29 +0700 Subject: [PATCH] Require Node.js 18 and move to ESM --- .github/workflows/main.yml | 21 ++++++++++++++++++++ .travis.yml | 5 ----- index.js | 40 ++++++++++---------------------------- license | 2 +- package.json | 30 ++++++++++++++++------------ readme.md | 24 ++++++++--------------- test.js | 33 ++++++++++++++++--------------- 7 files changed, 75 insertions(+), 80 deletions(-) create mode 100644 .github/workflows/main.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..346585c --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,21 @@ +name: CI +on: + - push + - pull_request +jobs: + test: + name: Node.js ${{ matrix.node-version }} + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + node-version: + - 20 + - 18 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: ${{ matrix.node-version }} + - run: npm install + - run: npm test diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 2ae9d62..0000000 --- a/.travis.yml +++ /dev/null @@ -1,5 +0,0 @@ -language: node_js -node_js: - - '10' - - '8' - - '6' diff --git a/index.js b/index.js index 704a29d..c570e44 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,5 @@ -'use strict'; -const PluginError = require('plugin-error'); -const through = require('through2'); -const imageminWebp = require('imagemin-webp'); +import imageminWebp from 'imagemin-webp'; +import {gulpPlugin} from 'gulp-plugin-extras'; const supportedExtensions = new Set([ 'png', @@ -9,37 +7,19 @@ const supportedExtensions = new Set([ 'jpeg', 'tif', 'tiff', - 'webp' + 'webp', ]); -module.exports = options => { +export default function gulpWebp(options) { const instance = imageminWebp(options); - return through.obj((file, encoding, callback) => { - if (file.isNull()) { - callback(null, file); + return gulpPlugin('gulp-webp', async file => { + if (!supportedExtensions.has(file.extname.slice(1).toLowerCase())) { return; } - if (file.isStream()) { - callback(new PluginError('gulp-webp', 'Streaming not supported')); - return; - } - - const ext = file.extname.slice(1).toLowerCase(); - if (!supportedExtensions.has(ext)) { - callback(null, file); - return; - } - - instance(file.contents) - .then(result => { - file.contents = result; - file.extname = '.webp'; - callback(null, file); - }) - .catch(error => { - callback(new PluginError('gulp-webp', error, {fileName: file.path})); - }); + file.contents = await instance(file.contents); + file.extname = '.webp'; + return file; }); -}; +} diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (https://sindresorhus.com) 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: diff --git a/package.json b/package.json index 4540238..94de160 100644 --- a/package.json +++ b/package.json @@ -4,13 +4,16 @@ "description": "Convert images to WebP", "license": "MIT", "repository": "sindresorhus/gulp-webp", + "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" + "url": "https://sindresorhus.com" }, + "type": "module", + "exports": "./index.js", "engines": { - "node": ">=6" + "node": ">=18" }, "scripts": { "test": "xo && ava" @@ -22,7 +25,6 @@ "gulpplugin", "webp", "image", - "img", "picture", "photo", "png", @@ -31,19 +33,23 @@ "gif" ], "dependencies": { - "imagemin-webp": "^5.0.0", - "plugin-error": "^1.0.1", - "through2": "^3.0.0" + "gulp-plugin-extras": "^0.2.1", + "imagemin-webp": "^8.0.0" }, "devDependencies": { - "ava": "^1.1.0", - "file-type": "^10.7.0", - "p-event": "^2.2.0", - "vinyl": "^2.2.0", - "vinyl-file": "^3.0.0", - "xo": "^0.24.0" + "ava": "^5.3.1", + "file-type": "^18.6.0", + "p-event": "^6.0.0", + "vinyl": "^3.0.0", + "vinyl-file": "^5.0.0", + "xo": "^0.56.0" }, "peerDependencies": { "gulp": ">=4" + }, + "peerDependenciesMeta": { + "gulp": { + "optional": true + } } } diff --git a/readme.md b/readme.md index 9168f25..68530f3 100644 --- a/readme.md +++ b/readme.md @@ -1,40 +1,32 @@ -# gulp-webp [![Build Status](https://travis-ci.org/sindresorhus/gulp-webp.svg?branch=master)](https://travis-ci.org/sindresorhus/gulp-webp) +# gulp-webp > Convert images to [WebP](https://developers.google.com/speed/webp/) Supports PNG, JPEG, TIFF, WebP. - ## Install -``` -$ npm install --save-dev gulp-webp +```sh +npm install --save-dev gulp-webp ``` - ## Usage ```js -const gulp = require('gulp'); -const webp = require('gulp-webp'); +import gulp from 'gulp'; +import webp from 'gulp-webp'; -gulp.task('default', () => +export default () => ( gulp.src('src/image.jpg') .pipe(webp()) .pipe(gulp.dest('dist')) ); ``` - ## API -Note that unsupported files are ignored. - -### webp([options]) +### webp(options?) See the `imagemin-webp` [options](https://github.com/imagemin/imagemin-webp#imageminwebpoptions). - -## License - -MIT © [Sindre Sorhus](https://sindresorhus.com) +Unsupported files are ignored and passed through. diff --git a/test.js b/test.js index c730701..4245516 100644 --- a/test.js +++ b/test.js @@ -1,31 +1,33 @@ -import path from 'path'; -import fileType from 'file-type'; +import {Buffer} from 'node:buffer'; +import path from 'node:path'; +import {fileURLToPath} from 'node:url'; +import {fileTypeFromBuffer} from 'file-type'; import test from 'ava'; import Vinyl from 'vinyl'; -import vinylFile from 'vinyl-file'; -import PluginError from 'plugin-error'; -import pEvent from 'p-event'; -import gulpWebp from '.'; +import {vinylFile} from 'vinyl-file'; +import {pEvent} from 'p-event'; +import webp from './index.js'; -test('converts images to WebP', async t => { - const file = await vinylFile.read(path.join(__dirname, 'fixture.jpg')); - const stream = gulpWebp(); +const __dirname = path.dirname(fileURLToPath(import.meta.url)); +test('converts images to WebP', async t => { + const file = await vinylFile(path.join(__dirname, 'fixture.jpg')); + const stream = webp(); const promise = pEvent(stream, 'data'); stream.end(file); const data = await promise; - - t.is(fileType(file.contents).mime, 'image/webp'); + const {mime} = await fileTypeFromBuffer(file.contents); + t.is(mime, 'image/webp'); t.is(data.path, path.join(__dirname, 'fixture.webp')); }); test('should not convert unsupported files', async t => { - const stream = gulpWebp(); + const stream = webp(); const promise = pEvent(stream, 'data'); stream.end(new Vinyl({ path: path.join(__dirname, 'fixture.jpg'), - contents: Buffer.from('contents') + contents: Buffer.from('contents'), })); const data = await promise; @@ -34,14 +36,13 @@ test('should not convert unsupported files', async t => { test('emits a plugin error when the image is corrupt', async t => { const fileName = path.join(__dirname, 'fixture-corrupt.jpg'); - const file = await vinylFile.read(fileName); - const stream = gulpWebp(); + const file = await vinylFile(fileName); + const stream = webp(); const promise = pEvent(stream, 'error'); stream.end(file); const error = await promise; - t.true(error instanceof PluginError); t.is(error.plugin, 'gulp-webp'); t.is(error.fileName, fileName); });