Skip to content

Commit

Permalink
Merge 1f89710 into 2067c1a
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnAlbin committed Apr 4, 2021
2 parents 2067c1a + 1f89710 commit 39e5214
Show file tree
Hide file tree
Showing 18 changed files with 4,677 additions and 7,297 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"presets": ["env"]
"presets": [["@babel/preset-env", {"targets": {"node": "10"}}]]
}
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
indent_size = 2

[package.json]
indent_size = 2
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ language: node_js
node_js:
- 10
- 12
- 14
- node
script: "npm test"
after_success:
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'
179 changes: 106 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# [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]

> Minify SVG with [SVGO][orig].
> A gulp plugin to minify SVG files with [SVGO][orig].
*If you have any difficulties with the output of this plugin, please use the
[SVGO tracker][bugs].*


## Install

With [npm](https://npmjs.org/package/gulp-svgmin) do:
Expand All @@ -14,113 +13,147 @@ With [npm](https://npmjs.org/package/gulp-svgmin) do:
npm install gulp-svgmin
```


## Example

```js
var gulp = require('gulp');
var svgmin = require('gulp-svgmin');

gulp.task('default', function () {
return gulp.src('logo.svg')
.pipe(svgmin())
.pipe(gulp.dest('./out'));
});
import { src, dest } from 'gulp';
import svgmin from 'gulp-svgmin';

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][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][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

To have per-file options, pass a function, that receives `file` object and
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):

```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](http://beneb.info) and [John Albin Wilkins](http://john.albin.net)

[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
[config]: https://github.com/svg/svgo#configuration
[plugins]: https://github.com/svg/svgo#built-in-plugins
Loading

0 comments on commit 39e5214

Please sign in to comment.