gulpfile.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. var gulp = require('gulp');
  2. var gutil = require('gulp-util');
  3. var rename = require('gulp-rename');
  4. var svgstore = require('gulp-svgstore');
  5. var svgmin = require('gulp-svgmin');
  6. var cheerio = require('gulp-cheerio');
  7. var through2 = require('through2');
  8. var iconsource = 'public/assets/images/icons/*.svg'
  9. gulp.task('svgstore', function () {
  10. return gulp
  11. .src(iconsource)
  12. .pipe(rename({prefix: 'icon-'}))
  13. .pipe(svgmin({
  14. js2svg: {
  15. pretty: true
  16. }
  17. }))
  18. .pipe(cheerio({
  19. run: function ($) {
  20. // remove green-screen color
  21. $('[fill="#50E3C2"]').removeAttr('fill').parents('[fill="none"]').removeAttr('fill');
  22. $('[fill="#BD0FE1"]').attr('fill', 'currentColor').parents('[fill="none"]').removeAttr('fill');
  23. // color in Sketch changed slightly BD0FE1 -> BD10E0
  24. $('[fill="#BD10E0"]').attr('fill', 'currentColor').parents('[fill="none"]').removeAttr('fill');
  25. },
  26. parserOptions: { xmlMode: true }
  27. }))
  28. .pipe(svgstore())
  29. .pipe(through2.obj(function (file, encoding, cb) {
  30. var $ = file.cheerio;
  31. var data = $('svg > symbol').map(function () {
  32. var viewBox = $(this).attr('viewBox').split(" ")
  33. return [
  34. '.'+ $(this).attr('id') + ' {' +
  35. ' width: ' + viewBox[2] + 'px;' +
  36. ' height: ' + viewBox[3] + 'px; ' +
  37. '}'
  38. ];
  39. }).get();
  40. var cssFile = new gutil.File({
  41. path: '../../../app/assets/stylesheets/svg-dimensions.css',
  42. contents: new Buffer(data.join("\n"))
  43. });
  44. this.push(cssFile);
  45. this.push(file);
  46. cb();
  47. }))
  48. .pipe(gulp.dest('public/assets/images'));
  49. });
  50. gulp.task('watch', function () {
  51. gulp.watch(iconsource, ['svgstore']);
  52. });
  53. gulp.task('default', ['svgstore', 'watch']);