gulpfile.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. .pipe(cheerio({
  15. run: function ($) {
  16. // remove green-screen color
  17. $('[fill="#50E3C2"]').removeAttr('fill').parents('[fill="none"]').removeAttr('fill');
  18. },
  19. parserOptions: { xmlMode: true }
  20. }))
  21. .pipe(svgstore())
  22. .pipe(through2.obj(function (file, encoding, cb) {
  23. var $ = file.cheerio;
  24. var data = $('svg > symbol').map(function () {
  25. var viewBox = $(this).attr('viewBox').split(" ")
  26. return [
  27. '.'+ $(this).attr('id') + ' {' +
  28. ' width: ' + viewBox[2] + 'px;' +
  29. ' height: ' + viewBox[3] + 'px; ' +
  30. '}'
  31. ];
  32. }).get();
  33. var cssFile = new gutil.File({
  34. path: '../../../app/assets/stylesheets/svg-dimensions.css',
  35. contents: new Buffer(data.join("\n"))
  36. });
  37. this.push(cssFile);
  38. this.push(file);
  39. cb();
  40. }))
  41. .pipe(gulp.dest('public/assets/images'));
  42. });
  43. gulp.task('watch', function () {
  44. gulp.watch(iconsource, ['svgstore']);
  45. });
  46. gulp.task('default', ['svgstore', 'watch']);