Skip to content

Commit

Permalink
Require Node.js 18 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 1, 2023
1 parent 05231b6 commit 5b9f53e
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 80 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

40 changes: 10 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,25 @@
'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',
'jpg',
'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;
});
};
}
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (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:

Expand Down
30 changes: 18 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -22,7 +25,6 @@
"gulpplugin",
"webp",
"image",
"img",
"picture",
"photo",
"png",
Expand All @@ -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
}
}
}
24 changes: 8 additions & 16 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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.
33 changes: 17 additions & 16 deletions test.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
});

0 comments on commit 5b9f53e

Please sign in to comment.