gulpfile.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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('cheerio');
  7. var gcheerio = require('gulp-cheerio');
  8. var through2 = require('through2');
  9. var path = require('path');
  10. var iconsource = 'icons/*.svg'
  11. function build(cb) {
  12. gulp
  13. .src(iconsource)
  14. .pipe(rename({prefix: 'icon-'}))
  15. .pipe(svgmin(function getOptions(file){
  16. var prefix = path.basename(
  17. file.relative,
  18. path.extname(file.relative)
  19. );
  20. return {
  21. plugins: [
  22. {
  23. removeViewBox: false,
  24. removeTitle: false,
  25. cleanupIDs: {
  26. prefix: prefix + '-',
  27. minify: true
  28. }
  29. },
  30. ],
  31. js2svg: {
  32. pretty: true,
  33. },
  34. }
  35. }))
  36. .pipe(gcheerio({
  37. run: function ($) {
  38. // remove green-screen color
  39. $('[fill="#50E3C2"]').removeAttr('fill').parents('[fill="none"]').removeAttr('fill');
  40. $('[fill="#BD0FE1"]').attr('fill', 'currentColor').parents('[fill="none"]').removeAttr('fill');
  41. // color in Sketch changed slightly BD0FE1 -> BD10E0
  42. $('[fill="#BD10E0"]').attr('fill', 'currentColor').parents('[fill="none"]').removeAttr('fill');
  43. },
  44. parserOptions: { xmlMode: true }
  45. }))
  46. .pipe(svgstore())
  47. .pipe(through2.obj(function (file, encoding, cb) {
  48. // Side effect: generate app/assets/stylesheets/svg-dimensions.css with
  49. // information about the available icon sizes.
  50. var $ = cheerio.load(file.contents)
  51. var data = $('svg > symbol').map(function (_i, tag) {
  52. var viewBox = tag.attribs.viewbox.split(" ")
  53. return [
  54. '.'+ $(this).attr('id') + ' {' +
  55. ' width: ' + viewBox[2] + 'px;' +
  56. ' height: ' + viewBox[3] + 'px; ' +
  57. '}'
  58. ];
  59. }).get();
  60. var cssFile = new gutil.File({
  61. path: '../../../app/assets/stylesheets/svg-dimensions.css',
  62. contents: Buffer.from(data.join("\n"))
  63. });
  64. this.push(cssFile);
  65. this.push(file);
  66. cb();
  67. }))
  68. .pipe(gulp.dest('./'));
  69. cb();
  70. }
  71. exports.default = function(cb) {
  72. gulp.watch(iconsource, build);
  73. cb();
  74. }
  75. exports.build = build