diff --git a/.editorconfig b/.editorconfig index dd139df..1c6314a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,17 +1,12 @@ -# editorconfig.org root = true [*] -indent_style = space -indent_size = 4 +indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true -[package.json] +[*.yml] indent_style = space indent_size = 2 - -[*.md] -trim_trailing_whitespace = false diff --git a/.gitattributes b/.gitattributes index 176a458..391f0a4 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,2 @@ * text=auto +*.js text eol=lf diff --git a/.gitignore b/.gitignore index 0da72a2..3c3629e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1 @@ node_modules -npm-debug.log -tmp -tmp-cache/ diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index 507f912..0000000 --- a/.jshintrc +++ /dev/null @@ -1,14 +0,0 @@ -{ - "boss": true, - "curly": true, - "eqeqeq": true, - "eqnull": true, - "immed": true, - "latedef": true, - "newcap": true, - "noarg": true, - "node": true, - "sub": true, - "undef": true, - "unused": true -} diff --git a/.travis.yml b/.travis.yml index f63bfeb..e631c40 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,15 +1,7 @@ -sudo: false language: node_js node_js: - - "0.10" - - "0.12" - - "4" - - "5" - - "iojs" -before_install: - - if [ "$TRAVIS_NODE_VERSION" = "0.10" ]; then npm install -g npm@2; fi -matrix: - fast_finish: true -cache: - directories: - - node_modules + - '8' + - '6' + - '4' + - '0.12' + - '0.10' diff --git a/Gruntfile.js b/Gruntfile.js deleted file mode 100644 index 7bc546e..0000000 --- a/Gruntfile.js +++ /dev/null @@ -1,64 +0,0 @@ -/* - * grunt-contrib-imagemin - * http://gruntjs.com/ - * - * Copyright (c) 2016 Sindre Sorhus, contributors - * Licensed under the MIT license. - */ - -'use strict'; - -module.exports = function (grunt) { - require('time-grunt')(grunt); - - grunt.initConfig({ - jshint: { - all: [ - 'Gruntfile.js', - 'tasks/*.js', - '<%= nodeunit.tests %>' - ], - options: { - jshintrc: '.jshintrc' - } - }, - clean: { - test: ['tmp'] - }, - imagemin: { - dist: { - files: [{ - expand: true, - cwd: 'test/fixtures', - src: '**/*.{gif,GIF,jpg,JPG,png,PNG}', - dest: 'tmp' - }] - }, - rename: { - files: { - 'tmp/rename.jpg': 'test/fixtures/test.jpg' - } - } - }, - nodeunit: { - tests: ['test/test.js'] - } - }); - - grunt.loadTasks('tasks'); - - grunt.loadNpmTasks('grunt-contrib-clean'); - grunt.loadNpmTasks('grunt-contrib-jshint'); - grunt.loadNpmTasks('grunt-contrib-nodeunit'); - grunt.loadNpmTasks('grunt-contrib-internal'); - - grunt.registerTask('test', [ - 'jshint', - 'clean', - 'imagemin', - 'nodeunit', - 'clean' - ]); - - grunt.registerTask('default', ['test', 'build-contrib']); -}; diff --git a/LICENSE-MIT b/LICENSE-MIT deleted file mode 100644 index 8243c3f..0000000 --- a/LICENSE-MIT +++ /dev/null @@ -1,22 +0,0 @@ -Copyright (c) 2016 Sindre Sorhus, contributors - -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. diff --git a/README.md b/README.md deleted file mode 100644 index 85eea95..0000000 --- a/README.md +++ /dev/null @@ -1,164 +0,0 @@ -# grunt-contrib-imagemin v1.0.1 [![Build Status: Linux](https://travis-ci.org/gruntjs/grunt-contrib-imagemin.svg?branch=master)](https://travis-ci.org/gruntjs/grunt-contrib-imagemin) [![Build Status: Windows](https://ci.appveyor.com/api/projects/status/7w491e6edsuanreu/branch/master?svg=true)](https://ci.appveyor.com/project/gruntjs/grunt-contrib-imagemin/branch/master) - -> Minify images - - - -## Getting Started - -If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command: - -```shell -npm install grunt-contrib-imagemin --save-dev -``` - -Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript: - -```js -grunt.loadNpmTasks('grunt-contrib-imagemin'); -``` - - - - -## Imagemin task -_Run this task with the `grunt imagemin` command._ - -Minify images using [imagemin](https://github.com/imagemin/imagemin). - -Comes bundled with the following optimizers: - -- [gifsicle](https://github.com/imagemin/imagemin-gifsicle) — *Compress GIF images* -- [jpegtran](https://github.com/imagemin/imagemin-jpegtran) — *Compress JPEG images* -- [optipng](https://github.com/imagemin/imagemin-optipng) — *Compress PNG images* -- [svgo](https://github.com/imagemin/imagemin-svgo) — *Compress SVG images* - -We recommend using [grunt-newer](https://github.com/tschaub/grunt-newer) to only process changed files as minifying images can be quite slow. - -### Options - -Options will only apply to the relevant files, so you don't need separate targets for png/jpg. - - -#### optimizationLevel *(png)* - -Type: `Number` -Default: `3` - -Select optimization level between `0` and `7`. - -> The optimization level 0 enables a set of optimization operations that require minimal effort. There will be no changes to image attributes like bit depth or color type, and no recompression of existing IDAT datastreams. The optimization level 1 enables a single IDAT compression trial. The trial chosen is what OptiPNG thinks it’s probably the most effective. The optimization levels 2 and higher enable multiple IDAT compression trials; the higher the level, the more trials. - -Level and trials: - -1. 1 trial -2. 8 trials -3. 16 trials -4. 24 trials -5. 48 trials -6. 120 trials -7. 240 trials - - -#### progressive *(jpg)* - -Type: `Boolean` -Default: `true` - -Lossless conversion to progressive. - - -#### interlaced *(gif)* - -Type: `Boolean` -Default: `true` - -Interlace gif for progressive rendering. - - -#### svgoPlugins *(svg)* - -Type: `array` -Default: `[]` - -Customize which SVGO plugins to use. [More here](https://github.com/sindresorhus/grunt-svgmin#available-optionsplugins). - - -#### use - -Type: `Array` -Default: `null` - -Additional [plugins](https://www.npmjs.com/browse/keyword/imageminplugin) to use with imagemin. - -#### Example config - -You can either map your files statically or [dynamically](http://gruntjs.com/configuring-tasks#building-the-files-object-dynamically). - -```js -var mozjpeg = require('imagemin-mozjpeg'); - -grunt.initConfig({ - imagemin: { // Task - static: { // Target - options: { // Target options - optimizationLevel: 3, - svgoPlugins: [{ removeViewBox: false }], - use: [mozjpeg()] - }, - files: { // Dictionary of files - 'dist/img.png': 'src/img.png', // 'destination': 'source' - 'dist/img.jpg': 'src/img.jpg', - 'dist/img.gif': 'src/img.gif' - } - }, - dynamic: { // Another target - files: [{ - expand: true, // Enable dynamic expansion - cwd: 'src/', // Src matches are relative to this path - src: ['**/*.{png,jpg,gif}'], // Actual patterns to match - dest: 'dist/' // Destination path prefix - }] - } - } -}); - -grunt.loadNpmTasks('grunt-contrib-imagemin'); -grunt.registerTask('default', ['imagemin']); -``` - - -## Release History - - * 2016-05-23   v1.0.1   Check for `data.contents` existence. - * 2015-11-09   v1.0.0   Update to imagemin ^4.0. - * 2015-03-22   v0.9.4   Add support for renaming files. - * 2015-02-11   v0.9.3   Remove pngquant. - * 2014-11-11   v0.9.2   Bump imagemin dependency. - * 2014-10-25   v0.9.1   Update plugin API. - * 2014-10-22   v0.9.0   Update to imagemin 2.0. - * 2014-08-27   v0.8.1   Bump dependencies. - * 2014-08-11   v0.8.0   Better output. Update to chalk 0.5.0. Fixes imagemin options. - * 2014-08-11   v0.7.2   Fix npm `EPEERINVALID`. - * 2014-05-31   v0.7.1   Caching original image size before optimization. Remove unused dependencies. - * 2014-04-29   v0.7.0   Update "imagemin" to 0.4 - * 2014-04-01   v0.6.1   Fix problem with corrupt images being created - * 2014-03-28   v0.6.0   Updated "imagemin" to 0.2. Added percentage to size saved view - fixes #167. `cache` option removed. Adds "pretty-bytes". - * 2014-01-13   v0.5.0   Extract the logic into an external lib [imagemin](https://github.com/kevva/imagemin). - * 2014-01-08   v0.4.1   Prevent "Maximum call stack size exceeded". Speed up loading this task by lazy requiring bin deps. - * 2013-11-22   v0.4.0   The `pngquant` option is now `false` by default instead of `true`. - * 2013-09-09   v0.3.0   Add `interlace` option for GIF files. - * 2013-08-16   v0.2.0   Add `gifsicle` and `pngquant`. Cache images so only changed images are optimized. Default `optimizationLevel` to `7` and `progressive` to `true`. - * 2013-04-10   v0.1.4   Fix exception when running in verbose mode. - * 2013-04-05   v0.1.3   Fix OptiPNG not being able to overwrite file. Allow overwriting src when dest/src is the same. Limit to 10 concurrent optimizations. - * 2013-02-22   v0.1.2   Fix OptiPNG not working on some systems. Prevent OptiPNG from producing .bak files. - * 2013-02-15   v0.1.1   First official release for Grunt 0.4.0. - * 2013-01-30   v0.1.1rc8   Fix task not creating destination folders. - * 2013-01-30   v0.1.1rc7   Updating to work with grunt v0.4.0rc7. Switching to `this.files` API. - * 2012-11-01   v0.1.0   Initial release. - ---- - -Task submitted by [Sindre Sorhus](http://github.com/sindresorhus) - -*This file was generated on Mon May 23 2016 17:23:51.* diff --git a/appveyor.yml b/appveyor.yml index a196c5e..b2892c6 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,44 +1,21 @@ -# http://www.appveyor.com/docs/appveyor-yml - -clone_depth: 10 - -version: "{build}" - -# What combinations to test environment: matrix: - - nodejs_version: "0.10" - platform: x86 - - nodejs_version: "0.12" - platform: x86 - - nodejs_version: "4" - platform: x64 - - nodejs_version: "4" - platform: x86 - - nodejs_version: "5" - platform: x86 - + - nodejs_version: '8' + - nodejs_version: '6' + - nodejs_version: '4' + - nodejs_version: '0.12' + - nodejs_version: '0.10' install: - - ps: Install-Product node $env:nodejs_version $env:platform - - ps: >- - if ($env:nodejs_version -eq "0.10") { - npm -g install npm@2 - $env:PATH="$env:APPDATA\npm;$env:PATH" - } + - ps: Install-Product node $env:nodejs_version + - set CI=true + - npm -g install npm@latest + - set PATH=%APPDATA%\npm;%PATH% - npm install - -test_script: - # Output useful info for debugging - - node --version && npm --version - # We test multiple Windows shells because of prior stdout buffering issues - # filed against Grunt. https://github.com/joyent/node/issues/3584 - - ps: "npm test # PowerShell" # Pass comment to PS for easier debugging - - cmd: npm test - -build: off - matrix: fast_finish: true - -cache: - - node_modules -> package.json +build: off +shallow_clone: true +test_script: + - node --version + - npm --version + - npm test diff --git a/AUTHORS b/authors similarity index 100% rename from AUTHORS rename to authors diff --git a/CHANGELOG b/changelog similarity index 100% rename from CHANGELOG rename to changelog diff --git a/CONTRIBUTING.md b/contributing.md similarity index 100% rename from CONTRIBUTING.md rename to contributing.md diff --git a/docs/imagemin-examples.md b/docs/imagemin-examples.md deleted file mode 100644 index 7fe9bee..0000000 --- a/docs/imagemin-examples.md +++ /dev/null @@ -1,35 +0,0 @@ -## Example config - -You can either map your files statically or [dynamically](http://gruntjs.com/configuring-tasks#building-the-files-object-dynamically). - -```js -var mozjpeg = require('imagemin-mozjpeg'); - -grunt.initConfig({ - imagemin: { // Task - static: { // Target - options: { // Target options - optimizationLevel: 3, - svgoPlugins: [{ removeViewBox: false }], - use: [mozjpeg()] - }, - files: { // Dictionary of files - 'dist/img.png': 'src/img.png', // 'destination': 'source' - 'dist/img.jpg': 'src/img.jpg', - 'dist/img.gif': 'src/img.gif' - } - }, - dynamic: { // Another target - files: [{ - expand: true, // Enable dynamic expansion - cwd: 'src/', // Src matches are relative to this path - src: ['**/*.{png,jpg,gif}'], // Actual patterns to match - dest: 'dist/' // Destination path prefix - }] - } - } -}); - -grunt.loadNpmTasks('grunt-contrib-imagemin'); -grunt.registerTask('default', ['imagemin']); -``` diff --git a/docs/imagemin-options.md b/docs/imagemin-options.md deleted file mode 100644 index fde7371..0000000 --- a/docs/imagemin-options.md +++ /dev/null @@ -1,55 +0,0 @@ -# Options - -Options will only apply to the relevant files, so you don't need separate targets for png/jpg. - - -## optimizationLevel *(png)* - -Type: `Number` -Default: `3` - -Select optimization level between `0` and `7`. - -> The optimization level 0 enables a set of optimization operations that require minimal effort. There will be no changes to image attributes like bit depth or color type, and no recompression of existing IDAT datastreams. The optimization level 1 enables a single IDAT compression trial. The trial chosen is what OptiPNG thinks it’s probably the most effective. The optimization levels 2 and higher enable multiple IDAT compression trials; the higher the level, the more trials. - -Level and trials: - -1. 1 trial -2. 8 trials -3. 16 trials -4. 24 trials -5. 48 trials -6. 120 trials -7. 240 trials - - -## progressive *(jpg)* - -Type: `Boolean` -Default: `true` - -Lossless conversion to progressive. - - -## interlaced *(gif)* - -Type: `Boolean` -Default: `true` - -Interlace gif for progressive rendering. - - -## svgoPlugins *(svg)* - -Type: `array` -Default: `[]` - -Customize which SVGO plugins to use. [More here](https://github.com/sindresorhus/grunt-svgmin#available-optionsplugins). - - -## use - -Type: `Array` -Default: `null` - -Additional [plugins](https://www.npmjs.com/browse/keyword/imageminplugin) to use with imagemin. diff --git a/docs/imagemin-overview.md b/docs/imagemin-overview.md deleted file mode 100644 index 6f1a80a..0000000 --- a/docs/imagemin-overview.md +++ /dev/null @@ -1,10 +0,0 @@ -Minify images using [imagemin](https://github.com/imagemin/imagemin). - -Comes bundled with the following optimizers: - -- [gifsicle](https://github.com/imagemin/imagemin-gifsicle) — *Compress GIF images* -- [jpegtran](https://github.com/imagemin/imagemin-jpegtran) — *Compress JPEG images* -- [optipng](https://github.com/imagemin/imagemin-optipng) — *Compress PNG images* -- [svgo](https://github.com/imagemin/imagemin-svgo) — *Compress SVG images* - -We recommend using [grunt-newer](https://github.com/tschaub/grunt-newer) to only process changed files as minifying images can be quite slow. diff --git a/gruntfile.js b/gruntfile.js new file mode 100644 index 0000000..088e90f --- /dev/null +++ b/gruntfile.js @@ -0,0 +1,23 @@ +'use strict'; +module.exports = function (grunt) { + grunt.initConfig({ + imagemin: { + dist: { + files: [{ + expand: true, + cwd: 'test/fixtures', + src: '**/*.{gif,GIF,jpg,JPG,png,PNG}', + dest: 'tmp' + }] + }, + rename: { + files: { + 'tmp/rename.jpg': 'test/fixtures/test.jpg' + } + } + } + }); + + grunt.loadTasks('tasks'); + grunt.registerTask('default', ['imagemin']); +}; diff --git a/license b/license new file mode 100644 index 0000000..654d0bf --- /dev/null +++ b/license @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) Sindre Sorhus (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: + +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. diff --git a/package.json b/package.json index 28c10cb..adbdeb2 100644 --- a/package.json +++ b/package.json @@ -1,35 +1,22 @@ { "name": "grunt-contrib-imagemin", - "description": "Minify images", "version": "1.0.1", + "description": "Minify images", + "license": "MIT", + "repository": "gruntjs/grunt-contrib-imagemin", "author": { "name": "Grunt Team", "url": "http://gruntjs.com/" }, - "repository": "gruntjs/grunt-contrib-imagemin", - "license": "MIT", "engines": { "node": ">=0.10.0" }, - "main": "tasks/imagemin.js", "scripts": { - "test": "grunt test" - }, - "dependencies": { - "async": "^1.5.2", - "chalk": "^1.0.0", - "gulp-rename": "^1.2.0", - "imagemin": "^4.0.0", - "pretty-bytes": "^3.0.1" - }, - "devDependencies": { - "grunt": "^1.0.0", - "grunt-contrib-clean": "^1.0.0", - "grunt-contrib-internal": "^1.1.0", - "grunt-contrib-jshint": "^1.0.0", - "grunt-contrib-nodeunit": "^1.0.0", - "time-grunt": "^1.1.0" + "test": "xo && grunt && ava && del-cli tmp" }, + "files": [ + "tasks" + ], "keywords": [ "gruntplugin", "compress", @@ -42,8 +29,19 @@ "png", "svg" ], - "files": [ - "tasks" - ], + "dependencies": { + "async": "^1.5.2", + "chalk": "^1.0.0", + "gulp-rename": "^1.2.0", + "imagemin": "^4.0.0", + "pretty-bytes": "^3.0.1" + }, + "devDependencies": { + "ava": "^0.17.0", + "del-cli": "^0.2.1", + "grunt": "^1.0.0", + "grunt-cli": "^1.2.0", + "xo": "^0.16.0" + }, "appveyor_id": "7w491e6edsuanreu" } diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..8c80296 --- /dev/null +++ b/readme.md @@ -0,0 +1,106 @@ +# grunt-contrib-imagemin [![Build Status: Linux](https://travis-ci.org/gruntjs/grunt-contrib-imagemin.svg?branch=master)](https://travis-ci.org/gruntjs/grunt-contrib-imagemin) [![Build Status: Windows](https://ci.appveyor.com/api/projects/status/7w491e6edsuanreu/branch/master?svg=true)](https://ci.appveyor.com/project/gruntjs/grunt-contrib-imagemin/branch/master) + +> Minify images using [imagemin](https://github.com/imagemin/imagemin) + + +## Install + +``` +$ npm install --save-dev grunt-contrib-imagemin +``` + + +## Usage + +```js +const mozjpeg = require('imagemin-mozjpeg'); + +grunt.initConfig({ + imagemin: { + static: { + options: { + optimizationLevel: 3, + svgoPlugins: [{removeViewBox: false}], + use: [mozjpeg()] + }, + files: { + 'dist/img.png': 'src/img.png', + 'dist/img.jpg': 'src/img.jpg', + 'dist/img.gif': 'src/img.gif' + } + }, + dynamic: { + files: [{ + expand: true, + cwd: 'src/', + src: ['**/*.{png,jpg,gif}'], + dest: 'dist/' + }] + } + } +}); + +grunt.loadNpmTasks('grunt-contrib-imagemin'); +grunt.registerTask('default', ['imagemin']); +``` + + +## Options + +### optimizationLevel *(png)* + +Type: `number`
+Default: `3` + +Select optimization level between `0` and `7`. + +> The optimization level 0 enables a set of optimization operations that require minimal effort. There will be no changes to image attributes like bit depth or color type, and no recompression of existing IDAT datastreams. The optimization level 1 enables a single IDAT compression trial. The trial chosen is what OptiPNG thinks it’s probably the most effective. The optimization levels 2 and higher enable multiple IDAT compression trials; the higher the level, the more trials. + +Level and trials: + +1. 1 trial +2. 8 trials +3. 16 trials +4. 24 trials +5. 48 trials +6. 120 trials +7. 240 trials + +### progressive *(jpg)* + +Type: `boolean`
+Default: `true` + +Lossless conversion to progressive. + +### interlaced *(gif)* + +Type: `boolean`
+Default: `true` + +Interlace gif for progressive rendering. + +### svgoPlugins *(svg)* + +Type: `Array` + +Customize which SVGO plugins to use. [More here](https://github.com/sindresorhus/grunt-svgmin#available-optionsplugins). + +### use + +Type: `Array`
+Default: `[imagemin.gifsicle(), imagemin.jpegtran(), imagemin.optipng(), imagemin.svgo()]` + +[Plugins](https://www.npmjs.com/browse/keyword/imageminplugin) to use with imagemin. It comes bundled with the following **lossless** optimizers: + +- [gifsicle](https://github.com/imagemin/imagemin-gifsicle) — *Compress GIF images* +- [jpegtran](https://github.com/imagemin/imagemin-jpegtran) — *Compress JPEG images* +- [optipng](https://github.com/imagemin/imagemin-optipng) — *Compress PNG images* +- [svgo](https://github.com/imagemin/imagemin-svgo) — *Compress SVG images* + +These are bundled for convenience and most users will not need anything else. + + +## License + +MIT © [Sindre Sorhus](https://sindresorhus.com) diff --git a/tasks/imagemin.js b/tasks/imagemin.js index fa462a3..06a6025 100644 --- a/tasks/imagemin.js +++ b/tasks/imagemin.js @@ -8,86 +8,78 @@ var prettyBytes = require('pretty-bytes'); var Imagemin = require('imagemin'); var rename = require('gulp-rename'); -/* - * grunt-contrib-imagemin - * http://gruntjs.com/ - * - * Copyright (c) 2016 Sindre Sorhus, contributors - * Licensed under the MIT license. - */ - module.exports = function (grunt) { - grunt.registerMultiTask('imagemin', 'Minify PNG, JPEG, GIF and SVG images', function () { - var done = this.async(); - var files = this.files; - var totalSaved = 0; - var options = this.options({ - interlaced: true, - optimizationLevel: 3, - progressive: true - }); + grunt.registerMultiTask('imagemin', 'Minify PNG, JPEG, GIF and SVG images', function () { + var done = this.async(); + var files = this.files; + var totalSaved = 0; + var options = this.options({ + interlaced: true, + optimizationLevel: 3, + progressive: true + }); - async.eachLimit(files, os.cpus().length, function (file, next) { - var msg; - var imagemin = new Imagemin() - .src(file.src[0]) - .dest(path.dirname(file.dest)) - .use(Imagemin.jpegtran(options)) - .use(Imagemin.gifsicle(options)) - .use(Imagemin.optipng(options)) - .use(Imagemin.svgo({plugins: options.svgoPlugins || []})); + async.eachLimit(files, os.cpus().length, function (file, next) { + var msg; + var imagemin = new Imagemin() + .src(file.src[0]) + .dest(path.dirname(file.dest)) + .use(Imagemin.jpegtran(options)) + .use(Imagemin.gifsicle(options)) + .use(Imagemin.optipng(options)) + .use(Imagemin.svgo({plugins: options.svgoPlugins || []})); - if (options.use) { - options.use.forEach(imagemin.use.bind(imagemin)); - } + if (options.use) { + options.use.forEach(imagemin.use.bind(imagemin)); + } - if (path.basename(file.src[0]) !== path.basename(file.dest)) { - imagemin.use(rename(path.basename(file.dest))); - } + if (path.basename(file.src[0]) !== path.basename(file.dest)) { + imagemin.use(rename(path.basename(file.dest))); + } - fs.stat(file.src[0], function (err, stats) { - if (err) { - grunt.warn(err + ' in file ' + file.src[0]); - return next(); - } + fs.stat(file.src[0], function (err, stats) { + if (err) { + grunt.warn(err + ' in file ' + file.src[0]); + return next(); + } - imagemin.run(function (err, data) { - if (err) { - grunt.warn(err + ' in file ' + file.src[0]); - return next(); - } + imagemin.run(function (err, data) { + if (err) { + grunt.warn(err + ' in file ' + file.src[0]); + return next(); + } - var origSize = stats.size; - var diffSize = origSize - ((data[0].contents && data[0].contents.length) || 0); + var origSize = stats.size; + var diffSize = origSize - ((data[0].contents && data[0].contents.length) || 0); - totalSaved += diffSize; + totalSaved += diffSize; - if (diffSize < 10) { - msg = 'already optimized'; - } else { - msg = [ - 'saved ' + prettyBytes(diffSize) + ' -', - (diffSize / origSize * 100).toFixed() + '%' - ].join(' '); - } + if (diffSize < 10) { + msg = 'already optimized'; + } else { + msg = [ + 'saved ' + prettyBytes(diffSize) + ' -', + (diffSize / origSize * 100).toFixed() + '%' + ].join(' '); + } - grunt.verbose.writeln(chalk.green('✔ ') + file.src[0] + chalk.gray(' (' + msg + ')')); - process.nextTick(next); - }); - }); - }, function (err) { - if (err) { - grunt.warn(err); - } + grunt.verbose.writeln(chalk.green('✔ ') + file.src[0] + chalk.gray(' (' + msg + ')')); + process.nextTick(next); + }); + }); + }, function (err) { + if (err) { + grunt.warn(err); + } - var msg = [ - 'Minified ' + files.length, - files.length === 1 ? 'image' : 'images', - chalk.gray('(saved ' + prettyBytes(totalSaved) + ')') - ].join(' '); + var msg = [ + 'Minified ' + files.length, + files.length === 1 ? 'image' : 'images', + chalk.gray('(saved ' + prettyBytes(totalSaved) + ')') + ].join(' '); - grunt.log.writeln(msg); - done(); - }); - }); + grunt.log.writeln(msg); + done(); + }); + }); }; diff --git a/test/test.js b/test/test.js index 64e15a5..2d8d492 100644 --- a/test/test.js +++ b/test/test.js @@ -1,74 +1,56 @@ -'use strict'; +import fs from 'fs'; +import path from 'path'; +import test from 'ava'; -var fs = require('fs'); +const fixture = path.join.bind(path, __dirname, 'fixtures'); +const tmp = path.join.bind(path, __dirname, '..', 'tmp'); -exports.imagemin = { - minifyPng: function (test) { - test.expect(1); +test('minify png', t => { + const original = fs.readFileSync(fixture('test.png')); + const actual = fs.readFileSync(tmp('test.png')); - var actual = fs.statSync('tmp/test.png').size; - var original = fs.statSync('test/fixtures/test.png').size; - test.ok(actual < original, 'should minify PNG images'); + t.true(actual.length < original.length); +}); - test.done(); - }, - minifyUppercasePng: function (test) { - test.expect(1); +test('minify uppercase png', t => { + const original = fs.readFileSync(fixture('test-uppercase.PNG')); + const actual = fs.readFileSync(tmp('test-uppercase.PNG')); - var actual = fs.statSync('tmp/test-uppercase.PNG').size; - var original = fs.statSync('test/fixtures/test-uppercase.PNG').size; - test.ok(actual < original, 'should minify uppercase extension PNG images'); + t.true(actual.length < original.length); +}); - test.done(); - }, - minifyJpg: function (test) { - test.expect(1); +test('minify jpg', t => { + const original = fs.readFileSync(fixture('test.jpg')); + const actual = fs.readFileSync(tmp('test.jpg')); - var actual = fs.statSync('tmp/test.jpg').size; - var original = fs.statSync('test/fixtures/test.jpg').size; - test.ok(actual < original, 'should minify JPG images'); + t.true(actual.length < original.length); +}); - test.done(); - }, - minifyUppercaseJpg: function (test) { - test.expect(1); +test('minify uppercase jpg', t => { + const original = fs.readFileSync(fixture('test-uppercase.JPG')); + const actual = fs.readFileSync(tmp('test-uppercase.JPG')); - var actual = fs.statSync('tmp/test-uppercase.JPG').size; - var original = fs.statSync('test/fixtures/test-uppercase.JPG').size; - test.ok(actual < original, 'should minify uppercase extension JPG images'); + t.true(actual.length < original.length); +}); - test.done(); - }, - minifyGif: function (test) { - test.expect(1); +test('minify gif', t => { + const original = fs.readFileSync(fixture('test.gif')); + const actual = fs.readFileSync(tmp('test.gif')); - var actual = fs.statSync('tmp/test.gif').size; - var original = fs.statSync('test/fixtures/test.gif').size; - test.ok(actual < original, 'should minify GIF images'); + t.true(actual.length < original.length); +}); - test.done(); - }, - minifyUppercaseGif: function (test) { - test.expect(1); +test('minify uppercase gif', t => { + const original = fs.readFileSync(fixture('test-uppercase.GIF')); + const actual = fs.readFileSync(tmp('test-uppercase.GIF')); - var actual = fs.statSync('tmp/test-uppercase.GIF').size; - var original = fs.statSync('test/fixtures/test-uppercase.GIF').size; - test.ok(actual < original, 'should minify uppercase extension GIF images'); + t.true(actual.length < original.length); +}); - test.done(); - }, - nestedDirs: function (test) { - test.expect(1); +test('nested directories', t => { + t.true(fs.existsSync(tmp('nested', 'nested', 'test.png'))); +}); - test.ok(fs.existsSync('tmp/nested/nested/test.png'), 'should support nested dest dir'); - - test.done(); - }, - rename: function (test) { - test.expect(1); - - test.ok(fs.existsSync('tmp/rename.jpg'), 'should support renaming'); - - test.done(); - } -}; +test('rename', t => { + t.true(fs.existsSync(tmp('rename.jpg'))); +});