├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── fixture-corrupt.jpg ├── fixture.jpg ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 20 14 | - 18 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: actions/setup-node@v4 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /fixture-corrupt.jpg: -------------------------------------------------------------------------------- 1 | RIFFWEBPVP8L/Alok -------------------------------------------------------------------------------- /fixture.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sindresorhus/gulp-webp/09aaf4c7139e2b5d7c03dca0080c1f23d63edef1/fixture.jpg -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import imageminWebp from 'imagemin-webp'; 2 | import {gulpPlugin} from 'gulp-plugin-extras'; 3 | 4 | const supportedExtensions = new Set([ 5 | 'png', 6 | 'jpg', 7 | 'jpeg', 8 | 'tif', 9 | 'tiff', 10 | 'webp', 11 | ]); 12 | 13 | export default function gulpWebp(options) { 14 | const instance = imageminWebp(options); 15 | 16 | return gulpPlugin('gulp-webp', async file => { 17 | if (!supportedExtensions.has(file.extname.slice(1).toLowerCase())) { 18 | return; 19 | } 20 | 21 | file.contents = await instance(file.contents); 22 | file.extname = '.webp'; 23 | return file; 24 | }); 25 | } 26 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-webp", 3 | "version": "5.0.0", 4 | "description": "Convert images to WebP", 5 | "license": "MIT", 6 | "repository": "sindresorhus/gulp-webp", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": ">=18" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava" 20 | }, 21 | "files": [ 22 | "index.js" 23 | ], 24 | "keywords": [ 25 | "gulpplugin", 26 | "webp", 27 | "image", 28 | "picture", 29 | "photo", 30 | "png", 31 | "jpg", 32 | "jpeg", 33 | "gif" 34 | ], 35 | "dependencies": { 36 | "gulp-plugin-extras": "^0.2.1", 37 | "imagemin-webp": "^8.0.0" 38 | }, 39 | "devDependencies": { 40 | "ava": "^5.3.1", 41 | "file-type": "^18.6.0", 42 | "p-event": "^6.0.0", 43 | "vinyl": "^3.0.0", 44 | "vinyl-file": "^5.0.0", 45 | "xo": "^0.56.0" 46 | }, 47 | "peerDependencies": { 48 | "gulp": ">=4" 49 | }, 50 | "peerDependenciesMeta": { 51 | "gulp": { 52 | "optional": true 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # gulp-webp 2 | 3 | > Convert images to [WebP](https://developers.google.com/speed/webp/) 4 | 5 | Supports PNG, JPEG, TIFF, WebP. 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install --save-dev gulp-webp 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | import gulp from 'gulp'; 17 | import webp from 'gulp-webp'; 18 | 19 | export default () => ( 20 | gulp.src('src/image.jpg') 21 | .pipe(webp()) 22 | .pipe(gulp.dest('dist')) 23 | ); 24 | ``` 25 | 26 | ## API 27 | 28 | ### webp(options?) 29 | 30 | See the `imagemin-webp` [options](https://github.com/imagemin/imagemin-webp#imageminwebpoptions). 31 | 32 | Unsupported files are ignored and passed through. 33 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import {Buffer} from 'node:buffer'; 2 | import path from 'node:path'; 3 | import {fileURLToPath} from 'node:url'; 4 | import {fileTypeFromBuffer} from 'file-type'; 5 | import test from 'ava'; 6 | import Vinyl from 'vinyl'; 7 | import {vinylFile} from 'vinyl-file'; 8 | import {pEvent} from 'p-event'; 9 | import webp from './index.js'; 10 | 11 | const __dirname = path.dirname(fileURLToPath(import.meta.url)); 12 | 13 | test('converts images to WebP', async t => { 14 | const file = await vinylFile(path.join(__dirname, 'fixture.jpg')); 15 | const stream = webp(); 16 | const promise = pEvent(stream, 'data'); 17 | stream.end(file); 18 | const data = await promise; 19 | const {mime} = await fileTypeFromBuffer(file.contents); 20 | t.is(mime, 'image/webp'); 21 | t.is(data.path, path.join(__dirname, 'fixture.webp')); 22 | }); 23 | 24 | test('should not convert unsupported files', async t => { 25 | const stream = webp(); 26 | 27 | const promise = pEvent(stream, 'data'); 28 | stream.end(new Vinyl({ 29 | path: path.join(__dirname, 'fixture.jpg'), 30 | contents: Buffer.from('contents'), 31 | })); 32 | 33 | const data = await promise; 34 | t.is(data.contents.toString(), 'contents'); 35 | }); 36 | 37 | test('emits a plugin error when the image is corrupt', async t => { 38 | const fileName = path.join(__dirname, 'fixture-corrupt.jpg'); 39 | const file = await vinylFile(fileName); 40 | const stream = webp(); 41 | 42 | const promise = pEvent(stream, 'error'); 43 | stream.end(file); 44 | 45 | const error = await promise; 46 | t.is(error.plugin, 'gulp-webp'); 47 | t.is(error.fileName, fileName); 48 | }); 49 | --------------------------------------------------------------------------------