gulp-svgmin
Advanced tools
Comparing version 3.0.0 to 4.0.0
@@ -1,49 +0,46 @@ | ||
'use strict'; | ||
"use strict"; | ||
var _stream = require('stream'); | ||
var _pluginError = _interopRequireDefault(require("plugin-error")); | ||
var _svgo = require('svgo'); | ||
var _stream = require("stream"); | ||
var _svgo2 = _interopRequireDefault(_svgo); | ||
var _svgo = require("svgo"); | ||
var _pluginError = require('plugin-error'); | ||
var _getSvgoConfig = require("./get-svgo-config.js"); | ||
var _pluginError2 = _interopRequireDefault(_pluginError); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var PLUGIN_NAME = 'gulp-svgmin'; | ||
const PLUGIN_NAME = 'gulp-svgmin'; | ||
module.exports = function (opts) { | ||
var stream = new _stream.Transform({ objectMode: true }); | ||
var svgo = void 0; | ||
module.exports = function (options) { | ||
const optionsFunction = typeof options === 'function'; | ||
const stream = new _stream.Transform({ | ||
objectMode: true | ||
}); | ||
if (typeof opts !== 'function') { | ||
svgo = new _svgo2.default(opts); | ||
stream._transform = function (file, encoding, cb) { | ||
if (file.isStream()) { | ||
return cb(new _pluginError.default(PLUGIN_NAME, 'Streaming not supported')); | ||
} | ||
stream._transform = function (file, encoding, cb) { | ||
if (file.isNull()) { | ||
return cb(null, file); | ||
} | ||
if (file.isBuffer()) { | ||
(0, _getSvgoConfig.getSvgoConfig)(optionsFunction ? options(file) : options, optionsFunction).then(config => { | ||
const result = (0, _svgo.optimize)(String(file.contents), config); // Ignore svgo meta data and return the SVG string. | ||
if (file.isStream()) { | ||
return cb(new _pluginError2.default(PLUGIN_NAME, 'Streaming not supported')); | ||
} | ||
if (typeof result.data === 'string') { | ||
file.contents = Buffer.from(result.data); | ||
return cb(null, file); | ||
} // Otherwise, throw an error, even if it is undefined. | ||
if (file.isBuffer()) { | ||
if (typeof opts === 'function') { | ||
svgo = new _svgo2.default(opts(file)); | ||
} | ||
svgo.optimize(String(file.contents)).then(function (result) { | ||
file.contents = Buffer.from(result.data); | ||
cb(null, file); | ||
}, function (error) { | ||
cb(new _pluginError2.default(PLUGIN_NAME, error)); | ||
}); | ||
} | ||
}; | ||
throw result.error; | ||
}).catch(error => cb(new _pluginError.default(PLUGIN_NAME, error))); | ||
return; | ||
} // Handle all other cases, like file.isNull(), file.isDirectory(). | ||
return stream; | ||
return cb(null, file); | ||
}; | ||
return stream; | ||
}; |
{ | ||
"name": "gulp-svgmin", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "Minify SVG files with gulp.", | ||
@@ -10,9 +10,9 @@ "license": "MIT", | ||
"email": "beneb.info@gmail.com", | ||
"url": "http://beneb.info" | ||
"url": "https://beneb.info" | ||
}, | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"pretest": "eslint src", | ||
"prepublishOnly": "del-cli dist && cross-env BABEL_ENV=publish babel src --out-dir dist --ignore /__tests__/", | ||
"test": "nyc ava src/__tests__" | ||
"pretest": "xo src tests", | ||
"prepublishOnly": "del-cli dist && cross-env BABEL_ENV=publish babel src --out-dir dist", | ||
"test": "nyc ava" | ||
}, | ||
@@ -32,26 +32,32 @@ "files": [ | ||
"plugin-error": "^1.0.1", | ||
"svgo": "^1.3.2" | ||
"svgo": "^2.3.1" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.17.0", | ||
"babel-cli": "^6.26.0", | ||
"babel-core": "^6.26.3", | ||
"babel-preset-env": "^1.7.0", | ||
"babel-register": "^6.26.0", | ||
"chai": "^4.2.0", | ||
"@babel/cli": "^7.14.5", | ||
"@babel/core": "^7.14.6", | ||
"@babel/preset-env": "^7.14.7", | ||
"@babel/register": "^7.14.5", | ||
"ava": "^3.15.0", | ||
"coveralls": "^3.1.0", | ||
"cross-env": "^7.0.2", | ||
"del-cli": "^3.0.1", | ||
"eslint": "^3.0.0", | ||
"eslint-config-cssnano": "^3.1.3", | ||
"eslint-plugin-babel": "^3.3.0", | ||
"eslint-plugin-import": "^2.21.2", | ||
"cross-env": "^7.0.3", | ||
"del-cli": "^4.0.0", | ||
"lodash.clonedeep": "^4.5.0", | ||
"nyc": "^15.1.0", | ||
"vinyl": "^2.2.0" | ||
"prettier": "^2.3.2", | ||
"vinyl": "^2.2.1", | ||
"xo": "^0.38.2" | ||
}, | ||
"eslintConfig": { | ||
"extends": "cssnano" | ||
"prettier": { | ||
"bracketSpacing": false, | ||
"trailingComma": "es5", | ||
"tabWidth": 4, | ||
"semi": true, | ||
"singleQuote": true | ||
}, | ||
"xo": { | ||
"prettier": true | ||
}, | ||
"ava": { | ||
"require": "babel-register" | ||
"require": "@babel/register", | ||
"verbose": true | ||
}, | ||
@@ -61,5 +67,13 @@ "nyc": { | ||
"node_modules", | ||
"**/__tests__" | ||
"tests" | ||
], | ||
"check-coverage": true, | ||
"functions": 100, | ||
"lines": 100, | ||
"branches": 100, | ||
"reporter": [ | ||
"text", | ||
"html" | ||
] | ||
} | ||
} |
202
README.md
@@ -1,12 +0,15 @@ | ||
# [gulp][gulp]-svgmin [![Build Status](https://travis-ci.org/ben-eb/gulp-svgmin.svg?branch=master)][ci] [![NPM version](https://badge.fury.io/js/gulp-svgmin.svg)][npm] [![Dependency Status](https://gemnasium.com/ben-eb/gulp-svgmin.svg)][deps] | ||
# gulp-svgmin | ||
> Minify SVG with [SVGO][orig]. | ||
[![Build Status](https://travis-ci.org/ben-eb/gulp-svgmin.svg?branch=master)][travis-status] | ||
[![NPM version](https://badge.fury.io/js/gulp-svgmin.svg)][npm-status] | ||
[![Dependency Status](https://david-dm.org/ben-eb/gulp-svgmin.svg)][deps-status] | ||
*If you have any difficulties with the output of this plugin, please use the | ||
[SVGO tracker][bugs].* | ||
> A [Gulp][gulp-url] plugin to minify SVG files with [svgo-url]. | ||
*If you have any difficulties with the output of this plugin, please use the [SVGO tracker][svgo-bugs].* | ||
## Install | ||
With [npm](https://npmjs.org/package/gulp-svgmin) do: | ||
With [npm][npm-url] do: | ||
@@ -17,68 +20,100 @@ ``` | ||
## Example | ||
```js | ||
var gulp = require('gulp'); | ||
var svgmin = require('gulp-svgmin'); | ||
import { src, dest } from 'gulp'; | ||
import svgmin from 'gulp-svgmin'; | ||
gulp.task('default', function () { | ||
return gulp.src('logo.svg') | ||
.pipe(svgmin()) | ||
.pipe(gulp.dest('./out')); | ||
}); | ||
const defaultTask = () => | ||
src('logo.svg') | ||
.pipe(svgmin()) | ||
.pipe(dest('./out')); | ||
export default defaultTask; | ||
``` | ||
## Configuration file | ||
## Plugins | ||
By default, `gulp-svgmin` loads options from a `svgo.config.js` file in your project. See the [svgo’s configuration docs][svgo-config] for more info on how to write one. | ||
Optionally, you can customise the output by specifying the `plugins` option. You | ||
will need to provide the config in comma separated objects, like the example | ||
below. Note that you can either disable the plugin by setting it to false, | ||
or pass different options to change the default behaviour. | ||
You can control which directory `svgo` searches for `svgo.config.js` with the `cwd` option. Or you can use a different file name with the `configFile` option. | ||
```js | ||
gulp.task('default', function () { | ||
return gulp.src('logo.svg') | ||
.pipe(svgmin({ | ||
plugins: [{ | ||
removeDoctype: false | ||
}, { | ||
removeComments: false | ||
}, { | ||
cleanupNumericValues: { | ||
floatPrecision: 2 | ||
} | ||
}, { | ||
convertColors: { | ||
names2hex: false, | ||
rgb2hex: false | ||
} | ||
}] | ||
})) | ||
.pipe(gulp.dest('./out')); | ||
}); | ||
import { src, dest } from 'gulp'; | ||
import svgmin from 'gulp-svgmin'; | ||
const defaultTask = () => | ||
src('logo.svg') | ||
.pipe(svgmin({ | ||
// Specify an absolute directory path to | ||
// search for the config file. | ||
cwd: '/users/admin/project/assets', | ||
// This path is relative to process.cwd() | ||
// or the 'cwd' option. | ||
configFile: 'images/svg/config.js', | ||
})) | ||
.pipe(dest('./out')); | ||
export default defaultTask; | ||
``` | ||
You can view the [full list of plugins here][plugins]. | ||
## Options | ||
Instead of using a config file, you can pass an object of svgo’s options to the `gulp-svgmin` plugin. You will need to provide the config in comma separated objects, like the example below. | ||
## Beautify | ||
```js | ||
const defaultTask = () => | ||
src('logo.svg') | ||
.pipe(svgmin({ | ||
// Ensures the best optimization. | ||
multipass: true, | ||
js2svg: { | ||
// Beutifies the SVG output instead of | ||
// stripping all white space. | ||
pretty: true, | ||
indent: 2, | ||
}, | ||
// Alter the default list of plugins. | ||
plugins: [ | ||
// You can enable a plugin with just its name. | ||
'sortAttrs', | ||
{ | ||
name: 'removeViewBox', | ||
// Disable a plugin by setting active to false. | ||
active: false, | ||
}, | ||
{ | ||
name: 'cleanupIDs', | ||
// Add plugin options. | ||
params: { | ||
minify: true, | ||
} | ||
}, | ||
], | ||
})) | ||
.pipe(dest('./out')); | ||
``` | ||
You can also use `gulp-svgmin` to optimise your SVG but render a pretty output, | ||
instead of the default where all extraneous whitespace is removed: | ||
You can view the [full list of plugins here][svgo-plugins]. | ||
By default, the plugins list given to the gulp plugin will alter the default list of svgo plugins. Optionally, you can specify your plugins and set the `full` flag to `true` to indicate that your plugins list should not be merged with the default list of plugins. | ||
```js | ||
gulp.task('pretty', function () { | ||
return gulp.src('logo.svg') | ||
.pipe(svgmin({ | ||
js2svg: { | ||
pretty: true | ||
} | ||
})) | ||
.pipe(gulp.dest('./out')) | ||
}); | ||
const defaultTask = () => | ||
src('logo.svg') | ||
.pipe(svgmin({ | ||
multipass: true, | ||
// The plugins list is the full list of plugins | ||
// to use. The default list is ignored. | ||
full: true, | ||
plugins: [ | ||
'removeDoctype', | ||
'removeComments', | ||
'sortAttrs', | ||
// ... | ||
], | ||
})) | ||
.pipe(dest('./out')); | ||
``` | ||
## Per-file options | ||
@@ -88,41 +123,46 @@ | ||
returns `svgo` options. For example, if you need to prefix ids with filenames | ||
to make them unique before combining svgs with [gulp-svgstore](https://github.com/w0rm/gulp-svgstore): | ||
to make them unique before combining svgs with [gulp-svgstore][gulp-svgostore]: | ||
```js | ||
gulp.task('default', function () { | ||
return gulp.src('src/*.svg') | ||
.pipe(svgmin(function getOptions (file) { | ||
var prefix = path.basename(file.relative, path.extname(file.relative)); | ||
return { | ||
plugins: [{ | ||
cleanupIDs: { | ||
prefix: prefix + '-', | ||
minify: true | ||
} | ||
}] | ||
} | ||
})) | ||
.pipe(svgstore()) | ||
.pipe(gulp.dest('./dest')); | ||
}); | ||
const defaultTask = () => | ||
src('src/*.svg') | ||
.pipe(svgmin(function getOptions(file) { | ||
const prefix = path.basename( | ||
file.relative, | ||
path.extname(file.relative) | ||
); | ||
return { | ||
plugins: [ | ||
{ | ||
name: 'cleanupIDs', | ||
parmas: { | ||
prefix: prefix + '-', | ||
minify: true, | ||
}, | ||
}, | ||
], | ||
}; | ||
})) | ||
.pipe(svgstore()) | ||
.pipe(dest('./dest')); | ||
``` | ||
## Contributing | ||
Pull requests are welcome. If you add functionality, then please add unit tests | ||
to cover it. | ||
Pull requests are welcome. If you add functionality, then please add unit tests to cover it. | ||
## License | ||
MIT © [Ben Briggs](http://beneb.info) | ||
MIT © [Ben Briggs](https://beneb.info) | ||
[bugs]: https://github.com/svg/svgo/issues | ||
[ci]: https://travis-ci.org/ben-eb/gulp-svgmin | ||
[deps]: https://gemnasium.com/ben-eb/gulp-svgmin | ||
[gulp]: https://github.com/wearefractal/gulp | ||
[npm]: http://badge.fury.io/js/gulp-svgmin | ||
[orig]: https://github.com/svg/svgo | ||
[plugins]: https://github.com/svg/svgo/tree/master/plugins | ||
[travis-status]: https://travis-ci.org/ben-eb/gulp-svgmin | ||
[deps-status]: https://david-dm.org/ben-eb/gulp-svgmin | ||
[npm-status]: https://badge.fury.io/js/gulp-svgmin | ||
[npm-url]: https://npmjs.org/package/gulp-svgmin | ||
[gulp-url]: https://github.com/gulpjs/gulp | ||
[gulp-svgostore]: https://github.com/w0rm/gulp-svgstore | ||
[svgo-url]: https://github.com/svg/svgo | ||
[svgo-bugs]: https://github.com/svg/svgo/issues | ||
[svgo-config]: https://github.com/svg/svgo#configuration | ||
[svgo-plugins]: https://github.com/svg/svgo#built-in-plugins |
Sorry, the diff of this file is not supported yet
12411
13
114
167
+ Added@trysound/sax@0.2.0(transitive)
+ Addedcommander@7.2.0(transitive)
+ Addedcss-select@4.3.0(transitive)
+ Addedcss-what@6.1.0(transitive)
+ Addeddom-serializer@1.4.1(transitive)
+ Addeddomhandler@4.3.1(transitive)
+ Addeddomutils@2.8.0(transitive)
+ Addednth-check@2.1.1(transitive)
+ Addedpicocolors@1.0.1(transitive)
+ Addedsvgo@2.8.0(transitive)
- Removed@types/q@1.5.8(transitive)
- Removedansi-styles@3.2.1(transitive)
- Removedargparse@1.0.10(transitive)
- Removedarray-buffer-byte-length@1.0.1(transitive)
- Removedarray.prototype.reduce@1.0.7(transitive)
- Removedarraybuffer.prototype.slice@1.0.3(transitive)
- Removedavailable-typed-arrays@1.0.7(transitive)
- Removedcall-bind@1.0.7(transitive)
- Removedchalk@2.4.2(transitive)
- Removedcoa@2.0.2(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedcss-select@2.1.0(transitive)
- Removedcss-select-base-adapter@0.1.1(transitive)
- Removedcss-tree@1.0.0-alpha.37(transitive)
- Removedcss-what@3.4.2(transitive)
- Removeddata-view-buffer@1.0.1(transitive)
- Removeddata-view-byte-length@1.0.1(transitive)
- Removeddata-view-byte-offset@1.0.0(transitive)
- Removeddefine-data-property@1.1.4(transitive)
- Removeddefine-properties@1.2.1(transitive)
- Removeddom-serializer@0.2.2(transitive)
- Removeddomelementtype@1.3.1(transitive)
- Removeddomutils@1.7.0(transitive)
- Removedes-abstract@1.23.3(transitive)
- Removedes-array-method-boxes-properly@1.0.0(transitive)
- Removedes-define-property@1.0.0(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedes-object-atoms@1.0.0(transitive)
- Removedes-set-tostringtag@2.0.3(transitive)
- Removedes-to-primitive@1.2.1(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedesprima@4.0.1(transitive)
- Removedfor-each@0.3.3(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedfunction.prototype.name@1.1.6(transitive)
- Removedfunctions-have-names@1.2.3(transitive)
- Removedget-intrinsic@1.2.4(transitive)
- Removedget-symbol-description@1.0.2(transitive)
- Removedglobalthis@1.0.4(transitive)
- Removedgopd@1.0.1(transitive)
- Removedhas-bigints@1.0.2(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removedhas-property-descriptors@1.0.2(transitive)
- Removedhas-proto@1.0.3(transitive)
- Removedhas-symbols@1.0.3(transitive)
- Removedhas-tostringtag@1.0.2(transitive)
- Removedhasown@2.0.2(transitive)
- Removedinternal-slot@1.0.7(transitive)
- Removedis-array-buffer@3.0.4(transitive)
- Removedis-bigint@1.0.4(transitive)
- Removedis-boolean-object@1.1.2(transitive)
- Removedis-callable@1.2.7(transitive)
- Removedis-data-view@1.0.1(transitive)
- Removedis-date-object@1.0.5(transitive)
- Removedis-negative-zero@2.0.3(transitive)
- Removedis-number-object@1.0.7(transitive)
- Removedis-regex@1.1.4(transitive)
- Removedis-shared-array-buffer@1.0.3(transitive)
- Removedis-string@1.0.7(transitive)
- Removedis-symbol@1.0.4(transitive)
- Removedis-typed-array@1.1.13(transitive)
- Removedis-weakref@1.0.2(transitive)
- Removedisarray@2.0.5(transitive)
- Removedjs-yaml@3.14.1(transitive)
- Removedmdn-data@2.0.4(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmkdirp@0.5.6(transitive)
- Removednth-check@1.0.2(transitive)
- Removedobject-inspect@1.13.1(transitive)
- Removedobject-keys@1.1.1(transitive)
- Removedobject.assign@4.1.5(transitive)
- Removedobject.getownpropertydescriptors@2.1.8(transitive)
- Removedobject.values@1.2.0(transitive)
- Removedpossible-typed-array-names@1.0.0(transitive)
- Removedq@1.5.1(transitive)
- Removedregexp.prototype.flags@1.5.2(transitive)
- Removedsafe-array-concat@1.1.2(transitive)
- Removedsafe-regex-test@1.0.3(transitive)
- Removedsax@1.2.4(transitive)
- Removedset-function-length@1.2.2(transitive)
- Removedset-function-name@2.0.2(transitive)
- Removedside-channel@1.0.6(transitive)
- Removedsprintf-js@1.0.3(transitive)
- Removedstring.prototype.trim@1.2.9(transitive)
- Removedstring.prototype.trimend@1.0.8(transitive)
- Removedstring.prototype.trimstart@1.0.8(transitive)
- Removedsupports-color@5.5.0(transitive)
- Removedsvgo@1.3.2(transitive)
- Removedtyped-array-buffer@1.0.2(transitive)
- Removedtyped-array-byte-length@1.0.1(transitive)
- Removedtyped-array-byte-offset@1.0.2(transitive)
- Removedtyped-array-length@1.0.6(transitive)
- Removedunbox-primitive@1.0.2(transitive)
- Removedunquote@1.1.1(transitive)
- Removedutil.promisify@1.0.1(transitive)
- Removedwhich-boxed-primitive@1.0.2(transitive)
- Removedwhich-typed-array@1.1.15(transitive)
Updatedsvgo@^2.3.1