-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
executable file
·82 lines (70 loc) · 2.29 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const gulp = require('gulp');
const svgo = require('gulp-svgo');
const iconfont = require('gulp-iconfont');
const Table = require('cli-table3'); // Note: cli-table2 might be outdated, consider using cli-table3
const fontmin = require('gulp-fontmin');
const cheerio = require('gulp-cheerio');
const pug = require('gulp-pug');
const svg2png = require('gulp-svg2png');
// Helper function to resize SVGs for PNG conversion
function resizeForPng($, file) {
let $svg = $('svg');
$svg.attr({ height: 500, width: 500, preserveAspectRatio: 'xMidYMid' });
}
// Helper function to clean up SVGs coming from Figma
function cleanUpFigmaSVG($, file) {
$('use').each(function() {
let $use = $(this);
let transform = $use.attr('transform');
let selector = $use.attr('xlink:href');
let $target = $(selector);
$target.attr('transform', transform);
$use.replaceWith($target);
});
$('[figma\\:type]').removeAttr('figma:type');
$('[fill="#FFFFFF"]').remove();
}
// Build task
function build() {
let glyphList = [];
let options = {
fontName: 'DIn pIqaD',
prependUnicode: true,
ascent: 588.235,
descent: 235.294,
fontHeight: 1000
};
let svgoPlugins = [
{ transformsWithOnePath: true, removeEditorsNSData: true, removeDesc: true, removeTitle: true, collapseGroups: true }
];
return gulp.src('svg/*.svg')
.pipe(cheerio(cleanUpFigmaSVG))
.pipe(svgo({ plugins: svgoPlugins }))
.pipe(gulp.dest('processed-svg'))
.pipe(iconfont(options))
.on('glyphs', (glyphs, options) => {
console.log(`Create font "${options.fontName}"`);
let table = new Table({ head: ['NAME', 'UNICODE'] });
glyphs.forEach(glyph => {
table.push([glyph.name, glyph.unicode[0]]);
glyphList.push([glyph.name, glyph.unicode[0]]);
});
console.log(table.toString());
let pugOpts = { locals: { glyphs: glyphList } };
gulp.src('demo.pug').pipe(pug(pugOpts)).pipe(gulp.dest('demo'));
})
.pipe(gulp.dest('dist'))
.on('end', () => {
gulp.src('svg/*.svg')
.pipe(cheerio(resizeForPng))
.pipe(svg2png())
.pipe(gulp.dest('./png'));
});
}
// Watch task
function watch() {
gulp.watch('svg/*.svg', build);
gulp.watch('demo.pug', build);
}
// Default task
exports.default = gulp.series(build, watch);