Skip to content

Commit

Permalink
Preserve SVG viewBox when minifying SVGs for production
Browse files Browse the repository at this point in the history
By default svgo removes the `viewBox` attribute of the SVG, which breaks
scaling of the SVG content when rendering.

This caused various icons on the site to render at an incorrect scale.
In some cases this was not very noticeable because the natural size of
the content was close to the scaled size. In other cases the icon failed
to appear at all.

To see the difference, compare:

```
rm build/images/icons/*
NODE_ENV=production gulp build-images
cat build/images/icons/groups.svg
```

Before and after this patch is applied.

Fixes #5656
  • Loading branch information
robertknight committed Jul 18, 2019
1 parent 5f22a00 commit caf5363
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,23 @@ gulp.task('build-images', function() {
return IS_PRODUCTION_BUILD && file.path.match(/\.svg$/);
};

// See https://github.com/ben-eb/gulp-svgmin#plugins
var svgminConfig = {
plugins: [
{
// svgo removes `viewBox` by default, which breaks scaled rendering of
// the SVG.
//
// See https://github.com/svg/svgo/issues/1128
removeViewBox: false,
},
],
};

return gulp
.src(imageFiles)
.pipe(changed(IMAGES_DIR))
.pipe(gulpIf(shouldMinifySVG, svgmin()))
.pipe(gulpIf(shouldMinifySVG, svgmin(svgminConfig)))
.pipe(gulp.dest(IMAGES_DIR));
});

Expand Down

0 comments on commit caf5363

Please sign in to comment.