gulpfile.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. },
  24. parserOptions: { xmlMode: true }
  25. }))
  26. .pipe(svgstore())
  27. .pipe(through2.obj(function (file, encoding, cb) {
  28. var $ = file.cheerio;
  29. var data = $('svg > symbol').map(function () {
  30. var viewBox = $(this).attr('viewBox').split(" ")
  31. return [
  32. '.'+ $(this).attr('id') + ' {' +
  33. ' width: ' + viewBox[2] + 'px;' +
  34. ' height: ' + viewBox[3] + 'px; ' +
  35. '}'
  36. ];
  37. }).get();
  38. var cssFile = new gutil.File({
  39. path: '../../../app/assets/stylesheets/svg-dimensions.css',
  40. contents: new Buffer(data.join("\n"))
  41. });
  42. this.push(cssFile);
  43. this.push(file);
  44. cb();
  45. }))
  46. .pipe(gulp.dest('public/assets/images'));
  47. });
  48. gulp.task('watch', function () {
  49. gulp.watch(iconsource, ['svgstore']);
  50. });
  51. gulp.task('default', ['svgstore', 'watch']);