icon.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * 工程中根据res/icon/svg/*.svg
  3. * 自动生成字体图标-cqh
  4. */
  5. let gulp = require('gulp');
  6. let iconfont = require('gulp-iconfont');
  7. let consolidate = require('gulp-consolidate');
  8. let rename = require('gulp-rename');
  9. let rev = require('gulp-rev');
  10. let path = require('path');
  11. let fileUtil = require('../util/fileUtil');
  12. module.exports = {
  13. async compileSvg2Icon (repoPath, iconPrefix, fontPath = '../fonts/') {
  14. let svgPath = path.join(repoPath, './svg/*.svg');
  15. let templatePath = path.join(__dirname, './iconTemplate/');
  16. // 先清除文件夹,防止缓存
  17. await fileUtil.deleteDirector(path.join(repoPath, './css'));
  18. await fileUtil.deleteDirector(path.join(repoPath, './fonts'));
  19. await this.compileIcon(iconPrefix, fontPath, svgPath, templatePath, repoPath);
  20. },
  21. compileIcon (iconPrefix, fontPath, svgPath, templatePath, repoPath) {
  22. return new Promise((resolve, reject) => {
  23. gulp.src([svgPath])
  24. .pipe(iconfont({
  25. fontName: iconPrefix,
  26. // prependUnicode: true,
  27. // startUnicode: 0xE001,
  28. formats: ['svg', 'ttf', 'eot', 'woff'],
  29. normalize: true,
  30. centerHorizontally: true,
  31. fontHeight: 1024 // must need for perfect icon
  32. }))
  33. .on('error', function (e) {
  34. reject(e);
  35. throw new Error(e);
  36. })
  37. .on('glyphs', function (glyphs, options) {
  38. glyphs.forEach(function (glyph, idx, arr) {
  39. arr[idx].codePoint = glyph.unicode[0].charCodeAt(0).toString(16).toUpperCase()
  40. });
  41. gulp.src(path.join(templatePath, './iconTemplate.css'))
  42. .pipe(consolidate('lodash', {
  43. glyphs: glyphs,
  44. fontName: iconPrefix,
  45. fontPath: '../fonts/',
  46. cssClass: iconPrefix
  47. }))
  48. // css 给demo文件用
  49. .pipe(rename('icons.css'))
  50. .pipe(rev())
  51. .pipe(gulp.dest(path.join(repoPath, './css/')))
  52. .on('finish', function () {
  53. console.log('css file generation over!');
  54. })
  55. })
  56. .pipe(rev())
  57. .pipe(gulp.dest(path.join(repoPath, './fonts/')))
  58. .on('finish', function () {
  59. console.log('font generation over');
  60. resolve();
  61. });
  62. })
  63. }
  64. };